-
-
Save benhoskings/2289996 to your computer and use it in GitHub Desktop.
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
# Bad because the dependency on ModelClass and ApiClass are hard-coded | |
module HitTheApi | |
def self.go | |
ids_to_update = ModelClass.what_ids_should_I_update | |
ids_to_update.each do |id| | |
data = ApiClass.fetch_me_data(id) | |
ModelClass.persist_data(id, data) | |
end | |
end | |
end |
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
# Cleaner 'go' method, but now the dependencies are hard-coded and pushed deeper. Worse. | |
module HitTheApi | |
def self.go | |
ids_to_update.each do |id| | |
fetch_and_persist(id) | |
end | |
end | |
private | |
def self.ids_to_update | |
ModelClass.what_ids_should_I_update | |
end | |
def self.fetch_and_persist(id) | |
data = ApiClass.fetch_me_data(id) | |
ModelClass.persist_data(id, data) | |
end | |
end |
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
# Deps passed in makes things easy to test | |
module HitTheApi | |
def self.go(model, api) | |
ids_to_update = model.what_ids_should_I_update | |
ids_to_update.each do |id| | |
data = api.fetch_me_data(id) | |
model.persist_data(id, data) | |
end | |
end | |
end |
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
# If the 'go' method needs to do something more complex, breaking out private methods starts to be painful | |
module HitTheApi | |
def self.go(model, api) | |
ids_to_update(model).each do |id| | |
fetch_and_persist(api, model, id) | |
end | |
end | |
private | |
def self.ids_to_update(model) | |
model.what_ids_should_I_update | |
end | |
def self.fetch_and_persist(api, model, id) | |
data = api.fetch_me_data(id) | |
model.persist_data(id, data) | |
end | |
end |
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
# Similar to 2, but now you're instantiating something for no conceptual reason | |
# or any real benefit to the code | |
module HitTheApi | |
def initialize(model, api) | |
@model = model | |
@api = api | |
end | |
def go | |
ids_to_update = @model.what_ids_should_I_update | |
ids_to_update.each do |id| | |
data = @api.fetch_me_data(id) | |
@model.persist_data(id, data) | |
end | |
end | |
end |
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
# Retains 2's testability but without 2a's ugly internal methods, but pushing the use of | |
# instance variables down into internal methods feels bad. | |
module HitTheApi | |
def initialize(model, api) | |
@model = model | |
@api = api | |
end | |
def go | |
ids_to_update.each do |id| | |
fetch_and_persist(id) | |
end | |
end | |
private | |
def ids_to_update | |
@model.what_ids_should_I_update | |
end | |
def fetch_and_persist(id) | |
data = @api.fetch_me_data(id) | |
@model.persist_data(id, data) | |
end | |
end |
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
module HitTheApi | |
attr_writer :api_getter, :model_getter | |
def go | |
ids_to_update.each do |id| | |
fetch_and_persist(id) | |
end | |
end | |
private | |
def ids_to_update | |
model.what_ids_should_I_update | |
end | |
def fetch_and_persist(id) | |
data = api.fetch_me_data(id) | |
model.persist_data(id, data) | |
end | |
def api | |
@api ||= api_getter.call | |
end | |
def api_getter | |
@api_getter ||= ApiClass.public_method(:new) | |
end | |
def model | |
@model ||= model_getter.call | |
end | |
def model_getter | |
@model_getter ||= ModelClass.public_method(:new) | |
end | |
end |
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
module HitTheApi | |
def self.go | |
ids_to_update = model_class.what_ids_should_I_update | |
ids_to_update.each do |id| | |
data = api_class.fetch_me_data(id) | |
model_class.persist_data(id, data) | |
end | |
end | |
def self.model_class | |
ModelClass | |
end | |
def self.api_class | |
ApiClass | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment