subhash 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/rh.rb ADDED
@@ -0,0 +1,157 @@
1
+ # encoding: UTF-8
2
+
3
+ # (c) Copyright 2014 Hewlett-Packard Development Company, L.P.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ # Rh common module included in Hash and Array class.
18
+ module Rh
19
+ public
20
+
21
+ def merge_cleanup!
22
+ _rh_remove_control(self)
23
+ end
24
+
25
+ def merge_cleanup
26
+ _rh_remove_control(rh_clone)
27
+ end
28
+
29
+ private
30
+
31
+ # Function which will parse arrays in hierarchie and will remove any control
32
+ # element (index 0)
33
+ def _rh_remove_control(result)
34
+ return unless [Hash, Array].include?(result.class)
35
+
36
+ if result.is_a?(Hash)
37
+ result.each { |elem| _rh_remove_control(elem) }
38
+ else
39
+ result.delete_at(0) if result[0].is_a?(Hash) && result[0].key?(:__control)
40
+ result.each_index { |index| _rh_remove_control(result[index]) }
41
+ end
42
+ result
43
+ end
44
+
45
+ # Internal function to determine if result and data key contains both Hash or
46
+ # Array and if so, do the merge task on those sub Hash/Array
47
+ #
48
+ def _rh_merge_recursive(result, key, data)
49
+ return false unless [Array, Hash].include?(data.class)
50
+
51
+ value = data[key]
52
+ return false unless [Array, Hash].include?(value.class) &&
53
+ value.class == result[key].class
54
+
55
+ if object_id == result.object_id
56
+ result[key].rh_merge!(value)
57
+ else
58
+ result[key] = result[key].rh_merge(value)
59
+ end
60
+
61
+ true
62
+ end
63
+
64
+ # Internal function to determine if changing from Hash/Array to anything else
65
+ # is authorized or not.
66
+ #
67
+ # The structure is changing if `result` or `value` move from Hash/Array to any
68
+ # other type.
69
+ #
70
+ # * *Args*:
71
+ # - result: Merged Hash or Array structure.
72
+ # - key : Key in result and data.
73
+ # - data : Hash or Array structure to merge.
74
+ #
75
+ # * *returns*:
76
+ # - +true+ : if :__struct_changing == true
77
+ # - +false+ : otherwise.
78
+ def _rh_struct_changing_ok?(result, key, data)
79
+ return true unless [Array, Hash].include?(data[key].class) ||
80
+ [Array, Hash].include?(result[key].class)
81
+
82
+ # result or value are structure (Hash or Array)
83
+ if result.is_a?(Hash)
84
+ control = result[:__struct_changing]
85
+ else
86
+ control = result[0][:__struct_changing]
87
+ key -= 1
88
+ end
89
+ return true if control.is_a?(Array) && control.include?(key)
90
+
91
+ false
92
+ end
93
+
94
+ # Internal function to determine if a data merged can be updated by any
95
+ # other object like Array, String, etc...
96
+ #
97
+ # The decision is given by a :__unset setting.
98
+ #
99
+ # * *Args*:
100
+ # - Hash/Array data to replace.
101
+ # - key: string or symbol.
102
+ #
103
+ # * *returns*:
104
+ # - +false+ : if key is found in :__protected Array.
105
+ # - +true+ : otherwise.
106
+ def _rh_merge_ok?(result, key)
107
+ if result.is_a?(Hash)
108
+ control = result[:__protected]
109
+ else
110
+ control = result[0][:__protected]
111
+ key -= 1
112
+ end
113
+
114
+ return false if control.is_a?(Array) && control.include?(key)
115
+
116
+ true
117
+ end
118
+
119
+ def _rh_control_tags
120
+ [:__remove, :__remove_index, :__add, :__add_index,
121
+ :__protected, :__struct_changing, :__control]
122
+ end
123
+ end
124
+
125
+ # Module to implement common function for Hash and Array class
126
+ module RhGet
127
+ def _regexp(key)
128
+ return key, [] if key.is_a?(Regexp)
129
+ return nil, nil unless key.is_a?(String)
130
+
131
+ regs = []
132
+ regs << [%r{^/(.*)/(e)?$}, []]
133
+ regs << [%r{^\[/(.*)/(e)?\]$}, []]
134
+ regs << [%r{^\{/(.*)/(e)?\}$}, {}]
135
+
136
+ _loop_on_regs(regs, key)
137
+ end
138
+
139
+ def _loop_on_regs(regs, key)
140
+ regs.each do |r|
141
+ init = r[1]
142
+ reg = r[0].match(key)
143
+ return Regexp.new(reg[1]), init, reg[2] if reg && reg[1]
144
+ end
145
+ [nil, nil, nil]
146
+ end
147
+
148
+ def _key_to_s(k)
149
+ return ':' + k.to_s if k.is_a?(Symbol)
150
+ k
151
+ end
152
+
153
+ def _update_res(res, k, v)
154
+ res << v if res.is_a?(Array)
155
+ res[k] = v if res.is_a?(Hash)
156
+ end
157
+ end
@@ -1,5 +1,5 @@
1
1
  # Recursive Hash
2
2
  module SubHash
3
- VERSION = '0.1.1'
4
- DATE = '2015-05-11'
3
+ VERSION = '0.1.2'
4
+ DATE = '2015-06-03'
5
5
  end