Module: NestedHashCleaner
- Defined in:
- lib/nested_hash_cleaner.rb,
lib/nested_hash_cleaner/version.rb
Constant Summary collapse
- VERSION =
"0.2.0"
Class Method Summary collapse
- .clean(x, key) ⇒ Object
- .clean_empty_values(x) ⇒ Object
- .clean_with_value(x, value) ⇒ Object
- .clean_with_values(x, values) ⇒ Object
Class Method Details
.clean(x, key) ⇒ Object
6 7 8 9 10 11 12 |
# File 'lib/nested_hash_cleaner.rb', line 6 def clean(x,key) case x when Hash then x = x.inject({}) {|m, (k, v)| m[k] = clean(v,key) unless k == key ; m } when Array then x.map! {|e| clean(e,key)} end x end |
.clean_empty_values(x) ⇒ Object
30 31 32 |
# File 'lib/nested_hash_cleaner.rb', line 30 def clean_empty_values(x) clean_with_values(x, [nil, {}, [], '']) end |
.clean_with_value(x, value) ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/nested_hash_cleaner.rb', line 14 def clean_with_value(x, value) case x when Hash then x = x.inject({}) {|m, (k, v)| m[k] = clean_with_value(v,value) unless v == value ; m } when Array then x.map! {|e| clean_with_value(e,value)}.compact! else x = ((x.respond_to?(:empty?) && x.empty?) ? nil : x) end x end |
.clean_with_values(x, values) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/nested_hash_cleaner.rb', line 23 def clean_with_values(x, values) values.each do |value| x = clean_with_value(x, value) end x end |