Sha256: 098afc058b49dfd23b416a67bc5526960d627099c32f7acdb75e8e1a94882628

Contents?: true

Size: 1.47 KB

Versions: 12

Compression:

Stored size: 1.47 KB

Contents

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

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
bogus-0.1.7 lib/bogus/fake_configuration.rb
bogus-0.1.6 lib/bogus/fake_configuration.rb
bogus-0.1.5 lib/bogus/fake_configuration.rb
bogus-0.1.4 lib/bogus/fake_configuration.rb
bogus-0.1.3 lib/bogus/fake_configuration.rb
bogus-0.1.2 lib/bogus/fake_configuration.rb
bogus-0.1.1 lib/bogus/fake_configuration.rb
bogus-0.1.0 lib/bogus/fake_configuration.rb
bogus-0.0.4 lib/bogus/fake_configuration.rb
bogus-0.0.3 lib/bogus/fake_configuration.rb
bogus-0.0.3.rc.2 lib/bogus/fake_configuration.rb
bogus-0.0.3.rc.1 lib/bogus/fake_configuration.rb