1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
module Bogus
class FakeConfiguration
def include?(name)
fakes.key?(name)
end
def fake(name, opts = {}, &block)
opts = opts.dup
class_block = opts.delete(:class)
fakes[name] = FakeDefinition.new(name: name,
opts: opts,
stubs: stubs_hash(&block),
class_block: class_block)
end
def evaluate(&block)
instance_eval(&block)
end
def get(name)
fakes[name]
end
private
def stubs_hash(&block)
stubs = StubsConfiguration.new(&block)
stubs.stubs
end
def fakes
@fakes ||= {}
end
end
class FakeDefinition
attr_reader :name, :class_block, :opts, :stubs
def initialize(attrs = {})
@name = attrs[:name]
@class_block = attrs[:class_block]
@opts = attrs[:opts] || {}
@stubs = attrs[:stubs] || {}
end
def merge(other)
FakeDefinition.new(name: other.name,
opts: opts.merge(other.opts),
stubs: stubs.merge(other.stubs),
class_block: other.class_block || class_block)
end
end
class StubsConfiguration
include ProxiesMethodCalls
def initialize(&block)
proxy(:add_stub).instance_eval(&block) if block
end
def add_stub(name, value = nil, &block)
stubs[name] = block || value
end
def stubs
@stubs ||= {}
end
end
end
|