Last active
October 26, 2015 02:43
-
-
Save tjstankus/438095e161dfa0f2a73f to your computer and use it in GitHub Desktop.
Functional Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# - 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