class Fabricators::Proxy

Public Class Methods

new(fabricator, &block) click to toggle source
# File lib/fabricators/proxy.rb, line 5
def initialize(fabricator, &block)
  @fabricator = fabricator
  @attributes = []
  @sequences = {}
  instance_eval &block
end

Public Instance Methods

attributes() click to toggle source
# File lib/fabricators/proxy.rb, line 12
def attributes
  {}.tap do |hash|
    if @fabricator.parent
      hash.merge! @fabricator.parent.proxy.attributes
    end
    @attributes.each do |name|
      hash[name] = send(name)
    end
  end
end
method_missing(name, *args, &block) click to toggle source
# File lib/fabricators/proxy.rb, line 31
def method_missing(name, *args, &block)
  unless name == :fabricator
    options = args.extract_options!
    strategy = options.delete(:strategy) || :build
    if block_given?
      logic = block
    elsif fabricator = Fabricators.definitions.find(name) rescue nil
      logic = -> { fabricator.send(strategy, options) }
    elsif fabricator = Fabricators.definitions.find(name.to_s.singularize.to_sym) rescue nil
      logic = -> { fabricator.send(strategy, (args.first || 1), options) }
    elsif args.any?
      logic = -> { args.first }
    end
    if defined? logic
      @attributes.send (block_given? ? :push : :unshift), name
      class_eval { define_method(name, logic) }
    end
  end
end
sequence(name, &block) click to toggle source
# File lib/fabricators/proxy.rb, line 23
def sequence(name, &block)
  @attributes << name
  @sequences[name] = Sequence.new(&block)
  class_eval do
    define_method(name) { @sequences[name] }
  end
end