class Fabrication::Generator::Base

Attributes

_instance[RW]
resolved_class[RW]

Public Class Methods

new(resolved_class) click to toggle source
# File lib/fabrication/generator/base.rb, line 91
def initialize(resolved_class)
  self.resolved_class = resolved_class
end
supports?(_resolved_class) click to toggle source
# File lib/fabrication/generator/base.rb, line 4
def self.supports?(_resolved_class)
  true
end

Public Instance Methods

_klass() click to toggle 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
build(attributes = [], callbacks = {}) click to toggle 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
build_instance() click to toggle source
# File lib/fabrication/generator/base.rb, line 80
def build_instance
  self._instance = resolved_class.new
  set_attributes
end
build_instance_with_constructor_override(callback) click to toggle 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
build_instance_with_init_callback(callback) click to toggle 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
create(attributes = [], callbacks = {}) click to toggle 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
execute_callbacks(callbacks) click to toggle source
# File lib/fabrication/generator/base.rb, line 48
def execute_callbacks(callbacks)
  callbacks&.each { |callback| _instance.instance_exec(_instance, _transient_attributes, &callback) }
end
execute_deprecated_callbacks(callbacks, callback_type, replacement_callback) click to toggle 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
method_missing(method_name, *args, &block) click to toggle source
Calls superclass method
# File lib/fabrication/generator/base.rb, line 99
def method_missing(method_name, *args, &block)
  _attributes.fetch(method_name) { super }
end
respond_to_missing?(method_name, _include_private = false) click to toggle source
# File lib/fabrication/generator/base.rb, line 95
def respond_to_missing?(method_name, _include_private = false)
  _attributes.key?(method_name)
end
set_attributes() click to toggle source
# File lib/fabrication/generator/base.rb, line 85
def set_attributes
  _attributes.each do |k, v|
    _instance.send("#{k}=", v)
  end
end
to_hash(attributes = [], _callbacks = []) click to toggle 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
to_params(attributes = []) click to toggle 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

_attributes() click to toggle source
# File lib/fabrication/generator/base.rb, line 115
def _attributes
  @_attributes ||= {}
end
_transient_attributes() click to toggle source
# File lib/fabrication/generator/base.rb, line 119
def _transient_attributes
  @_transient_attributes ||= {}
end
persist() click to toggle source
# File lib/fabrication/generator/base.rb, line 123
def persist
  _instance.save! if _instance.respond_to?(:save!)
end
process_attributes(attributes) click to toggle 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