Sha256: 2c36be5179cdc5e214d59d95e907b6288be0be9f7551a0af49e1d6748fcd4065
Contents?: true
Size: 1.39 KB
Versions: 3
Compression:
Stored size: 1.39 KB
Contents
module Fabricators class Proxy include Callbacks def initialize(fabricator, &block) @fabricator = fabricator @attributes = [] @sequences = {} instance_eval &block end 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 def sequence(name, &block) @attributes << name @sequences[name] = Sequence.new(&block) class_eval do define_method(name) { @sequences[name] } end end 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 end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
fabricators-0.1.2 | lib/fabricators/proxy.rb |
fabricators-0.1.1 | lib/fabricators/proxy.rb |
fabricators-0.1.0 | lib/fabricators/proxy.rb |