Sha256: a603c912199b0cb8bca4f7616614c755af7865f51f3bc12828747e6ceae50b0a

Contents?: true

Size: 1.15 KB

Versions: 4

Compression:

Stored size: 1.15 KB

Contents

module Bogus
  class CreatesFakesWithStubbedMethods
    extend Bogus::Takes

    takes :multi_stubber, :creates_fakes,
      :responds_to_everything, :fake_configuration

    def create(name = nil, methods = {}, &block)
      if name.is_a?(Hash)
        methods = name
        name = nil
      end

      fake = responds_to_everything unless name

      fake_opts, methods = split_methods(methods)
      fake_definition = get_configuration(name, fake_opts, methods, block)

      fake ||= creates_fakes.create(fake_definition.name, fake_definition.opts,
                                    &fake_definition.class_block)

      multi_stubber.stub_all(fake, fake_definition.stubs)
    end

    private

    def split_methods(methods)
      fake_args = proc{ |k,_| [:as].include?(k) }
      [methods.select(&fake_args), methods.reject(&fake_args)]
    end

    def get_configuration(name, fake_opts, methods, block)
      fake = FakeDefinition.new(name: name, opts: fake_opts, stubs: methods, class_block: block)
      return fake unless fake_configuration.include?(name)

      configured_fake = fake_configuration.get(name)
      configured_fake.merge(fake)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
bogus-0.0.4 lib/bogus/creates_fakes_with_stubbed_methods.rb
bogus-0.0.3 lib/bogus/creates_fakes_with_stubbed_methods.rb
bogus-0.0.3.rc.2 lib/bogus/creates_fakes_with_stubbed_methods.rb
bogus-0.0.3.rc.1 lib/bogus/creates_fakes_with_stubbed_methods.rb