Class: Hash

Inherits:
Object show all
Defined in:
lib/y_support/core_ext/hash/misc.rb,
lib/y_support/typing/hash/typing.rb

Instance Method Summary collapse

Instance Method Details

#aE_has(key, options = {}, &b) ⇒ Object

This method behaves exactly like #aT_has, but with the difference, that it raises ArgumentError instead of TypeError



69
70
71
72
73
74
75
# File 'lib/y_support/typing/hash/typing.rb', line 69

def aE_has key, options={}, &b
  begin
    options.empty? ? aT_has( key, &b ) : aT_has( key, options, &b )
  rescue TypeError => e
    raise AErr, e.message
  end
end

#aT_has(key, options = {}, &b) ⇒ Object Also known as: must_have

This enforcer method (aka. runtime assertion) raises TypeError when:

  1. Neither the required key nor any of its synonyms are present.

  2. The supplied criterion block, if any, returns false when applied

to the value of the key in question. If the block takes an argument (or more arguments), the value is passed in. If the block takes no arguments (arity 0), it is executed inside the singleton class of the receiver (using #instance_exec method).

Raises:



54
55
56
57
58
59
60
61
62
63
# File 'lib/y_support/typing/hash/typing.rb', line 54

def aT_has key, options={}, &b
  raise TErr, "Key '#{key}' absent!" unless has? key, options
  # Now validate self[key] using the supplied block
  if block_given?
    m = "Value for #{key} fails its duck type!"
    raise TErr, m unless ( b.arity == 0 ? self[key].instance_exec( &b ) :
                             b.( self[key] ) )
  end
  return self[key]
end

#dot!(oo = {}) ⇒ Object

Makes hash keys accessible as methods. If the hash keys collide with its methods, ArgumentError is raised, unless :overwrite_methods option == true.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/y_support/core_ext/hash/misc.rb', line 69

def dot!( oo = {} )
  keys.each do |key|
    msg = "key #{key} of #dot!-ted hash is not convertible to a symbol"
    raise ArgumentError, msg unless key.respond_to? :to_sym
    unless oo[:overwrite_methods]
      if methods.include? key.to_sym
        raise ArgumentError, "#dot!-ted hash must not have key names " +
          "colliding with its methods"
      end
    end
    
    define_singleton_method key.to_sym do
      self[key]
    end
    
    define_singleton_method "#{key}=".to_sym do |value|
      self[key] = value
    end
  end
  return self
end

#has?(key, options = {}) ⇒ Boolean

This method behaves similarly to #may_have, with the difference that it does not return the value of the key, only true / false to indicate whether the key or any synonym has been found.

Returns:

  • (Boolean)


42
43
44
# File 'lib/y_support/typing/hash/typing.rb', line 42

def has? key, options={}
  !merge_synonym_keys!( key, *options[:syn!] ).nil?
end

#may_have(key, options = {}) ⇒ Object

This method uses #merge_synonym_keys! method first and then returns the value under the key. The first argument is the main key. Synonyms may be supplied as a named argument :syn!. (Bang indicates that the synonym keys will be merged with the main key, modifying the hash.)



33
34
35
36
# File 'lib/y_support/typing/hash/typing.rb', line 33

def may_have key, options={}
  merge_synonym_keys!( key, *options[:syn!] ).nil?
  return self[key]
end

#merge_synonym_keys!(key, *synonyms) ⇒ Object

Merges the synonymous hash keys into a single key - useful for argument validation. Returns nil if neither main key, nor synonyms are found. Returns false (no merging) if the main key was found, but no synonym keys. Returns true (yes merging) if any of the synonym keys is found and renamed/merged to the main key. Value collisions in synonym keys (detected by #==) raise ArgumentError.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/y_support/typing/hash/typing.rb', line 11

def merge_synonym_keys!( key, *synonyms )
  synonyms.reduce has_key?( key ) ? false : nil do |acc, syn|
    next acc unless has_key? syn
    if acc.nil? then
      self[key] = self[syn]
      delete syn
      next true
    end
    if self[key] == self[syn] then
      delete syn
      next true
    else
      raise TErr, "Value collision between #{key} and its synonym #{syn}!"
    end
  end
end

#modifyObject

Like #map that returns a hash.



58
59
60
61
62
63
# File 'lib/y_support/core_ext/hash/misc.rb', line 58

def modify
  each_with_object self.class.new do |hash_pair, |
    key, val = yield hash_pair
    [key] = val
  end
end

#modify_keysObject

The difference from do_with_keys is that modify_keys expects block that takes 2 arguments (key: value pair) and returns the new key.



19
20
21
22
23
# File 'lib/y_support/core_ext/hash/misc.rb', line 19

def modify_keys
  each_with_object self.class.new do |hash_pair, |
    [ yield( hash_pair ) ] = self[ hash_pair[0] ]
  end
end

#modify_valuesObject

The difference from #do_with_values is that modify_values expects block that takes 2 arguments (key: value pair) and returns the new value.



44
45
46
47
48
# File 'lib/y_support/core_ext/hash/misc.rb', line 44

def modify_values
  each_with_object self.class.new do |hash_pair, |
    [ hash_pair[0] ] = yield( hash_pair )
  end
end

#modify_values!Object

Like #modify_values, but modifies the receiver



51
52
53
54
55
# File 'lib/y_support/core_ext/hash/misc.rb', line 51

def modify_values!
  each_with_object self do |hash_pair, |
    [ hash_pair[0] ] = yield( hash_pair )
  end
end

#with_keysObject Also known as: do_with_keys

Applies a block as a mapping on all keys, returning a new hash



10
11
12
13
14
# File 'lib/y_support/core_ext/hash/misc.rb', line 10

def with_keys
  keys.each_with_object self.class.new do |hash_key, |
    [ yield( hash_key ) ] = self[ hash_key ]
  end
end

#with_valuesObject Also known as: do_with_values

Applies a block as a mapping on all values, returning a new hash



26
27
28
29
30
# File 'lib/y_support/core_ext/hash/misc.rb', line 26

def with_values
  each_with_object self.class.new do |hash_pair, |
    [ hash_pair[0] ] = yield( hash_pair[1] )
  end
end

#with_values!Object Also known as: do_with_values!

Like #do_with_values, but modifies the receiver.



34
35
36
37
38
39
# File 'lib/y_support/core_ext/hash/misc.rb', line 34

def with_values!
  each_with_object self do |hash_pair, |
    hash_key, hash_val = hash_pair
    [ hash_key ] = yield( hash_val )
  end
end