Class: JayAPI::Mergeables::MergeSelector::Merger

Inherits:
Object
  • Object
show all
Defined in:
lib/jay_api/mergeables/merge_selector/merger.rb

Overview

This class is responsible for merging two hashes together into a new one. The merge behaviour here differs from the standard Hash merging, and can be summarized with the following rules: (Let A be a ‘mergee’ Hash and B a ‘merger’ Hash. This would be equivalent to A.merge(B))

  • All nodes in Hash B will completely overwrite nodes in Hash A.

  • All nodes in Hash A that are not found in Hash B will be ignored in the result.

  • If a node value is ‘nil’ in Hash B and the node matches a node in Hash A then the matching node in Hash A will be ‘selected’ to be in the result.

See Also:

  • tests for details and examples.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mergee, merger) ⇒ Merger

Returns a new instance of Merger.

Parameters:

  • merger (HashWithIndifferentAccess)
  • mergee (HashWithIndifferentAccess)


24
25
26
27
# File 'lib/jay_api/mergeables/merge_selector/merger.rb', line 24

def initialize(mergee, merger)
  @merger = merger
  @mergee = mergee
end

Instance Attribute Details

#mergeeObject (readonly)

Returns the value of attribute mergee.



20
21
22
# File 'lib/jay_api/mergeables/merge_selector/merger.rb', line 20

def mergee
  @mergee
end

#mergerObject (readonly)

Returns the value of attribute merger.



20
21
22
# File 'lib/jay_api/mergeables/merge_selector/merger.rb', line 20

def merger
  @merger
end

Instance Method Details

#to_hHashWithIndifferentAccess

Returns The merged result.

Returns:

  • (HashWithIndifferentAccess)

    The merged result.



30
31
32
# File 'lib/jay_api/mergeables/merge_selector/merger.rb', line 30

def to_h
  {}.with_indifferent_access.tap { |hash| deep_merge(merger, hash) }
end