class ContextRunner
DANGER = EntityModel.create_access_model('danger')
DANGER_ENTITY = Entity.find_by(table_name: 'danger')
include FactoryGirl::Syntax::Methods
def initialize(opts={})
opts.each do |var, value|
let(var) do
value
end
end
end
def let!(name)
result = yield
self.class.send(:define_method, name) do
result
end
end
def let(name)
self.class.send(:define_method, name) do
if instance_variable_defined?(let_instance_var(name))
instance_variable_get(let_instance_var(name))
else
result = yield
instance_variable_set(let_instance_var(name), result)
result
end
end
end
def let_instance_var(name)
"@___let_var_#{name}"
end
def run(name)
ActiveRecord::Base.transaction do
block = RspecContextWrapper.get(name)
instance_exec(&block)
self
end
end
alias include_context run
def self.init
new(stage_1: Stage.first, danger_dimension: DANGER_ENTITY, danger_low: DANGER.first, danger_high: DANGER.last)
end
end
module RspecContextWrapper
CONTEXTS = {}
module_function
def add(name, &block)
CONTEXTS[name] = block
RSpec.shared_context(name, &block)
end
def get(name)
CONTEXTS[name]
end
end