Class: FakerMaker::Factory
- Inherits:
-
Object
- Object
- FakerMaker::Factory
- Includes:
- Auditable
- Defined in:
- lib/faker_maker/factory.rb
Overview
Factories construct instances of a fake
Instance Attribute Summary collapse
-
#chaos_selected_attributes ⇒ Object
readonly
Returns the value of attribute chaos_selected_attributes.
-
#class_name ⇒ Object
readonly
Returns the value of attribute class_name.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Instance Method Summary collapse
- #as_json(*_args) ⇒ Object (also: #to_h)
- #assemble ⇒ Object
- #attach_attribute(attribute) ⇒ Object
- #attribute_names(collection = []) ⇒ Object
- #attributes(collection = []) ⇒ Object
- #build(attributes: {}, chaos: false, **kwargs) {|instance| ... } ⇒ Object
- #find_attribute(name = '') ⇒ Object
-
#initialize(name, options = {}) ⇒ Factory
constructor
A new instance of Factory.
- #instance ⇒ Object
- #json_key_map ⇒ Object
- #parent? ⇒ Boolean
- #parent_class ⇒ Object
- #to_json(*_args) ⇒ Object
Methods included from Auditable
Constructor Details
#initialize(name, options = {}) ⇒ Factory
Returns a new instance of Factory.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/faker_maker/factory.rb', line 10 def initialize( name, = {} ) @name = name.respond_to?(:to_sym) ? name.to_sym : name.to_s.underscore.to_sym @class_name = ([:class] || @name).to_s.camelcase @naming_strategy = case [:naming] when :json FakerMaker::Naming::JSON when :json_capitalized, :json_capitalised FakerMaker::Naming::JSONCapitalized when nil nil else raise FakerMaker::NoSuchAttributeNamingStrategy, [:naming] end @attributes = [] @klass = nil @parent = [:parent] end |
Instance Attribute Details
#chaos_selected_attributes ⇒ Object (readonly)
Returns the value of attribute chaos_selected_attributes.
8 9 10 |
# File 'lib/faker_maker/factory.rb', line 8 def chaos_selected_attributes @chaos_selected_attributes end |
#class_name ⇒ Object (readonly)
Returns the value of attribute class_name.
8 9 10 |
# File 'lib/faker_maker/factory.rb', line 8 def class_name @class_name end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/faker_maker/factory.rb', line 8 def name @name end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
8 9 10 |
# File 'lib/faker_maker/factory.rb', line 8 def parent @parent end |
Instance Method Details
#as_json(*_args) ⇒ Object Also known as: to_h
81 82 83 |
# File 'lib/faker_maker/factory.rb', line 81 def as_json(*_args) build.as_json end |
#assemble ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/faker_maker/factory.rb', line 67 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) ⇒ Object
37 38 39 |
# File 'lib/faker_maker/factory.rb', line 37 def attach_attribute( attribute ) @attributes << attribute end |
#attribute_names(collection = []) ⇒ Object
109 110 111 112 |
# File 'lib/faker_maker/factory.rb', line 109 def attribute_names( collection = [] ) collection |= FakerMaker[parent].attribute_names( collection ) if parent? collection | @attributes.map( &:name ) end |
#attributes(collection = []) ⇒ Object
114 115 116 117 |
# File 'lib/faker_maker/factory.rb', line 114 def attributes( collection = [] ) collection |= FakerMaker[parent].attributes( collection ) if parent? collection | @attributes end |
#build(attributes: {}, chaos: false, **kwargs) {|instance| ... } ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/faker_maker/factory.rb', line 45 def build( attributes: {}, chaos: false, **kwargs ) if kwargs.present? validate_deprecated_build(kwargs) attributes = kwargs end @instance = nil before_build if respond_to? :before_build assert_only_known_attributes_for_override( attributes ) chaos if chaos optional_attributes required_attributes populate_instance instance, attributes, chaos yield instance if block_given? after_build if respond_to? :after_build audit(@instance) if FakerMaker.configuration.audit? instance end |
#find_attribute(name = '') ⇒ Object
119 120 121 |
# File 'lib/faker_maker/factory.rb', line 119 def find_attribute( name = '' ) attributes.filter { |a| [a.name, a.translation, @naming_strategy&.name(a.name)].include? name }.first end |
#instance ⇒ Object
41 42 43 |
# File 'lib/faker_maker/factory.rb', line 41 def instance @instance ||= instantiate end |
#json_key_map ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/faker_maker/factory.rb', line 90 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? ⇒ Boolean
86 87 88 |
# File 'lib/faker_maker/factory.rb', line 86 def parent? !@parent.nil? end |
#parent_class ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/faker_maker/factory.rb', line 29 def parent_class if @parent Object.const_get( FakerMaker[@parent].class_name ) else Object end end |
#to_json(*_args) ⇒ Object
77 78 79 |
# File 'lib/faker_maker/factory.rb', line 77 def to_json(*_args) build.to_json end |