Sha256: c7cd59340ccdd9a7c68cd93cdef9de1e60118095262085693095f94305eb5eb7

Contents?: true

Size: 1.14 KB

Versions: 8

Compression:

Stored size: 1.14 KB

Contents

module Bogus
  class CreatesFakesWithStubbedMethods
    extend 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

8 entries across 8 versions & 1 rubygems

Version Path
bogus-0.1.7 lib/bogus/fakes/creates_fakes_with_stubbed_methods.rb
bogus-0.1.6 lib/bogus/fakes/creates_fakes_with_stubbed_methods.rb
bogus-0.1.5 lib/bogus/fakes/creates_fakes_with_stubbed_methods.rb
bogus-0.1.4 lib/bogus/fakes/creates_fakes_with_stubbed_methods.rb
bogus-0.1.3 lib/bogus/creates_fakes_with_stubbed_methods.rb
bogus-0.1.2 lib/bogus/creates_fakes_with_stubbed_methods.rb
bogus-0.1.1 lib/bogus/creates_fakes_with_stubbed_methods.rb
bogus-0.1.0 lib/bogus/creates_fakes_with_stubbed_methods.rb