Created
November 13, 2011 16:23
-
-
Save nicholasjhenry/1362288 to your computer and use it in GitHub Desktop.
Recycle App with Repository #railsisnotyourapp
This file contains hidden or 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
# RecycleApp | |
module RecycleApp | |
class << self | |
attr_accessor :can_repository | |
def config | |
yield self | |
end | |
end | |
end | |
class RecycleApp::Can | |
attr_reader :id, :amount | |
def initialize(repository=RecycleApp::can_repository) | |
@amount = 0 | |
@repository = repository | |
end | |
def incr | |
@amount += 1 | |
@repository.store(self) | |
end | |
end | |
# Rails App | |
RecycleApp.config do |config| | |
config.can_repository = CanRepository.new | |
end | |
class CanRepository | |
def store(can) | |
can.id ? create(can) : update(can) | |
end | |
def create(can) | |
new_can = Can.create!(:amount => can.amount) | |
can.id = new_can.id | |
end | |
def update(can) | |
update_can = can.find(can.id) | |
update_can.attributes(:amount => amount) | |
update_can.save! | |
end | |
end | |
class Can < ActiveRecord | |
end | |
# Usage | |
c = RecycleApp::Can.new | |
c.incr | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment