Se ha denunciado esta presentación.
Utilizamos tu perfil de LinkedIn y tus datos de actividad para personalizar los anuncios y mostrarte publicidad más relevante. Puedes cambiar tus preferencias de publicidad en cualquier momento.

[Practical] Functional Programming in Rails

2.993 visualizaciones

Publicado el

Tired of writing messy messy "if" statements for all your exception handling? Learn how to take full advantage of functional programming theory, without the theory, Ruby-style!

https://github.com/mindeavor/solid_use_case

Publicado en: Software
  • DOWNLOAD FULL BOOKS, INTO AVAILABLE FORMAT ......................................................................................................................... ......................................................................................................................... 1.DOWNLOAD FULL. PDF EBOOK here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB Ebook here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... 1.DOWNLOAD FULL. doc Ebook here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... 1.DOWNLOAD FULL. PDF EBOOK here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB Ebook here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... 1.DOWNLOAD FULL. doc Ebook here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... ......................................................................................................................... ......................................................................................................................... .............. Browse by Genre Available eBooks ......................................................................................................................... Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult,
       Responder 
    ¿Estás seguro?    No
    Tu mensaje aparecerá aquí

[Practical] Functional Programming in Rails

  1. 1. FUNCTIONAL PROGRAMMING Practical! RAILS RUBYIN ON BY
  2. 2. Overview • What is a Service Layer? • Railway Oriented Programming • Creating Beautifully Composable Services
  3. 3. Whatisa LAYER ? SERVICE
  4. 4. Whatisa LAYER SERVICE ??
  5. 5. A Service is an object that represents a process.
  6. 6. A process is something you do. • Typically initiated by the user • Usually implemented as a dedicated class • Named with verbs, not nouns • Examples: SignupUser, PurchaseItem, etc. SERVICES
  7. 7. SignupUser Purchase Item RAILS SERVICES SERVICE LAYER Payout Account
  8. 8. A Service in Rails SERVICES CODEEXAMPLE A Service Usage in Controller
  9. 9. [Naive] Validations CODEEXAMPLE A Service Usage in Controller SERVICES
  10. 10. Why a Service useful? • It encapsulates domain logic • It thins out your models & controllers • It ties together cross-cutting concerns • It makes your Rails app easier to test. SERVICES
  11. 11. A service layer is about clear separation. • Rails depends on the service layer • The service layer knows nothing about Rails (routes, controllers, & views) • Other things could depend on the service layer (REST API, web sockets, rake tasks, etc.) • ActiveRecord is unavoidable, however. SERVICES
  12. 12. SERVICES
  13. 13. Quick Recap • A service is an object that represents a process • Services encapsulate domain logic for specific processes • No functional programming (yet). SERVICES
  14. 14. RAILWAYORIENTED PROGRAMMING http://fsharpforfunandprofit.com/posts/recipe-part2/
  15. 15. Services: Another Perspective A service is a sequence of steps • They often involve a lot of setup • Authentication, authorization, validation, etc. Any one of these steps could fail • How to handle failure?? RAILWAY
  16. 16. Naive Validations CODEEXAMPLE(REVISITED) A Service RAILWAY
  17. 17. Can we do be er? RAILWAY What if each step was an independent method? How would each step communicate?
  18. 18. Source: http://www.slideshare.net/ScottWlaschin/railway-oriented-programming RAILWAY The “Switch” Input→ Success! Failure. fetch_item DIAGRAM
  19. 19. RAILWAY The “Switch” Input→ Success! Failure fetch_item CODEEXAMPLE
  20. 20. RAILWAY The “Switch” Input→ Success! Failure. fetch_item CODEEXAMPLE Failure. Success!
  21. 21. Source: http://www.slideshare.net/ScottWlaschin/railway-oriented-programming RAILWAY Our Target Flow onsuccess Success! Failure. Input→ fetch_item validate onfailure
  22. 22. Source: http://www.slideshare.net/ScottWlaschin/railway-oriented-programming RAILWAY Our Target Flow Success! Failure. Input→ create_ordervalidatefetch_item
  23. 23. Source: http://www.slideshare.net/ScottWlaschin/railway-oriented-programming RAILWAY Our Target Flow Success! Failure. Input→ create_ordervalidatefetch_item IT’DBECOOLIFWECOULDCONNECTTHESETOGETHER
  24. 24. Source: http://www.slideshare.net/ScottWlaschin/railway-oriented-programming RAILWAY Connecting Switches Success! Failure. Input→ create_ordervalidatefetch_item create_ordervalidatefetch_item
  25. 25. Source: http://www.slideshare.net/ScottWlaschin/railway-oriented-programming RAILWAY Connecting Switches Success! Failure. Input→ create_ordervalidatefetch_item create_ordervalidatefetch_item create_ordervalidatefetch_item Success! Failure.
  26. 26. RAILWAY Connecting Switches CODEEXAMPLES create_ordervalidatefetch_item ?? ??
  27. 27. RAILWAY Connecting Switches CODEEXAMPLES create_ordervalidatefetch_item ?? ?? HOWDOWECONNECT
 ITALLTOGETHER? ? ? ? ? ? ? ? ?
  28. 28. RAILWAY Answer:
  29. 29. RAILWAY ¯_(ツ)_/¯ Answer:
  30. 30. RAILWAY ¯_(ツ)_/¯ Answer: ITDOESN’TMATTER!
  31. 31. RAILWAY ¯_(ツ)_/¯ Answer: ITDOESN’TMATTER! :D
  32. 32. RAILWAY ¯_(ツ)_/¯ Answer: ITDOESN’TMATTER! :D (actual answer: monads)
  33. 33. There’s a gem for that! Doesn’t matter. RAILWAY github.com/mindeavor/solid_use_case
  34. 34. There’s a gem for that! Don’t worry.
  35. 35. Handling failures? RAILWAY What about github.com/mindeavor/solid_use_case
  36. 36. Quick Recap • Split steps into methods • Return a success or failure in each one • Elegant (with gem) error handling! • Steps can be tested independently RAILWAY
  37. 37. COMPOSINGSERVICES FORMAXIMUMCOOLNESS
  38. 38. Source: http://www.slideshare.net/ScottWlaschin/railway-oriented-programming COMPOSE Remember This? Success! Failure Input→ create_ordervalidatefetch_item create_ordervalidatefetch_item create_ordervalidatefetch_item Success! Failure
  39. 39. Source: http://www.slideshare.net/ScottWlaschin/railway-oriented-programming COMPOSE Input→ Success! Failure create_ordervalidatefetch_item What if we treat it like a single thing…
  40. 40. Source: http://www.slideshare.net/ScottWlaschin/railway-oriented-programming COMPOSE Input→ Success! FailurePurchaseItem What if we treat it like a single thing…
  41. 41. Source: http://www.slideshare.net/ScottWlaschin/railway-oriented-programming COMPOSE Input→ PurchaseItem Success! Failure What if we treat it like a single thing…
  42. 42. Source: http://www.slideshare.net/ScottWlaschin/railway-oriented-programming COMPOSE …and then connect it to other things?? Input→ PurchaseItem Success! Failure
  43. 43. Source: http://www.slideshare.net/ScottWlaschin/railway-oriented-programming COMPOSE Input→ PurchaseItem EmailReceipt Success! Failure …and then connect it to other things??
  44. 44. Service Composition CODEEXAMPLE COMPOSE
  45. 45. Service Composition CODEEXAMPLE Another Service! COMPOSE
  46. 46. Service Composition CODEEXAMPLE Another Service! COMPOSE
  47. 47. Recap • We can compose services like we can steps (in fact we can compose anything that returns our success or failure) • Functional programming rocks! COMPOSE
  48. 48. Conclusion • Service layers are great for apps more complex than CRUD • Railway oriented programming is great for seamless error handling • Functional programming gives us composable services!
  49. 49. In the end, don’t take my word for it.
  50. 50. TRY ITYOURSELF!
  51. 51. Thanks! H urrah! END. THE BY - Gilbert (@mindeavor)
  52. 52. Further Reading • http://martinfowler.com/eaaCatalog/serviceLayer.html • https://gist.github.com/blaix/5764401 • http://fsharpforfunandprofit.com/posts/recipe-part2/ • The gem: https://github.com/mindeavor/solid_use_case

×