Class: Fabrication::Schematic::Definition
- Inherits:
-
Object
- Object
- Fabrication::Schematic::Definition
- 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
- #attributes ⇒ Object
-
#block ⇒ Object
Returns the value of attribute block.
- #callbacks ⇒ Object
-
#name ⇒ Object
Returns the value of attribute name.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #append_or_update_attribute(attribute_name, value, params = {}, &block) ⇒ Object
- #attribute(name) ⇒ Object
- #build(overrides = {}, &block) ⇒ Object
- #fabricate(overrides = {}, &block) ⇒ Object
- #generate_value(name, params) ⇒ Object
- #generator ⇒ Object
-
#initialize(name, options = {}, &block) ⇒ Definition
constructor
A new instance of Definition.
- #initialize_copy(original) ⇒ Object
- #klass ⇒ Object
- #merge(overrides = {}, &block) ⇒ Object
- #process_block(&block) ⇒ Object
- #sorted_attributes ⇒ Object
- #to_attributes(overrides = {}, &block) ⇒ Object
- #to_params(overrides = {}, &block) ⇒ Object
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, = {}, &block) self.name = name self. = self.block = block end |
Instance Attribute Details
#attributes ⇒ Object
42 43 44 45 |
# File 'lib/fabrication/schematic/definition.rb', line 42 def attributes load_body @attributes ||= [] end |
#block ⇒ Object
Returns the value of attribute block.
12 13 14 |
# File 'lib/fabrication/schematic/definition.rb', line 12 def block @block end |
#callbacks ⇒ Object
47 48 49 50 |
# File 'lib/fabrication/schematic/definition.rb', line 47 def callbacks load_body @callbacks ||= {} end |
#name ⇒ Object
Returns the value of attribute name.
12 13 14 |
# File 'lib/fabrication/schematic/definition.rb', line 12 def name @name end |
#options ⇒ Object
Returns the value of attribute options.
12 13 14 |
# File 'lib/fabrication/schematic/definition.rb', line 12 def @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 |
#generator ⇒ Object
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 |
#klass ⇒ Object
137 138 139 140 141 142 143 144 |
# File 'lib/fabrication/schematic/definition.rb', line 137 def klass @klass ||= Fabrication::Support.class_for( [:class_name] || parent&.klass || [: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_attributes ⇒ Object
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 |