Class: Fabrication::Schematic::Definition

Inherits:
Object
  • Object
show all
Defined in:
lib/fabrication/schematic/definition.rb

Constant Summary collapse

GENERATORS =
[
  Fabrication::Generator::ActiveRecord,
  Fabrication::Generator::DataMapper,
  Fabrication::Generator::Sequel,
  Fabrication::Generator::Mongoid,
  Fabrication::Generator::Base
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}, &block) ⇒ Definition

Returns a new instance of Definition.



14
15
16
17
18
# File 'lib/fabrication/schematic/definition.rb', line 14

def initialize(name, options = {}, &block)
  self.name = name
  self.options = options
  self.block = block
end

Instance Attribute Details

#attributesObject



42
43
44
45
# File 'lib/fabrication/schematic/definition.rb', line 42

def attributes
  load_body
  @attributes ||= []
end

#blockObject

Returns the value of attribute block.



12
13
14
# File 'lib/fabrication/schematic/definition.rb', line 12

def block
  @block
end

#callbacksObject



47
48
49
50
# File 'lib/fabrication/schematic/definition.rb', line 47

def callbacks
  load_body
  @callbacks ||= {}
end

#nameObject

Returns the value of attribute name.



12
13
14
# File 'lib/fabrication/schematic/definition.rb', line 12

def name
  @name
end

#optionsObject

Returns the value of attribute options.



12
13
14
# File 'lib/fabrication/schematic/definition.rb', line 12

def options
  @options
end

Instance Method Details

#append_or_update_attribute(attribute_name, value, params = {}, &block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/fabrication/schematic/definition.rb', line 28

def append_or_update_attribute(attribute_name, value, params = {}, &block)
  attribute = Fabrication::Schematic::Attribute.new(klass, attribute_name, value, params, &block)
  index = attributes.index { |a| a.name == attribute.name }

  if index
    attribute.transient! if attributes[index].transient?
    attributes[index] = attribute
  else
    attributes << attribute
  end
end

#attribute(name) ⇒ Object



24
25
26
# File 'lib/fabrication/schematic/definition.rb', line 24

def attribute(name)
  attributes.detect { |a| a.name == name }
end

#build(overrides = {}, &block) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/fabrication/schematic/definition.rb', line 60

def build(overrides = {}, &block)
  Fabrication.manager.prevent_recursion!
  if Fabrication.manager.to_params_stack.any?
    to_params(overrides, &block)
  else
    begin
      Fabrication.manager.build_stack << name
      merge(overrides, &block).instance_eval do
        generator.new(klass).build(sorted_attributes, callbacks)
      end
    ensure
      Fabrication.manager.build_stack.pop
    end
  end
end

#fabricate(overrides = {}, &block) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/fabrication/schematic/definition.rb', line 76

def fabricate(overrides = {}, &block)
  Fabrication.manager.prevent_recursion!
  if Fabrication.manager.build_stack.any?
    build(overrides, &block)
  elsif Fabrication.manager.to_params_stack.any?
    to_params(overrides, &block)
  else
    begin
      Fabrication.manager.create_stack << name
      merge(overrides, &block).instance_eval do
        generator.new(klass).create(sorted_attributes, callbacks)
      end
    ensure
      Fabrication.manager.create_stack.pop
    end
  end
end

#generate_value(name, params) ⇒ Object



119
120
121
122
123
124
125
126
# File 'lib/fabrication/schematic/definition.rb', line 119

def generate_value(name, params)
  if params[:count] || params[:rand]
    name = Fabrication::Support.singularize(name.to_s)
    proc { Fabricate.build(params[:fabricator] || name) }
  else
    proc { Fabricate(params[:fabricator] || name) }
  end
end

#generatorObject



52
53
54
# File 'lib/fabrication/schematic/definition.rb', line 52

def generator
  @generator ||= Fabrication::Config.generator_for(GENERATORS, klass)
end

#initialize_copy(original) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/fabrication/schematic/definition.rb', line 110

def initialize_copy(original)
  self.callbacks = {}
  original.callbacks.each do |type, callbacks|
    self.callbacks[type] = callbacks.clone
  end

  self.attributes = original.attributes.clone
end

#klassObject



137
138
139
140
141
142
143
144
# File 'lib/fabrication/schematic/definition.rb', line 137

def klass
  @klass ||= Fabrication::Support.class_for(
    options[:class_name] ||
      parent&.klass ||
      options[:from] ||
      name
  )
end

#merge(overrides = {}, &block) ⇒ Object



128
129
130
131
132
133
134
135
# File 'lib/fabrication/schematic/definition.rb', line 128

def merge(overrides = {}, &block)
  clone.tap do |definition|
    definition.process_block(&block)
    overrides.each do |name, value|
      definition.append_or_update_attribute(name.to_sym, value)
    end
  end
end

#process_block(&block) ⇒ Object



20
21
22
# File 'lib/fabrication/schematic/definition.rb', line 20

def process_block(&block)
  Fabrication::Schematic::Evaluator.new.process(self, &block) if block
end

#sorted_attributesObject



56
57
58
# File 'lib/fabrication/schematic/definition.rb', line 56

def sorted_attributes
  attributes.select(&:value_static?) + attributes.select(&:value_proc?)
end

#to_attributes(overrides = {}, &block) ⇒ Object



104
105
106
107
108
# File 'lib/fabrication/schematic/definition.rb', line 104

def to_attributes(overrides = {}, &block)
  merge(overrides, &block).instance_eval do
    generator.new(klass).to_hash(sorted_attributes, callbacks)
  end
end

#to_params(overrides = {}, &block) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/fabrication/schematic/definition.rb', line 94

def to_params(overrides = {}, &block)
  Fabrication.manager.prevent_recursion!
  Fabrication.manager.to_params_stack << name
  merge(overrides, &block).instance_eval do
    generator.new(klass).to_params(sorted_attributes)
  end
ensure
  Fabrication.manager.to_params_stack.pop
end