Guest User

Untitled

a guest
Jan 18th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. # Functional Ruby - Notes
  2.  
  3. - immutable (don't modify inputs)
  4. - prefer stateless objects
  5.  
  6. ## Prevent access to initial state
  7. ```ruby
  8. class UserQuery
  9. def initialize(query)
  10. @query = query
  11. end
  12.  
  13. def with(query)
  14. self.class.new(query)
  15. end
  16.  
  17. def call
  18. query.execute
  19. end
  20. end
  21. ```
  22.  
  23. Consider:
  24. use initializer to ONLY inject dependencies
  25. pass ALL inputs to methods
  26. - less coupling
  27. - instantiate objects once, use them numerous times
  28. - data (from db or external API) becomes first class
  29. - make sure this is well defined so that you can compose
  30.  
  31. Object composition:
  32.  
  33. ```ruby
  34. input = { name: "Person" }
  35.  
  36. persist_person.call(
  37. validate.call(
  38. coerce.call(input)
  39. )
  40. )
  41. ```
  42.  
  43. __Lazy function calling__
  44.  
  45. Partially implement function based on arguments passed in
Add Comment
Please, Sign In to add comment