class FakerMaker::Factory
Factories construct instances of a fake
Attributes
class_name[R]
name[R]
parent[R]
Public Class Methods
new( name, options = {} )
click to toggle source
# File lib/faker_maker/factory.rb, line 9 def initialize( name, options = {} ) assert_valid_options options @name = name.respond_to?(:to_sym) ? name.to_sym : name.to_s.underscore.to_sym @class_name = (options[:class] || @name).to_s.camelcase @naming_strategy = case options[:naming] when :json FakerMaker::Naming::JSON when :json_capitalized, :json_capitalised FakerMaker::Naming::JSONCapitalized when nil nil else raise FakerMaker::NoSuchAttributeNamingStrategy, opttions[:naming] end @attributes = [] @klass = nil @parent = options[:parent] end
Public Instance Methods
as_json(*_args)
click to toggle source
# File lib/faker_maker/factory.rb, line 68 def as_json(*_args) build.as_json end
assemble()
click to toggle source
# File lib/faker_maker/factory.rb, line 54 def assemble if @klass.nil? @klass = Class.new parent_class Object.const_set @class_name, @klass attach_attributes_to_class attach_json_overrides_to_class end @klass end
attach_attribute( attribute )
click to toggle source
# File lib/faker_maker/factory.rb, line 36 def attach_attribute( attribute ) @attributes << attribute end
attribute_names( collection = [] )
click to toggle source
# File lib/faker_maker/factory.rb, line 95 def attribute_names( collection = [] ) collection |= FakerMaker[parent].attribute_names( collection ) if parent? collection | @attributes.map( &:name ) end
attributes( collection = [] )
click to toggle source
# File lib/faker_maker/factory.rb, line 100 def attributes( collection = [] ) collection |= FakerMaker[parent].attributes( collection ) if parent? collection | @attributes end
build( attributes = {} ) { |instance| ... }
click to toggle source
# File lib/faker_maker/factory.rb, line 44 def build( attributes = {} ) @instance = nil before_build if respond_to? :before_build assert_only_known_attributes_for_override( attributes ) populate_instance instance, attributes yield instance if block_given? after_build if respond_to? :after_build instance end
find_attribute( name = '' )
click to toggle source
# File lib/faker_maker/factory.rb, line 105 def find_attribute( name = '' ) attributes.filter { |a| [a.name, a.translation, @naming_strategy&.name(name)].include? name }.first end
instance()
click to toggle source
# File lib/faker_maker/factory.rb, line 40 def instance @instance ||= instantiate end
json_key_map()
click to toggle source
# File lib/faker_maker/factory.rb, line 76 def json_key_map unless @json_key_map @json_key_map = {}.with_indifferent_access @json_key_map.merge!( FakerMaker[parent].json_key_map ) if parent? attributes.each_with_object( @json_key_map ) do |attr, map| key = if attr.translation? attr.translation elsif @naming_strategy @naming_strategy.name(attr.name) else attr.name end map[attr.name] = key end end @json_key_map end
parent?()
click to toggle source
# File lib/faker_maker/factory.rb, line 72 def parent? !@parent.nil? end
parent_class()
click to toggle source
# File lib/faker_maker/factory.rb, line 28 def parent_class if @parent Object.const_get( FakerMaker[@parent].class_name ) else Object end end
to_json(*_args)
click to toggle source
# File lib/faker_maker/factory.rb, line 64 def to_json(*_args) build.to_json end
Protected Instance Methods
populate_instance( instance, attr_override_values )
click to toggle source
# File lib/faker_maker/factory.rb, line 111 def populate_instance( instance, attr_override_values ) FakerMaker[parent].populate_instance instance, attr_override_values if parent? @attributes.each do |attr| value = value_for_attribute( instance, attr, attr_override_values ) instance.send "#{attr.name}=", value end instance.instance_variable_set( :@fm_factory, self ) end
Private Instance Methods
assert_only_known_attributes_for_override( attr_override_values )
click to toggle source
# File lib/faker_maker/factory.rb, line 122 def assert_only_known_attributes_for_override( attr_override_values ) unknown_attrs = attr_override_values.keys - attribute_names issue = "Can't build an instance of '#{class_name}' " \ "setting '#{unknown_attrs.join( ', ' )}', no such attribute(s)" raise FakerMaker::NoSuchAttributeError, issue unless unknown_attrs.empty? end
assert_valid_options( options )
click to toggle source
# File lib/faker_maker/factory.rb, line 162 def assert_valid_options( options ) options.assert_valid_keys :class, :parent, :naming end
attach_attributes_to_class()
click to toggle source
# File lib/faker_maker/factory.rb, line 147 def attach_attributes_to_class @attributes.each do |attr| @klass.send( :attr_accessor, attr.name ) end @klass.send( :attr_reader, :fm_factory ) end
attach_json_overrides_to_class()
click to toggle source
Calls superclass method
# File lib/faker_maker/factory.rb, line 154 def attach_json_overrides_to_class @klass.define_method :as_json do |options = {}| super( options.merge( except: 'fm_factory' ) ) .transform_keys { |key| @fm_factory.json_key_map[key] || key } .filter { |key, value| !@fm_factory.find_attribute(key)&.omit?( value ) } end end
attribute_hash_overridden_value?( attr, attr_override_values )
click to toggle source
# File lib/faker_maker/factory.rb, line 129 def attribute_hash_overridden_value?( attr, attr_override_values ) attr_override_values.keys.include?( attr.name ) end
instantiate()
click to toggle source
# File lib/faker_maker/factory.rb, line 143 def instantiate assemble.new end
value_for_attribute( instance, attr, attr_override_values )
click to toggle source
# File lib/faker_maker/factory.rb, line 133 def value_for_attribute( instance, attr, attr_override_values ) if attribute_hash_overridden_value?( attr, attr_override_values ) attr_override_values[attr.name] elsif attr.array? [].tap { |a| attr.cardinality.times { a << instance.instance_eval(&attr.block) } } else instance.instance_eval(&attr.block) end end