Last active
August 29, 2015 13:57
-
-
Save gogogarrett/9563565 to your computer and use it in GitHub Desktop.
basic idea for implementing a state machine. Not tested or throught out in much detail.. but meh.
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
| module Persistance | |
| class Trade < ActiveRecord::Base | |
| validates :item_id, :actor_id, presence: true | |
| def state | |
| super.to_s | |
| end | |
| end | |
| end |
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
| module Policy | |
| class Trade | |
| def initialize(item, requester, quanity) | |
| @item, @requester, @quanity = item, requester, quanity | |
| end | |
| def valid? | |
| items.size >= quanity && has_enough_funds?(requester, items) | |
| end | |
| private | |
| attr_reader :item, :requester, :quanity | |
| def items | |
| Item.where(item_type: item.class) | |
| end | |
| def has_enough_funds(requester, items) | |
| requester.funds == items.map(&:cost) | |
| end | |
| end | |
| end |
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
| module Service | |
| class CreateTrade | |
| def initialize(item, requester) | |
| @item, @requester = item, requester | |
| end | |
| def call | |
| Persistance::Trade.create(item_id: item.id, actor_id: requester.id) | |
| StateKeeper.new(item, requester).requested | |
| end | |
| private | |
| attr_reader :item, :requester | |
| end | |
| end |
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
| class StateKeeper | |
| def initialize(item, actor) | |
| @item, @actor = item, actor | |
| end | |
| def requested! | |
| transision_to(:request) | |
| end | |
| def complete! | |
| transision_to(:accepted) | |
| end | |
| def current_state | |
| Persistance::Trade.where(item_id: item.id, actor_id: actor.id).state | |
| end | |
| private | |
| attr_reader :item, :actor | |
| def transision_to(state=nil) | |
| Persistance::Trade.where(item_id: item.id, actor_id: actor.id).update(state: state) | |
| end | |
| end |
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
| class TradeRequest # this could be a controller action I suppose | |
| def self.trade(item, requester, quanity) | |
| if Policy::Trade.new(item, requester, quanity).valid? | |
| Service::CreateTrade.new(item, requester).call | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment