Created
April 13, 2015 19:23
-
-
Save brunoocasali/35decc644893bd077ecd to your computer and use it in GitHub Desktop.
Tirei o proc!
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 MpaStory | |
module Job | |
class FakeNotificationCounter | |
def initialize | |
@count = {} | |
end | |
def increment(name) | |
@count[name.to_sym] ||= 0 | |
@count[name.to_sym] += 1 | |
end | |
def method_missing(name) | |
@count[name.to_sym] | |
end | |
end | |
class FakeBusinessRuleParser | |
attr_reader :business_rules | |
def initialize | |
@business_rules = {} | |
end | |
def <<(rule, expression) | |
@business_rules[rule] = expression | |
init_attributes_reader | |
end | |
private | |
def init_attributes_reader | |
@business_rules.each do |business_rule, expression| | |
create_attribute_reader(business_rule) { expression } | |
end | |
end | |
def create_attribute_reader(business_rule, &expression) | |
self.class_eval { | |
define_method(business_rule, &expression) | |
} | |
end | |
end | |
class FakeNotificationSender | |
attr_reader :notifications | |
def initialize | |
@counter = FakeNotificationCounter.new | |
@notifications = [] | |
end | |
def <<(value) | |
@notifications << value | |
init_attributes_reader | |
end | |
def count | |
@counter | |
end | |
private | |
def init_attributes_reader | |
@notifications.each do |notification| | |
create_attribute_reader notification | |
end | |
end | |
def create_attribute_reader(notification) | |
self.class.send :define_method, notification do |params = {}| | |
@counter.increment notification | |
end | |
end | |
end | |
def self.included(base) | |
base.extend ClassMethods | |
instance = Instance.create status: InstanceStatus::READY | |
@@_artifact = Artifact.new instance | |
@@_notification_sender = FakeNotificationSender.new | |
@@_business_rule_parser = FakeBusinessRuleParser.new | |
@@_condition = 0 | |
base.instance_variable_set :@_klass, base | |
@@_klass = base | |
base.class_eval do | |
def artifacts | |
@@_artifact | |
end | |
def condition | |
@@_klass.class_variable_get :@@_condition | |
end | |
def set_condition(value) | |
@@_condition = value | |
end | |
def notifications | |
@@_notification_sender | |
end | |
def business_rules | |
@@_business_rule_parser | |
end | |
end | |
end | |
module ClassMethods | |
def artifacts | |
@_klass.class_variable_get :@@_artifact | |
end | |
def condition | |
@_klass.class_variable_get :@@_condition | |
end | |
def notifications | |
@_klass.class_variable_get :@@_notification_sender | |
end | |
def business_rules | |
@_klass.class_variable_get :@@_business_rule_parser | |
end | |
def perform | |
job = @_klass.new | |
job.perform | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment