Class: Hash
- 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
- #all_keys? ⇒ Boolean
- #all_values? ⇒ Boolean
- #any_key? ⇒ Boolean
- #any_value? ⇒ Boolean
-
#invert_with_dups ⇒ Hash
Invert a hash keeping possible duplicate values.
- #left_deep_merge(other_hash, &block) ⇒ Object
-
#left_deep_merge!(other_hash, &block) ⇒ Hash
Do a deep merge, but don’t override the key-value pairs on the left.
- #nmerge(other_hash, &block) ⇒ Object
-
#nmerge!(other_hash) ⇒ Hash
Does a typical merge, but this will remove blank values on the right side.
- #no_keys? ⇒ Boolean
- #no_values? ⇒ Boolean
-
#only(*keys) ⇒ Object
The opposite of
Hash.except. -
#only!(*keys) ⇒ Object
See
only.
Instance Method Details
#all_keys? ⇒ Boolean
5 6 7 |
# File 'lib/ruby-rails-extensions/extensions/all_keys.rb', line 5 def all_keys?(...) keys.all?(...) end |
#all_values? ⇒ Boolean
5 6 7 |
# File 'lib/ruby-rails-extensions/extensions/all_values.rb', line 5 def all_values?(...) values.all?(...) end |
#any_key? ⇒ Boolean
5 6 7 |
# File 'lib/ruby-rails-extensions/extensions/any_key.rb', line 5 def any_key?(...) keys.any?(...) end |
#any_value? ⇒ Boolean
5 6 7 |
# File 'lib/ruby-rails-extensions/extensions/any_value.rb', line 5 def any_value?(...) values.any?(...) end |
#invert_with_dups ⇒ Hash
Invert a hash keeping possible duplicate values.
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
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.
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
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.
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
5 6 7 |
# File 'lib/ruby-rails-extensions/extensions/no_keys.rb', line 5 def no_keys?(...) keys.none?(...) end |
#no_values? ⇒ Boolean
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.
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 |