Class: Array
- Defined in:
- lib/iarray.rb,
lib/iarray.rb,
lib/iarray.rb
Overview
Internal function for merge.
Instance Method Summary collapse
-
#rh_clone ⇒ Object
return an exact clone of the recursive Array and Hash contents.
-
#rh_exist?(*p) ⇒ Boolean
Recursive Hash deep level existence See details at #Hash.rh_exist?.
-
#rh_get(*p) ⇒ Object
Recursive Hash Get Please look to #Hash::rh_get for details about this function.
-
#rh_lexist?(*p) ⇒ Boolean
Recursive Hash deep level found counter See details from #Hash.rh_lexist?.
-
#rh_merge(data) ⇒ Object
This function is part of the rh_merge functionnality adapted for Array.
- #rh_merge!(data) ⇒ Object
Methods included from RhGet
#_key_options, #_key_to_s, #_loop_on_regs, #_regexp, #_update_res
Methods included from Rh
#merge_cleanup, #merge_cleanup!, #structured?
Instance Method Details
#rh_clone ⇒ Object
return an exact clone of the recursive Array and Hash contents.
-
Args :
-
Returns :
-
Recursive Array/Hash cloned.
-
-
Raises : Nothing
examples:
hdata = { :test => { :test2 => { :test5 => :test,
'text' => 'blabla' },
'test5' => 'test' },
:array => [{ :test => :value1 }, 2, { :test => :value3 }]}
hclone = hdata.rh_clone
hclone[:test] = "test"
hdata[:test] == { :test2 => { :test5 => :test,'text' => 'blabla' }
# => true
hclone[:array].pop
hdata[:array].length != hclone[:array].length
# => true
hclone[:array][0][:test] = "value2"
hdata[:array][0][:test] != hclone[:array][0][:test]
# => true
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/iarray.rb', line 90 def rh_clone result = [] each do |value| begin result << value.rh_clone rescue result << value end end result end |
#rh_exist?(*p) ⇒ Boolean
Recursive Hash deep level existence See details at #Hash.rh_exist?
39 40 41 42 43 44 45 46 |
# File 'lib/iarray.rb', line 39 def rh_exist?(*p) p = p.flatten return nil if p.length == 0 count = p.length (rh_lexist?(*p) == count) end |
#rh_get(*p) ⇒ Object
Recursive Hash Get Please look to #Hash::rh_get for details about this function.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/iarray.rb', line 50 def rh_get(*p) p = p.flatten return self if p.length == 0 key = p[0] sp = p.drop(1) re, res, opts = _regexp(key) return _keys_match(re, res, sp, opts) unless re.nil? return _get_array(sp, key) if [Fixnum, Range].include?(key.class) _loop_get_array(key, p) end |
#rh_lexist?(*p) ⇒ Boolean
Recursive Hash deep level found counter See details from #Hash.rh_lexist?
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/iarray.rb', line 21 def rh_lexist?(*p) p = p.flatten return 0 if p.length == 0 key = p[0] sp = p.drop(1) re, _, opts = _regexp(key) return _keys_match_lexist(re, [], sp, opts) unless re.nil? return _lexist_array(sp, key) if [Fixnum, Range].include?(key.class) _loop_lexist_array(p, key) end |
#rh_merge(data) ⇒ Object
This function is part of the rh_merge functionnality adapted for Array.
To provide Array recursivity, we uses the element index.
**Warning!** If the Array order has changed (sort/random) the index changed and can generate unwanted result.
To implement recursivity, and some specific Array management (add/remove) you have to create an Hash and insert it at position 0 in the ‘self’ Array.
**Warning!** If you create an Array, where index 0 contains a Hash, this Hash will be considered as the Array control element. If the first index of your Array is not a Hash, an empty Hash will be inserted at position 0.
‘data’ has the same restriction then ‘self’ about the first element. ‘data’ can influence the rh_merge Array behavior, by updating the first element.
The first Hash element has the following attributes:
-
:__struct_changing: Array of index which accepts to move from a structured data (Hash/Array) to another structure or type.
Ex: Hash => Array, Array => Integer
-
:__protected: Array of index which protects against update from ‘data’
-
:__remove: Array of elements to remove. each element are remove with Array.delete function. See Array delete function for details.
-
:__remove_index: Array of indexes to remove. Each element are removed with Array.delete_at function. It starts from the highest index until the lowest. See Array delete function for details.
NOTE: __remove and __remove_index cannot be used together.
if both are set, __remove is choosen
NOTE : __remove* is executed before __add*
-
:__add: Array of elements to add. Those elements are systematically added at the end of the Array. See Array.<< for details.
-
:__add_index: Hash of index(key) + Array of data(value) to add. The index listed refer to merged ‘self’ Array. several elements with same index are grouply inserted in the index. ex:
[:data3].rh_merge({:__add_index => [0 => [:data1, :data2]]}) => [{}, :data1, :data2, :data3]
NOTE: __add and __add_index cannot be used together.
if both are set, __add is choosen
How merge is executed:
Starting at index 0, each index of ‘data’ and ‘self’ are used to compare indexed data.
-
If ‘data’ index 0 has not an Hash, the ‘self’ index 0 is just skipped.
-
If ‘data’ index 0 has the ‘control’ Hash, the array will be updated according to :__add and :__remove arrays. when done, those attributes are removed
-
For all next index (1 => ‘data’.length), data are compared
-
If the ‘data’ length is > than ‘self’ length addtionnal indexed data are added to ‘self’
-
If index element exist in both ‘data’ and ‘self’, ‘self’ indexed data is updated/merged according to control. ‘data’ indexed data can use :unset to remove the data at this index nil is also supported. But the index won’t be removed. data will just be set to nil
-
when all Arrays elements are merged, rh_merge will:
-
remove ‘self’ elements containing ‘:unset’
-
merge ‘self’ data at index 0 with ‘data’ found index 0
181 182 183 |
# File 'lib/iarray.rb', line 181 def rh_merge(data) _rh_merge(clone, data) end |
#rh_merge!(data) ⇒ Object
185 186 187 |
# File 'lib/iarray.rb', line 185 def rh_merge!(data) _rh_merge(self, data) end |