Class: HashDeepDiff::ChangeKey
- Inherits:
-
Object
- Object
- HashDeepDiff::ChangeKey
- Extended by:
- Forwardable
- Defined in:
- lib/hash_deep_diff/change_key.rb
Overview
Key for a compared value inside Array or Hash
Constant Summary collapse
- NESTED_HASH =
element that indicates nested Hash
'{}'
- ARRAY_VALUE =
element that indicates Array value
'...'
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
private
Returns the value of attribute path.
Class Method Summary collapse
-
.initial_object(values:) ⇒ Array, Hash
based on the first element of the key returns the initial object to buld the change representation.
Instance Method Summary collapse
-
#init_nesting(current_key, obj, clone_keys) ⇒ Object
private
prepare nesting before further processing.
-
#init_value(current_key, obj, clone_keys) ⇒ Object
private
prepare an object before further processing.
-
#initialize(path:) ⇒ ChangeKey
constructor
private
A new instance of ChangeKey.
-
#recursive_set(current_key, obj, value, clone_keys) ⇒ Object
private
recursion for deeply nested values.
-
#set(obj, value, clone_keys = path.clone) ⇒ Array, Hash
set the value inside Hash based on the change_key TOFIX; check if @path are mutated.
-
#set_value(current_key, obj, value, clone_keys) ⇒ Object
private
no more nesting - set value inside object.
-
#to_a ⇒ Array
see #to_ary.
-
#to_ary ⇒ Array
array with keysused to initialize the object.
-
#to_s ⇒ String
see #to_str.
-
#to_str ⇒ String
visual representation of the change key.
Constructor Details
#initialize(path:) ⇒ ChangeKey (private)
Returns a new instance of ChangeKey.
68 69 70 |
# File 'lib/hash_deep_diff/change_key.rb', line 68 def initialize(path:) @path = path.to_ary end |
Instance Attribute Details
#path ⇒ Object (readonly, private)
Returns the value of attribute path.
66 67 68 |
# File 'lib/hash_deep_diff/change_key.rb', line 66 def path @path end |
Class Method Details
.initial_object(values:) ⇒ Array, Hash
based on the first element of the key returns the initial object to buld the change representation
18 19 20 21 22 |
# File 'lib/hash_deep_diff/change_key.rb', line 18 def self.initial_object(values:) return [] if values.size.positive? && [NESTED_HASH, ARRAY_VALUE].include?(values[0][0][0]) {} end |
Instance Method Details
#init_nesting(current_key, obj, clone_keys) ⇒ Object (private)
prepare nesting before further processing
82 83 84 85 86 87 88 89 |
# File 'lib/hash_deep_diff/change_key.rb', line 82 def init_nesting(current_key, obj, clone_keys) element = if NESTED_HASH == current_key obj elsif NESTED_HASH == clone_keys.first obj[current_key] end element << {} unless element.nil? || element.last.respond_to?(:to_hash) end |
#init_value(current_key, obj, clone_keys) ⇒ Object (private)
prepare an object before further processing
73 74 75 76 77 78 79 |
# File 'lib/hash_deep_diff/change_key.rb', line 73 def init_value(current_key, obj, clone_keys) if [ARRAY_VALUE] == clone_keys || NESTED_HASH == clone_keys.first obj[current_key] ||= [] elsif !clone_keys.empty? && ![ARRAY_VALUE, NESTED_HASH].include?(current_key) obj[current_key] ||= {} end end |
#recursive_set(current_key, obj, value, clone_keys) ⇒ Object (private)
recursion for deeply nested values
106 107 108 109 110 111 112 113 114 |
# File 'lib/hash_deep_diff/change_key.rb', line 106 def recursive_set(current_key, obj, value, clone_keys) if NESTED_HASH == current_key set(obj.last, value, clone_keys) elsif NESTED_HASH == clone_keys.first set(obj[current_key].last, value, clone_keys[1..]) elsif !clone_keys.empty? set(obj[current_key], value, clone_keys) end end |
#set(obj, value, clone_keys = path.clone) ⇒ Array, Hash
set the value inside Hash based on the change_key TOFIX; check if @path are mutated
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/hash_deep_diff/change_key.rb', line 27 def set(obj, value, clone_keys = path.clone) # 1. Fetch key current_key = clone_keys.shift # 2. Prepare object for further processing init_value(current_key, obj, clone_keys) init_nesting(current_key, obj, clone_keys) # 3. Set value - directly or recursively set_value(current_key, obj, value, clone_keys) recursive_set(current_key, obj, value, clone_keys) return obj end |
#set_value(current_key, obj, value, clone_keys) ⇒ Object (private)
no more nesting - set value inside object
92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/hash_deep_diff/change_key.rb', line 92 def set_value(current_key, obj, value, clone_keys) if ARRAY_VALUE == current_key obj.prepend(*value) clone_keys.pop elsif clone_keys.empty? obj[current_key] = value clone_keys.pop elsif [ARRAY_VALUE] == clone_keys obj[current_key] = value + obj[current_key] clone_keys.pop end end |
#to_a ⇒ Array
see #to_ary
42 43 44 |
# File 'lib/hash_deep_diff/change_key.rb', line 42 def to_a to_ary end |
#to_ary ⇒ Array
array with keysused to initialize the object
48 49 50 |
# File 'lib/hash_deep_diff/change_key.rb', line 48 def to_ary path end |
#to_s ⇒ String
see #to_str
54 55 56 |
# File 'lib/hash_deep_diff/change_key.rb', line 54 def to_s to_str end |
#to_str ⇒ String
visual representation of the change key
60 61 62 |
# File 'lib/hash_deep_diff/change_key.rb', line 60 def to_str path.map { |key| "[#{key}]" }.join end |