class Fabrication::Schematic::Definition
Constants
- GENERATORS
Attributes
Public Class Methods
Source
# File lib/fabrication/schematic/definition.rb, line 13 def initialize(name, options = {}, &block) self.name = name self.options = options self.block = block end
Public Instance Methods
Source
# File lib/fabrication/schematic/definition.rb, line 27 def append_or_update_attribute(attribute_name, value, params = {}, &) attribute = Fabrication::Schematic::Attribute.new(klass, attribute_name, value, params, &) index = attributes.index { |a| a.name == attribute.name } if index attribute.transient! if attributes[index].transient? attributes[index] = attribute else attributes << attribute end end
Source
# File lib/fabrication/schematic/definition.rb, line 23 def attribute(name) attributes.detect { |a| a.name == name } end
Source
# File lib/fabrication/schematic/definition.rb, line 41 def attributes load_body @attributes ||= [] end
Source
# File lib/fabrication/schematic/definition.rb, line 59 def build(overrides = {}, &) Fabrication.manager.prevent_recursion! if Fabrication.manager.to_params_stack.any? to_params(overrides, &) else begin Fabrication.manager.build_stack << name merge(overrides, &).instance_eval do generator.new(klass).build(sorted_attributes, callbacks) end ensure Fabrication.manager.build_stack.pop end end end
Source
# File lib/fabrication/schematic/definition.rb, line 46 def callbacks load_body @callbacks ||= {} end
Source
# File lib/fabrication/schematic/definition.rb, line 75 def fabricate(overrides = {}, &) Fabrication.manager.prevent_recursion! if Fabrication.manager.build_stack.any? build(overrides, &) elsif Fabrication.manager.to_params_stack.any? to_params(overrides, &) else begin Fabrication.manager.create_stack << name merge(overrides, &).instance_eval do generator.new(klass).create(sorted_attributes, callbacks) end ensure Fabrication.manager.create_stack.pop end end end
Source
# File lib/fabrication/schematic/definition.rb, line 118 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
Source
# File lib/fabrication/schematic/definition.rb, line 51 def generator @generator ||= Fabrication::Config.generator_for(GENERATORS, klass) end
Source
# File lib/fabrication/schematic/definition.rb, line 109 def initialize_copy(original) self.callbacks = {} original.callbacks.each do |type, callbacks| self.callbacks[type] = callbacks.clone end self.attributes = original.attributes.clone end
Source
# File lib/fabrication/schematic/definition.rb, line 136 def klass @klass ||= Fabrication::Support.class_for( options[:class_name] || parent&.klass || options[:from] || name ) end
Source
# File lib/fabrication/schematic/definition.rb, line 127 def merge(overrides = {}, &) clone.tap do |definition| definition.process_block(&) overrides.each do |name, value| definition.append_or_update_attribute(name.to_sym, value) end end end
Source
# File lib/fabrication/schematic/definition.rb, line 19 def process_block(&block) Fabrication::Schematic::Evaluator.new.process(self, &block) if block end
Source
# File lib/fabrication/schematic/definition.rb, line 55 def sorted_attributes attributes.select(&:value_static?) + attributes.select(&:value_proc?) end
Source
# File lib/fabrication/schematic/definition.rb, line 103 def to_attributes(overrides = {}, &) merge(overrides, &).instance_eval do generator.new(klass).to_hash(sorted_attributes, callbacks) end end
Source
# File lib/fabrication/schematic/definition.rb, line 93 def to_params(overrides = {}, &) Fabrication.manager.prevent_recursion! Fabrication.manager.to_params_stack << name merge(overrides, &).instance_eval do generator.new(klass).to_params(sorted_attributes) end ensure Fabrication.manager.to_params_stack.pop end
Protected Instance Methods
Source
# File lib/fabrication/schematic/definition.rb, line 151 def load_body return if loaded? @loaded = true if parent merge_result = parent.merge(&block) @attributes = merge_result.attributes @callbacks = merge_result.callbacks else process_block(&block) end end
Source
# File lib/fabrication/schematic/definition.rb, line 147 def loaded? !!(@loaded ||= nil) end
Source
# File lib/fabrication/schematic/definition.rb, line 165 def parent @parent ||= Fabrication.manager[options[:from].to_s] if options[:from] end