Skip to content

Instantly share code, notes, and snippets.

@benhoskings
Forked from geelen/1_coupled.rb
Created April 3, 2012 07:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save benhoskings/2289996 to your computer and use it in GitHub Desktop.
Save benhoskings/2289996 to your computer and use it in GitHub Desktop.
# 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
# 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
# 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
# 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
# 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
# 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
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
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