Class: ObjectMomma::Child

Inherits:
Object
  • Object
show all
Defined in:
lib/object_momma/child.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object_type, hash, actualize_strategy) ⇒ Child

Returns a new instance of Child.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/object_momma/child.rb', line 29

def initialize(object_type, hash, actualize_strategy)
  unless ACTUALIZE_STRATEGIES.include?(actualize_strategy)
    raise ArgumentError, "Invalid actualize strategy "\
      "`#{actualize_strategy}'; valid values are "\
      "#{ACTUALIZE_STRATEGIES.map(&:to_s).join(', ')}"
  end

  @actualize_strategy = actualize_strategy
  @builder            = ObjectMomma.builder_for(object_type).new(self)
  @object_type        = object_type

  builder.build_child_from_hash(hash) do |sibling_object_type, sibling_id|
    self.class.new(sibling_object_type, sibling_id, @actualize_strategy)
  end
end

Instance Attribute Details

#actualize_strategyObject (readonly)

Returns the value of attribute actualize_strategy.



6
7
8
# File 'lib/object_momma/child.rb', line 6

def actualize_strategy
  @actualize_strategy
end

#builderObject (readonly)

Returns the value of attribute builder.



6
7
8
# File 'lib/object_momma/child.rb', line 6

def builder
  @builder
end

#child_idObject Also known as: to_s

Returns the value of attribute child_id.



5
6
7
# File 'lib/object_momma/child.rb', line 5

def child_id
  @child_id
end

#object_typeObject (readonly)

Returns the value of attribute object_type.



6
7
8
# File 'lib/object_momma/child.rb', line 6

def object_type
  @object_type
end

Class Method Details

.new(object_type, string_or_hash, *args) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/object_momma/child.rb', line 52

def new(object_type, string_or_hash, *args)
  if string_or_hash.is_a?(String)
    hash = Builder.string_to_hash(object_type, string_or_hash)
  elsif string_or_hash.is_a?(Hash)
    hash = string_or_hash
  else
    raise ArgumentError, "Must instantiate a Child with a String or a "\
      "Hash, not a #{string_or_hash.class.name}"
  end

  original_new(object_type, hash, *args)
end

.original_newObject



50
# File 'lib/object_momma/child.rb', line 50

alias_method :original_new, :new

Instance Method Details

#attributes_for_childObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/object_momma/child.rb', line 9

def attributes_for_child
  return {} unless ObjectMomma.use_serialized_attributes

  # Pluralize
  if object_type.to_s.chars.to_a.last == "s"
    file_name = object_type
  else
    file_name = "#{object_type}s"
  end

  path = File.join(ObjectMomma.serialized_attributes_path, "#{file_name}.yml")

  if File.size?(path)
    attributes_by_child_id = YAML::load(ERB.new(File.read(path)).result)
    return recursively_symbolize_hash(attributes_by_child_id.fetch(child_id, {}))
  end

  {}
end

#child_objectObject



45
46
47
# File 'lib/object_momma/child.rb', line 45

def child_object
  @child_object ||= actualize_child_object
end