Module: Extensions::OpenStruct
- Defined in:
- lib/baltix/extensions.rb
Instance Method Summary collapse
- #compact ⇒ Object
- #deep_dup ⇒ Object
-
#deep_merge(other_in, options_in = {}) ⇒ Object
deep_merge
deeply merges the Open Struct hash structure with theother_in
enumerating it key by key. - #each(*args, &block) ⇒ Object
- #find(&block) ⇒ Object
- #map(*args, &block) ⇒ Object
- #merge(other) ⇒ Object
- #merge_to(other) ⇒ Object
- #reduce(default = nil, &block) ⇒ Object
- #replace(new_os) ⇒ Object
- #select(&block) ⇒ Object
- #to_os ⇒ Object
Instance Method Details
#compact ⇒ Object
134 135 136 |
# File 'lib/baltix/extensions.rb', line 134 def compact select { |_, value| !value.blank? } end |
#deep_dup ⇒ Object
164 165 166 167 168 169 170 |
# File 'lib/baltix/extensions.rb', line 164 def deep_dup self.reduce({}.to_os) do |r, x, y| r[x] = y.respond_to?(:deep_dup) ? y.deep_dup : y.dup r end end |
#deep_merge(other_in, options_in = {}) ⇒ Object
deep_merge
deeply merges the Open Struct hash structure with the other_in
enumerating it key by key. options
are the options to change behaviour of the method. It allows two keys: :mode, and :dedup :mode key can be :append, :prepend, or :replace, defaulting to :append, when mode is to append, it combines duplicated keys’ values into an array, when :prepend it prepends an other value before previously stored one unlike for :append mode, when :replace it replace duplicate values with a last ones. :dedup key can be true, or false. It allows to deduplicate values when appending or prepending values. Examples:
open_struct.deep_merge(other_open_struct)
open_struct.deep_merge(other_open_struct, :prepend)
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/baltix/extensions.rb', line 182 def deep_merge other_in, = {} return self if other_in.nil? or other_in.blank? = { mode: :append }.merge() other = if other_in.is_a?(::OpenStruct) other_in.deep_dup elsif other_in.is_a?(::Hash) other_in.to_os else ::OpenStruct.new(nil => other_in) end other.reduce(self.to_os) do |res, key, value| res[key] = if res.table.keys.include?(key) case value when ::Hash value.deep_merge(res[key].to_h, ) when ::OpenStruct value.deep_merge(res[key].to_os, ) when ::Array value.concat([res[key]].compact.flatten(1)) when ::NilClass res[key] else value_out = if [:mode] == :append [value, res[key]].compact.flatten(1) elsif [:mode] == :prepend [res[key], value].compact.flatten(1) else value end if value_out.is_a?(Array) && [:dedup] value_out.uniq else value_out end end else value end res end end |
#each(*args, &block) ⇒ Object
138 139 140 |
# File 'lib/baltix/extensions.rb', line 138 def each *args, &block self.each_pair(*args, &block) end |
#find(&block) ⇒ Object
152 153 154 |
# File 'lib/baltix/extensions.rb', line 152 def find &block select(&block).first end |
#map(*args, &block) ⇒ Object
114 115 116 117 118 119 120 121 122 |
# File 'lib/baltix/extensions.rb', line 114 def map *args, &block res = self.class.new self.each_pair do |key, value| res[key] = block[key, value] end res end |
#merge(other) ⇒ Object
110 111 112 |
# File 'lib/baltix/extensions.rb', line 110 def merge other ::OpenStruct.new(self.to_h.deep_merge(other.to_h)) end |
#merge_to(other) ⇒ Object
106 107 108 |
# File 'lib/baltix/extensions.rb', line 106 def merge_to other ::OpenStruct.new(other.to_h.deep_merge(self.to_h)) end |
#reduce(default = nil, &block) ⇒ Object
142 143 144 145 146 147 148 149 150 |
# File 'lib/baltix/extensions.rb', line 142 def reduce default = nil, &block res = default self.each_pair do |key, value| res = block[res, key, value] end res end |
#replace(new_os) ⇒ Object
156 157 158 159 160 161 162 |
# File 'lib/baltix/extensions.rb', line 156 def replace new_os self.to_h.keys.each {|x| self[x] = nil } new_os.to_os.each {|x, v| self[x] = v } self end |
#select(&block) ⇒ Object
124 125 126 127 128 129 130 131 132 |
# File 'lib/baltix/extensions.rb', line 124 def select &block res = self.class.new self.each_pair do |key, value| res[key] = value if block[key, value] end res end |
#to_os ⇒ Object
102 103 104 |
# File 'lib/baltix/extensions.rb', line 102 def to_os self end |