Class: Hash

Inherits:
Object show all
Defined in:
opal/fron/core_ext/hash.rb

Overview

Hash

Instance Method Summary collapse

Instance Method Details

#deep_diff(other) ⇒ Hash

Produces a diff hash from self and the other hash

Parameters:

  • other (Hash)

    The other hash

Returns:

  • (Hash)

    The difference from 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

Parameters:

  • other (Hash)

    The other hash

Returns:

  • (Hash)

    The difference



8
9
10
# File 'opal/fron/core_ext/hash.rb', line 8

def difference(other)
  Hash[to_a - other.to_a]
end

#to_form_dataFormData

Converts the hash into a form data object

Returns:

  • (FormData)

    The native 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_stringString

Converts the hash into an url encoded query string

Returns:

  • (String)

    the 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