Class: Brujula::Mergers::ObjectMerger
- Inherits:
-
Object
- Object
- Brujula::Mergers::ObjectMerger
- Defined in:
- lib/brujula/mergers/object_merger.rb
Instance Attribute Summary collapse
-
#instance ⇒ Object
readonly
Returns the value of attribute instance.
-
#superinstance ⇒ Object
readonly
Returns the value of attribute superinstance.
Instance Method Summary collapse
- #applicable_attributes ⇒ Object
- #call ⇒ Object
- #each_inheritable_attributes ⇒ Object
- #excluded_attributes ⇒ Object
-
#initialize(instance:, superinstance:) ⇒ ObjectMerger
constructor
A new instance of ObjectMerger.
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
#instance ⇒ Object (readonly)
Returns the value of attribute instance.
4 5 6 |
# File 'lib/brujula/mergers/object_merger.rb', line 4 def instance @instance end |
#superinstance ⇒ Object (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_attributes ⇒ Object
32 33 34 |
# File 'lib/brujula/mergers/object_merger.rb', line 32 def applicable_attributes superinstance.instance_variables - excluded_attributes end |
#call ⇒ Object
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_attributes ⇒ Object
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_attributes ⇒ Object
36 37 38 |
# File 'lib/brujula/mergers/object_merger.rb', line 36 def excluded_attributes i( @parent @name @type ) end |