class Fabricators::Fabricator
Attributes
options[R]
proxy[R]
Public Class Methods
new(name, options={}, &block)
click to toggle source
# File lib/fabricators/fabricator.rb, line 6 def initialize(name, options={}, &block) @name = name @options = options @block = block @loaded = false Fetcher.new(name, options, &block) end
Public Instance Methods
attributes_for(options={})
click to toggle source
# File lib/fabricators/fabricator.rb, line 22 def attributes_for(options={}) {}.tap do |hash| iterate_attributes(options, proxy) do |name, value| hash[name] = value end end end
parent()
click to toggle source
# File lib/fabricators/fabricator.rb, line 14 def parent @parent ||= begin if @options[:parent] Fabricators.definitions.find(@options[:parent]) end end end
Protected Instance Methods
build_one(options={})
click to toggle source
# File lib/fabricators/fabricator.rb, line 69 def build_one(options={}) @options[:class].new.tap do |instance| trigger :before_build, instance iterate_attributes(options, instance) do |name, value| instance.send :"#{name}=", value end trigger :after_build, instance end end
create_one(options={})
click to toggle source
# File lib/fabricators/fabricator.rb, line 79 def create_one(options={}) build_one(options).tap do |instance| trigger :before_create, instance if instance.save Fabricators.records << instance end trigger :after_create, instance end end
iterate_attributes(options={}, context) { |name, value| ... }
click to toggle source
# File lib/fabricators/fabricator.rb, line 97 def iterate_attributes(options={}, context) proxy.attributes.merge(options).each do |name, value| case value when Proc context.instance_eval &value when Sequence value.generate context else value end.tap do |value| yield name, value end end end
load()
click to toggle source
# File lib/fabricators/fabricator.rb, line 56 def load unless @loaded if parent @options = parent.options.merge(@options) end unless @options[:class] ||= @name.to_s.classify.constantize raise "Class not found for fabricator #{@name}" end @proxy = Proxy.new(self, &@block) @loaded = true end end
trigger(name, instance)
click to toggle source
# File lib/fabricators/fabricator.rb, line 89 def trigger(name, instance) globals = (Fabricators.configuration.callbacks[name] || []) locals = (proxy.callbacks[name] || []) (globals + locals).each do |callback| callback.call instance end end