class Fabrication::Generator::Base
Attributes
Public Class Methods
Source
# File lib/fabrication/generator/base.rb, line 91 def initialize(resolved_class) self.resolved_class = resolved_class end
Source
# File lib/fabrication/generator/base.rb, line 4 def self.supports?(_resolved_class) true end
Public Instance Methods
Source
# File lib/fabrication/generator/base.rb, line 103 def _klass Fabrication::Support.log_deprecation( 'The `_klass` method in fabricator definitions has been replaced by `resolved_class`' ) resolved_class end
Source
# File lib/fabrication/generator/base.rb, line 8 def build(attributes = [], callbacks = {}) process_attributes(attributes) if callbacks[:initialize_with] build_instance_with_constructor_override(callbacks[:initialize_with]) elsif callbacks[:on_init] Fabrication::Support.log_deprecation( 'The on_init callback has been replaced by initialize_with. Please see the documentation for usage' ) build_instance_with_init_callback(callbacks[:on_init]) else build_instance end execute_callbacks(callbacks[:after_build]) _instance end
Source
# File lib/fabrication/generator/base.rb, line 80 def build_instance self._instance = resolved_class.new set_attributes end
Source
# File lib/fabrication/generator/base.rb, line 70 def build_instance_with_constructor_override(callback) self._instance = instance_exec(_transient_attributes, &callback) set_attributes end
Source
# File lib/fabrication/generator/base.rb, line 75 def build_instance_with_init_callback(callback) self._instance = resolved_class.new(*callback.call(_transient_attributes)) set_attributes end
Source
# File lib/fabrication/generator/base.rb, line 25 def create(attributes = [], callbacks = {}) build(attributes, callbacks) execute_deprecated_callbacks(callbacks, :before_validation, :before_create) execute_deprecated_callbacks(callbacks, :after_validation, :before_create) execute_deprecated_callbacks(callbacks, :before_save, :before_create) execute_callbacks(callbacks[:before_create]) persist execute_callbacks(callbacks[:after_create]) execute_deprecated_callbacks(callbacks, :after_save, :after_create) _instance end
Source
# File lib/fabrication/generator/base.rb, line 48 def execute_callbacks(callbacks) callbacks&.each { |callback| _instance.instance_exec(_instance, _transient_attributes, &callback) } end
Source
# File lib/fabrication/generator/base.rb, line 37 def execute_deprecated_callbacks(callbacks, callback_type, replacement_callback) if callbacks[callback_type] Fabrication::Support.log_deprecation( "Using #{callback_type} is deprecated but you can replace it " \ "with #{replacement_callback} with the same result." ) end execute_callbacks(callbacks[callback_type]) end
Source
# File lib/fabrication/generator/base.rb, line 99 def method_missing(method_name, *args, &) _attributes.fetch(method_name) { super } end
Calls superclass method
Source
# File lib/fabrication/generator/base.rb, line 95 def respond_to_missing?(method_name, _include_private = false) _attributes.key?(method_name) end
Source
# File lib/fabrication/generator/base.rb, line 85 def set_attributes _attributes.each do |k, v| _instance.send(:"#{k}=", v) end end
Source
# File lib/fabrication/generator/base.rb, line 57 def to_hash(attributes = [], _callbacks = []) process_attributes(attributes) Fabrication::Support.hash_class.new.tap do |hash| _attributes.map do |name, value| if value.respond_to?(:id) hash["#{name}_id"] = value.id else hash[name] = value end end end end
Source
# File lib/fabrication/generator/base.rb, line 52 def to_params(attributes = []) process_attributes(attributes) _attributes.respond_to?(:with_indifferent_access) ? _attributes.with_indifferent_access : _attributes end
Protected Instance Methods
Source
# File lib/fabrication/generator/base.rb, line 115 def _attributes @_attributes ||= {} end
Source
# File lib/fabrication/generator/base.rb, line 119 def _transient_attributes @_transient_attributes ||= {} end
Source
# File lib/fabrication/generator/base.rb, line 123 def persist _instance.save! if _instance.respond_to?(:save!) end
Source
# File lib/fabrication/generator/base.rb, line 127 def process_attributes(attributes) attributes.each do |attribute| _attributes[attribute.name] = attribute.processed_value(_attributes) _transient_attributes[attribute.name] = _attributes[attribute.name] if attribute.transient? end _attributes.reject! { |k| _transient_attributes.key?(k) } end