Sha256: 16f5cc91adc105d7d8bf7ee34c5e03f9061421b55ea741befeafe51f5e809fad

Contents?: true

Size: 859 Bytes

Versions: 3

Compression:

Stored size: 859 Bytes

Contents

module Bogus
  class CreatesFakes
    class UnknownMode < RuntimeError; end

    extend Takes

    takes :copies_classes, :converts_name_to_class, :makes_ducks

    def create(name, opts = {}, &block)
      klass = self.klass(name, &block)
      duck = make_duck(klass)
      klass_copy = copies_classes.copy(duck)

      mode = opts.fetch(:as, :instance)

      case mode
      when :instance
        return klass_copy.new
      when :class
        return klass_copy
      else
        raise UnknownMode.new("Unknown fake creation mode: #{mode}. Allowed values are :instance, :class")
      end
    end

    protected

    def klass(name, &block)
      return block.call if block_given?
      converts_name_to_class.convert(name)
    end

    def make_duck(klass)
      return klass unless klass.is_a?(Array)
      makes_ducks.make(*klass)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

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