Class: Brujula::Mergers::ObjectMerger

Inherits:
Object
  • Object
show all
Defined in:
lib/brujula/mergers/object_merger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance:, superinstance:) ⇒ ObjectMerger

Returns a new instance of ObjectMerger.



6
7
8
9
# File 'lib/brujula/mergers/object_merger.rb', line 6

def initialize(instance:, superinstance:)
  @superinstance = superinstance
  @instance      = instance
end

Instance Attribute Details

#instanceObject (readonly)

Returns the value of attribute instance.



4
5
6
# File 'lib/brujula/mergers/object_merger.rb', line 4

def instance
  @instance
end

#superinstanceObject (readonly)

Returns the value of attribute superinstance.



4
5
6
# File 'lib/brujula/mergers/object_merger.rb', line 4

def superinstance
  @superinstance
end

Instance Method Details

#applicable_attributesObject



32
33
34
# File 'lib/brujula/mergers/object_merger.rb', line 32

def applicable_attributes
  superinstance.instance_variables - excluded_attributes
end

#callObject



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/brujula/mergers/object_merger.rb', line 11

def call
  return superinstance.dup if instance.nil?
  instance.dup.tap do |object|
    each_inheritable_attributes do |name, attribute|
      original_item = object.instance_variable_get("@#{ name }")
      merged_item   = Merger.new(
        instance: original_item, superinstance: attribute
      ).call
      object.instance_variable_set("@#{ name }", merged_item)
    end
  end
end

#each_inheritable_attributesObject



24
25
26
27
28
29
30
# File 'lib/brujula/mergers/object_merger.rb', line 24

def each_inheritable_attributes
  applicable_attributes.each do |name|
    next if superinstance.instance_variable_get(name).nil?
    string_name = name.to_s.gsub(/^@/, '')
    yield(string_name, superinstance.instance_variable_get(name))
  end
end

#excluded_attributesObject



36
37
38
# File 'lib/brujula/mergers/object_merger.rb', line 36

def excluded_attributes
  %i( @parent @name @type )
end