Skip to content

Instantly share code, notes, and snippets.

@tjstankus
Last active October 26, 2015 02:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjstankus/438095e161dfa0f2a73f to your computer and use it in GitHub Desktop.
Save tjstankus/438095e161dfa0f2a73f to your computer and use it in GitHub Desktop.
Functional Ruby
# - Reusable, immutable objects
# - Inject dependencies in constructor
class UserQuery
attr_reader :query
def initialize(query)
@query = query
end
def with(query)
self.class.new(query)
end
def call
query.execute
end
end
# `PersistPerson` is bound the the interface of user_repository, which is better
# than being bound to, say, the hardcoded class name.
class PersistPerson
attr_reader :user_repository
def initialize(user_repository)
@user_repository = user_repository
end
def call(person)
user_repostiory.save(person)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment