class Traver::ObjectCreator

Attributes

after_create[RW]
cache[R]
factory[R]
factory_definer[R]
factory_name[R]
nesting[R]
object[R]
params[R]
sequencer[R]

Public Class Methods

create_object(factory_name, params, factory_definer, sequencer, cache = {}, nesting = 1) click to toggle source
# File lib/traver/object_creator.rb, line 16
def self.create_object(factory_name, params, factory_definer, sequencer, cache = {}, nesting = 1)
  creator = new(factory_name, params, factory_definer, sequencer, cache, nesting)
  creator.create_object
  
  creator.object
end
new(factory_name, params, factory_definer, sequencer, cache = {}, nesting = 1) click to toggle source
# File lib/traver/object_creator.rb, line 23
def initialize(factory_name, params, factory_definer, sequencer, cache = {}, nesting = 1)
  @factory_name    = factory_name
  @params          = params
  @factory_definer = factory_definer
  @sequencer       = sequencer
  @cache           = cache
  @nesting         = nesting
end

Public Instance Methods

create_object() click to toggle source
# File lib/traver/object_creator.rb, line 32
def create_object
  obtain_factory
  
  if obtain_object_from_cache?
    obtain_object_from_cache
  else
    # puts "#{'-' * nesting} #{factory_name}<br/>"
    
    change_ref_to_be_empty_hash
    merge_factory_params
    merge_default_params
    instantiate_object
    set_nested_objects
    set_attributes
    persist_object
    cache_object
    set_has_one_objects
    set_nested_collections
    call_after_create_hook
  end
end

Private Instance Methods

cache_object() click to toggle source
# File lib/traver/object_creator.rb, line 231
def cache_object
  cache[factory.root_name] = object
end
call_after_create_hook() click to toggle source

Hooks

# File lib/traver/object_creator.rb, line 238
def call_after_create_hook
  after_create.call(self) if after_create
end
change_ref_to_be_empty_hash() click to toggle source
# File lib/traver/object_creator.rb, line 69
def change_ref_to_be_empty_hash
  @params = {} if params == :__ref__
end
create_nested_collection(factory_name, params_array) click to toggle source
# File lib/traver/object_creator.rb, line 164
def create_nested_collection(factory_name, params_array)
  if params_array.is_a?(Integer)
    params_array = [{}] * params_array
    
    params_array.map do |params|
      create_nested_object(factory_name, params)
    end
  elsif params_array.first == :__ref__
    params_array.map do |_|
      create_nested_object(factory_name, :__ref__)
    end
  elsif params_array.first.is_a?(Symbol)
    params_array.map do |factory_name|
      create_nested_object(factory_name, {})
    end
  else
    if params_array.first.is_a?(Integer)
      num, hash_or_symbol = params_array
    
      if hash_or_symbol.nil?
        params_array = [{}] * num
      elsif (hash = hash_or_symbol).is_a?(Hash)
        params_array = [hash] * num
      elsif hash_or_symbol.is_a?(Symbol)
        factory_name = hash_or_symbol
      
        params_array = [{}] * num
      else
        raise "Wrong collection params #{params_array}"
      end
      
      params_array.map do |params|
        create_nested_object(factory_name, params)
      end
    elsif params_array.first.is_a?(Hash)
      params_array.map do |params|
        create_nested_object(factory_name, params)
      end
    else
      params_array
    end
  end
end
create_nested_object(factory_name, params) click to toggle source
# File lib/traver/object_creator.rb, line 133
def create_nested_object(factory_name, params)
  object_creator = ObjectCreator.new(factory_name, params, factory_definer, sequencer, cache, nesting + 1)
  object_creator.after_create = after_create
  object_creator.create_object
  
  object_creator.object
end
instantiate_object() click to toggle source
# File lib/traver/object_creator.rb, line 81
def instantiate_object
  @object = factory.object_class.new
end
merge_default_params() click to toggle source
# File lib/traver/object_creator.rb, line 77
def merge_default_params
  @params = default_params_creator.default_params(factory.object_class).merge(params)
end
merge_factory_params() click to toggle source
# File lib/traver/object_creator.rb, line 73
def merge_factory_params
  @params = factory.inherited_params.merge(params)
end
object_is_active_record?() click to toggle source
# File lib/traver/object_creator.rb, line 220
def object_is_active_record?
  factory.object_class < ActiveRecord::Base
end
obtain_factory() click to toggle source
# File lib/traver/object_creator.rb, line 57
def obtain_factory
  @factory = factory_definer.factory_by_name(factory_name)
end
obtain_object_from_cache() click to toggle source
# File lib/traver/object_creator.rb, line 65
def obtain_object_from_cache
  @object = cache[factory.root_name]
end
obtain_object_from_cache?() click to toggle source
# File lib/traver/object_creator.rb, line 61
def obtain_object_from_cache?
  params == :__ref__ && cache.has_key?(factory.root_name)
end
persist_object() click to toggle source

Persist Object

# File lib/traver/object_creator.rb, line 227
def persist_object
  object_persister.persist_object(object)
end
set_attribute(attribute, value) click to toggle source
# File lib/traver/object_creator.rb, line 93
def set_attribute(attribute, value)
  if value.is_a?(Proc)
    value = 
      if value.arity == 0
        value.call
      elsif value.arity == 1
        value.call(object)
      else
        raise "Parameter block can have either 0 or 1 argument"
      end
  elsif value.is_a?(String)
    if sequencer.value_has_sequence?(value)
      value = sequencer.interpolate_sequence(attribute, value)
    end
  end
  
  object.public_send("#{attribute}=", value)
end
set_attributes() click to toggle source

Attributes

# File lib/traver/object_creator.rb, line 87
def set_attributes
  attributes_resolver.select_attributes_params(params, factory.object_class).each do |name, value|
    set_attribute(name, value)
  end
end
set_has_one_objects() click to toggle source

Has one objects

# File lib/traver/object_creator.rb, line 144
def set_has_one_objects
  attributes_resolver.select_has_one_objects_params(params, factory.object_class).each do |name, value|
    set_nested_object(name, value)
  end
end
set_nested_collection(collection_name, params_array) click to toggle source
# File lib/traver/object_creator.rb, line 158
def set_nested_collection(collection_name, params_array)
  factory_name = collection_name.to_s.singularize.to_sym
  
  set_attribute(collection_name, create_nested_collection(factory_name, params_array))
end
set_nested_collections() click to toggle source

Nested Collections

# File lib/traver/object_creator.rb, line 152
def set_nested_collections
  attributes_resolver.select_collections_params(object, factory, params).each do |collection_name, params_array|
    set_nested_collection(collection_name, params_array)
  end
end
set_nested_object(name, value) click to toggle source
# File lib/traver/object_creator.rb, line 120
def set_nested_object(name, value)
  if value.is_a?(Integer)
    set_attribute(name, create_nested_object(name, {}))
  elsif value.is_a?(Hash) || value == :__ref__
    set_attribute(name, create_nested_object(name, value))
  elsif value.is_a?(Symbol)
    set_attribute(name, create_nested_object(value, {}))
  else
    set_attribute(name, value)
    cache[name] = value
  end
end
set_nested_objects() click to toggle source

Nested Objects

# File lib/traver/object_creator.rb, line 114
def set_nested_objects
  attributes_resolver.select_objects_params(params, factory.object_class).each do |name, value|
    set_nested_object(name, value)
  end
end
settings() click to toggle source

Settings

# File lib/traver/object_creator.rb, line 211
def settings
  @settings ||=
    if object_is_active_record?
      ActiveRecordSettings.new
    else
      PoroSettings.new
    end
end