Skip to content

Instantly share code, notes, and snippets.

@lukeredpath
Forked from mattwynne/sketch.rb
Created June 29, 2012 16:04
Show Gist options
  • Save lukeredpath/3018837 to your computer and use it in GitHub Desktop.
Save lukeredpath/3018837 to your computer and use it in GitHub Desktop.
sketch for Matt Wynne
class Organization
def initialize(parameters = {})
@parameters = parameters
end
def to_param
"42"
end
def valid?
rand > 0.5
end
end
class InMemoryStore
def initialize
@objects = []
end
def store(object)
@objects << object
end
end
ORGANIZATION_STORE = InMemoryStore.new
class OrganizationRepository
def initialize(storage)
@listeners = []
@storage = storage
end
def add_listener(listener)
@listeners << listener
end
def get(id)
#TODO
end
def add(organization)
unless organization.valid?
send_to_listeners(:organization_was_invalid, organization)
return
end
if @storage.store(organization)
send_to_listeners(:organzation_was_stored, organization)
else
send_to_listeners(:organization_was_not_stored, organization)
end
end
def update(organization)
#TODO
end
def send_to_listeners(msg, *args)
@listeners.each { |l| l.send(msg, *args) if l.respond_to?(msg) }
end
end
class OrganizationsController
attr_accessor :repository
def create(params = {})
organization = Organization.new(params
repository.add(organization)
end
def organzation_was_stored(organization)
redirect_to organizations_path(organization)
end
def organization_was_not_stored(organization)
render("failed")
end
def organization_was_invalid(organization)
# @errors = organization.errors
render("failed")
end
private
def repository
@repository ||= OrganizationRepository.new(ORGANIZATION_STORE)
@repository.add_listener(self)
end
def find_from_repository
@repository.get(params[:id])
end
def redirect_to(path)
puts "redirecting_to #{path}"
end
def render(renderable)
puts "rendering #{renderable}"
end
def current_user
Object.new
end
def organizations_path(org)
"/organizations/#{org.to_param}"
end
end
def test_create_action_adds_organization_to_repository
controller = OrganizationsController.new
controller.repository = mock('repostitory')
params = {}
organization = stub
Organization.stub(:new).with(params).and_return(organization)
controller.repostitory.expects(:add).with(organization)
controller.create(params)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment