Module: HashSubtraction
- Defined in:
- lib/hash_subtraction.rb
Instance Method Summary collapse
-
#-(h) ⇒ Object
extending with HashSubtraction allows for removal of duplicate keys by subtracting one hash from another.
- #compact(opts = {}) ⇒ Object
- #deep_diff(h) ⇒ Object
- #deep_remove_duplicate_keys(h) ⇒ Object
- #deep_remove_duplicates(h) ⇒ Object
- #deep_subtraction(h) ⇒ Object
- #diff(h) ⇒ Object
- #remove_duplicate_keys(h) ⇒ Object
- #remove_duplicates(h) ⇒ Object
Instance Method Details
#-(h) ⇒ Object
extending with HashSubtraction allows for removal of duplicate keys by subtracting one hash from another. This is used to keep us from re-translating already translated text.
3 4 5 6 |
# File 'lib/hash_subtraction.rb', line 3 def - (h) h.extend(HashSubtraction) self.deep_subtraction(h).extend(HashSubtraction).compact end |
#compact(opts = {}) ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/hash_subtraction.rb', line 23 def compact(opts={}) inject({}) do |new_hash, (k,v)| if !v.nil? new_hash[k] = opts[:recurse] && v.class == Hash ? v.compact(opts) : v end new_hash end end |
#deep_diff(h) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/hash_subtraction.rb', line 32 def deep_diff(h) self.merge(h) do |k, old, new| old.extend(HashSubtraction) if old.is_a?(Hash) case old when Hash then (new.class == Hash) ? (old.diff(new)) : old else (old != new) ? new : nil end end end |
#deep_remove_duplicate_keys(h) ⇒ Object
69 70 71 72 73 |
# File 'lib/hash_subtraction.rb', line 69 def deep_remove_duplicate_keys(h) self.merge(h) do |k, old, new| nil end end |
#deep_remove_duplicates(h) ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/hash_subtraction.rb', line 59 def deep_remove_duplicates(h) self.merge(h) do |k, old, new| old.extend(HashSubtraction) if old.is_a?(Hash) case old when Hash then (new.class == Hash) ? (old.remove_duplicates(new)) : old else (old == new) ? nil : old end end end |
#deep_subtraction(h) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/hash_subtraction.rb', line 42 def deep_subtraction(h) self.merge(h) do |k, old, new| old.extend(HashSubtraction) if old.is_a?(Hash) case old when Array then (new.class == Array) ? (old - new) : (old - [new]) when Hash then (new.class == Hash) ? (old - new) : old when Fixnum, Float, Integer then (old - new) when String old = (new.class == String) ? old.gsub(new,'') : old old = nil if old.empty? old else (old == new) ? nil : old end end end |
#diff(h) ⇒ Object
18 19 20 21 |
# File 'lib/hash_subtraction.rb', line 18 def diff(h) h.extend(HashSubtraction) self.deep_diff(h).extend(HashSubtraction).compact end |
#remove_duplicate_keys(h) ⇒ Object
13 14 15 16 |
# File 'lib/hash_subtraction.rb', line 13 def remove_duplicate_keys(h) h.extend(HashSubtraction) self.deep_remove_duplicate_keys(h).extend(HashSubtraction).compact end |
#remove_duplicates(h) ⇒ Object
8 9 10 11 |
# File 'lib/hash_subtraction.rb', line 8 def remove_duplicates(h) h.extend(HashSubtraction) self.deep_remove_duplicates(h).extend(HashSubtraction).compact end |