Class: FakerMaker::Factory

Inherits:
Object
  • Object
show all
Includes:
Auditable
Defined in:
lib/faker_maker/factory.rb

Overview

Factories construct instances of a fake

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Auditable

#audit

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, 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, options[:naming]
                     end
  @attributes = []
  @klass = nil
  @parent = options[:parent]
end

Instance Attribute Details

#chaos_selected_attributesObject (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_nameObject (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

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/faker_maker/factory.rb', line 8

def name
  @name
end

#parentObject (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



81
82
83
# File 'lib/faker_maker/factory.rb', line 81

def as_json(*_args)
  build.as_json
end

#assembleObject



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



108
109
110
111
# File 'lib/faker_maker/factory.rb', line 108

def attribute_names( collection = [] )
  collection |= FakerMaker[parent].attribute_names( collection ) if parent?
  collection | @attributes.map( &:name )
end

#attributes(collection = []) ⇒ Object



113
114
115
116
# File 'lib/faker_maker/factory.rb', line 113

def attributes( collection = [] )
  collection |= FakerMaker[parent].attributes( collection ) if parent?
  collection | @attributes
end

#build(attributes: {}, chaos: false, **kwargs) {|instance| ... } ⇒ Object

Yields:



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 )

  assert_chaos_options 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



118
119
120
# File 'lib/faker_maker/factory.rb', line 118

def find_attribute( name = '' )
  attributes.filter { |a| [a.name, a.translation, @naming_strategy&.name(a.name)].include? name }.first
end

#instanceObject



41
42
43
# File 'lib/faker_maker/factory.rb', line 41

def instance
  @instance ||= instantiate
end

#json_key_mapObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/faker_maker/factory.rb', line 89

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

Returns:

  • (Boolean)


85
86
87
# File 'lib/faker_maker/factory.rb', line 85

def parent?
  !@parent.nil?
end

#parent_classObject



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