Module: Gorillib::Hashlike::Slice

Included in:
Hash
Defined in:
lib/gorillib/hashlike/slice.rb

Instance Method Summary collapse

Instance Method Details

#except(*rejected) ⇒ Object

Return a copy that excludes the given keys

If the receiver responds to +convert_key+, the method is called on each of the arguments. This allows +except+ to play nice with hashes with indifferent access for instance:

Examples:

Exclude a blacklist of parameters

@person.update_attributes(params[:person].except(:admin))

mash, hash, it does the right thing:

{:a => 1}.to_mash.except(:a)  # => {}
{:a => 1}.to_mash.except('a') # => {}


78
79
80
# File 'lib/gorillib/hashlike/slice.rb', line 78

def except(*rejected)
  dup.except!(*rejected)
end

#except!(*rejected) ⇒ Object

Modifies the hash to exclude the given keys

Returns:

  • self

See Also:



86
87
88
89
# File 'lib/gorillib/hashlike/slice.rb', line 86

def except!(*rejected)
  rejected.each{|key| delete(key) }
  self
end

#extract!(*allowed) ⇒ Object

Removes and returns the key/value pairs matching the given keys.

Examples:

hsh = {:a => 1, :b => 2, :c => 3, :d => 4}
hsh.extract!(:a, :b)
# => {:a => 1, :b => 2}
hsh
# => {:c => 3, :d =>4}

Returns:

  • a copy containing the removed key/value pairs



58
59
60
# File 'lib/gorillib/hashlike/slice.rb', line 58

def extract!(*allowed)
  slice!(* self.keys-allowed)
end

#only(*allowed) ⇒ Hash

Return a copy having only the given keys

Examples:

Limit a set of parameters to everything but a few known toggles:

{ :one => 1, :two => 2, :three => 3 }.only(:one)    #=> { :one => 1 }

Parameters:

  • allowed (#include?)

    keys to include.

Returns:

  • (Hash)

    a copy with only the selected keys.



99
100
101
# File 'lib/gorillib/hashlike/slice.rb', line 99

def only(*allowed)
  dup.only!(*allowed)
end

#only!(*allowed) ⇒ Object

Retain only the given keys; return self

Examples:

Limit a set of parameters to everything but a few known toggles:

{ :one => 1, :two => 2, :three => 3 }.only!(:one)    #=> { :one => 1 }

Parameters:

  • allowed (#include?)

    keys to include.

Returns:

  • self



111
112
113
114
# File 'lib/gorillib/hashlike/slice.rb', line 111

def only!(*allowed)
  allowed.map!{|key| convert_key(key) } if respond_to?(:convert_key, true)
  keep_if{|key, val| allowed.include?(key) }
end

#slice(*allowed) ⇒ Object

Returns a copy having only the given keys

If you have an array of keys you want to limit to, splat them:

valid_keys = [:mass, :velocity, :time] search(options.slice(*valid_keys))

Note: Compatible with Rails 4.0 Active Support

Examples:

limit an options hash to valid keys before passing to a method:

def search(criteria = {})
  assert_valid_keys(:mass, :velocity, :time) # gorillib/hash/keys
end
search(options.slice(:mass, :velocity, :time))

Returns:

  • key/value pairs for keys in self and allowed



22
23
24
25
26
27
# File 'lib/gorillib/hashlike/slice.rb', line 22

def slice(*allowed)
  allowed.map!{|key| convert_key(key) } if respond_to?(:convert_key, true)
  hash = self.class.new
  allowed.each{|key| hash[key] = self[key] if has_key?(key) }
  hash
end

#slice!(*allowed) ⇒ Object

Retains only the given keys. Returns a copy containing the removed key/value pairs.

Examples:

hsh = {:a => 1, :b => 2, :c => 3, :d => 4}
hsh.slice!(:a, :b)
# => {:c => 3, :d =>4}
hsh
# => {:a => 1, :b => 2}

Returns:

  • the removed key/value pairs



40
41
42
43
44
45
46
# File 'lib/gorillib/hashlike/slice.rb', line 40

def slice!(*allowed)
  allowed.map!{|key| convert_key(key) } if respond_to?(:convert_key, true)
  omit = slice(*self.keys - allowed)
  hash = slice(*allowed)
  replace(hash)
  omit
end