Class: Fabricators::Proxy
- Inherits:
-
Object
- Object
- Fabricators::Proxy
- Includes:
- Callbacks
- Defined in:
- lib/fabricators/proxy.rb
Instance Method Summary collapse
- #attributes ⇒ Object
-
#initialize(fabricator, &block) ⇒ Proxy
constructor
A new instance of Proxy.
- #method_missing(name, *args, &block) ⇒ Object
Methods included from Callbacks
Constructor Details
#initialize(fabricator, &block) ⇒ Proxy
Returns a new instance of Proxy.
5 6 7 8 9 |
# File 'lib/fabricators/proxy.rb', line 5 def initialize(fabricator, &block) @fabricator = fabricator @attributes = [] instance_eval &block end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/fabricators/proxy.rb', line 22 def method_missing(name, *args, &block) unless name == :fabricator = args. strategy = .delete(:strategy) || :build if block_given? logic = block elsif fabricator = Fabricators.definitions.find(name, :fabricator) rescue nil logic = -> { fabricator.send(strategy, ) } elsif fabricator = Fabricators.definitions.find(name.to_s.singularize.to_sym, :fabricator) rescue nil logic = -> { fabricator.send(strategy, (args.first || 1), ) } elsif generator = Fabricators.definitions.find(name, :generator) rescue nil logic = -> { generator.generate } 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 |
Instance Method Details
#attributes ⇒ Object
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/fabricators/proxy.rb', line 11 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 |