Class: Hash

Inherits:
Object show all
Defined in:
lib/ruby-rails-extensions/extensions/nmerge.rb,
lib/ruby-rails-extensions/extensions/any_key.rb,
lib/ruby-rails-extensions/extensions/no_keys.rb,
lib/ruby-rails-extensions/extensions/all_keys.rb,
lib/ruby-rails-extensions/extensions/any_value.rb,
lib/ruby-rails-extensions/extensions/hash_only.rb,
lib/ruby-rails-extensions/extensions/no_values.rb,
lib/ruby-rails-extensions/extensions/all_values.rb,
lib/ruby-rails-extensions/extensions/left_deep_merge.rb,
lib/ruby-rails-extensions/extensions/invert_with_dups.rb

Instance Method Summary collapse

Instance Method Details

#all_keys?Boolean

Returns:

  • (Boolean)

See Also:

  • Enumerable#all?


5
6
7
# File 'lib/ruby-rails-extensions/extensions/all_keys.rb', line 5

def all_keys?(...)
  keys.all?(...)
end

#all_values?Boolean

Returns:

  • (Boolean)

See Also:

  • Enumerable#all?


5
6
7
# File 'lib/ruby-rails-extensions/extensions/all_values.rb', line 5

def all_values?(...)
  values.all?(...)
end

#any_key?Boolean

Returns:

  • (Boolean)

See Also:

  • Enumerable#any?


5
6
7
# File 'lib/ruby-rails-extensions/extensions/any_key.rb', line 5

def any_key?(...)
  keys.any?(...)
end

#any_value?Boolean

Returns:

  • (Boolean)

See Also:

  • Enumerable#any?


5
6
7
# File 'lib/ruby-rails-extensions/extensions/any_value.rb', line 5

def any_value?(...)
  values.any?(...)
end

#invert_with_dupsHash

Invert a hash keeping possible duplicate values.

Examples:

h = {:a => 1, :b => 2, :c => 3, :d => 4, :z => 1}
h.invert
# => {1=>:z, 2=>:b, 3=>:c, 4=>:d}
h.invert_with_dups
# => {1=>[:a, :z], 2=>[:b], 3=>[:c], 4=>[:d]}

Returns:

See Also:

  • for more details or to use without keeping duplicates.
  • for source.


17
18
19
# File 'lib/ruby-rails-extensions/extensions/invert_with_dups.rb', line 17

def invert_with_dups
  keys.group_by { |key| self[key] }
end

#left_deep_merge(other_hash, &block) ⇒ Object

See Also:



23
24
25
# File 'lib/ruby-rails-extensions/extensions/left_deep_merge.rb', line 23

def left_deep_merge(other_hash, &block)
  dup.left_deep_merge!(other_hash, &block)
end

#left_deep_merge!(other_hash, &block) ⇒ Hash

Do a deep merge, but don’t override the key-value pairs on the left.

Parameters:

Returns:



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ruby-rails-extensions/extensions/left_deep_merge.rb', line 10

def left_deep_merge!(other_hash, &block)
  merge!(other_hash) do |key, this_val, other_val|
    if this_val.is_a?(Hash) && other_hash.is_a?(Hash)
      this_val.left_deep_merge(other_val, &block)
    elsif block_given?
      yield(key, this_val, other_val)
    else
      this_val.presence || other_val
    end
  end
end

#nmerge(other_hash, &block) ⇒ Object

See Also:

  • #merge!


21
22
23
# File 'lib/ruby-rails-extensions/extensions/nmerge.rb', line 21

def nmerge(other_hash, &block)
  dup.nmerge!(other_hash, &block)
end

#nmerge!(other_hash) ⇒ Hash

Does a typical merge, but this will remove blank values on the right side.

Parameters:

Returns:



10
11
12
13
14
15
16
17
18
# File 'lib/ruby-rails-extensions/extensions/nmerge.rb', line 10

def nmerge!(other_hash)
  merge!(other_hash) do |key, this_val, other_val|
    if block_given?
      yield(key, this_val, other_val)
    else
      RubyRailsExtensions.presence(other_val) || this_val
    end
  end
end

#no_keys?Boolean

Returns:

  • (Boolean)

See Also:

  • Enumerable#none?


5
6
7
# File 'lib/ruby-rails-extensions/extensions/no_keys.rb', line 5

def no_keys?(...)
  keys.none?(...)
end

#no_values?Boolean

Returns:

  • (Boolean)

See Also:

  • Enumerable#none?


5
6
7
# File 'lib/ruby-rails-extensions/extensions/no_values.rb', line 5

def no_values?(...)
  values.none?(...)
end

#only(*keys) ⇒ Object

The opposite of Hash.except. ~/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.5/lib/active_support/core_ext/hash/except.rb apidock.com/rails/Hash/except

Selects only the supplied keys.

api.rubyonrails.org/classes/Hash.html#method-i-slice



12
13
14
# File 'lib/ruby-rails-extensions/extensions/hash_only.rb', line 12

def only(*keys)
  slice(*keys)
end

#only!(*keys) ⇒ Object



20
21
22
# File 'lib/ruby-rails-extensions/extensions/hash_only.rb', line 20

def only!(*keys)
  slice!(*keys)
end