Class: Hash
Overview
Hash
Instance Method Summary collapse
-
#deep_diff(other) ⇒ Hash
Produces a diff hash from self and the other hash.
-
#difference(other) ⇒ Hash
(also: #-)
Returns the difference from the other object.
-
#to_form_data ⇒ FormData
Converts the hash into a form data object.
-
#to_query_string ⇒ String
Converts the hash into an url encoded query string.
Instance Method Details
#deep_diff(other) ⇒ Hash
Produces a diff hash from self and the other hash
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'opal/fron/core_ext/hash.rb', line 39 def deep_diff(other) (keys + other.keys).uniq.each_with_object({}) do |key, diff| self_key = self[key] other_key = other[key] next if self_key == other_key diff[key] = if self_key.respond_to?(:deep_diff) && other_key.respond_to?(:deep_diff) self_key.deep_diff(other_key) else [self_key, other_key] end diff end end |
#difference(other) ⇒ Hash Also known as: -
Returns the difference from the other object
8 9 10 |
# File 'opal/fron/core_ext/hash.rb', line 8 def difference(other) Hash[to_a - other.to_a] end |
#to_form_data ⇒ FormData
Converts the hash into a form data object
26 27 28 29 30 31 32 |
# File 'opal/fron/core_ext/hash.rb', line 26 def to_form_data form_data = `new FormData()` each do |key, value| `#{form_data}.append(#{key},#{value})` end form_data end |
#to_query_string ⇒ String
Converts the hash into an url encoded query string
17 18 19 20 21 |
# File 'opal/fron/core_ext/hash.rb', line 17 def to_query_string map do |key, value| `encodeURIComponent(#{key})+"="+encodeURIComponent(#{value})` end.join '&' end |