Module: ModNormalizedHash::InstanceMethods

Included in:
NormalizedHash
Defined in:
lib/doodle/normalized_hash.rb

Instance Method Summary collapse

Instance Method Details

#[](k) ⇒ Object



66
67
68
# File 'lib/doodle/normalized_hash.rb', line 66

def [](k)
  super(normalize_key(k))
end

#[]=(k, v) ⇒ Object



70
71
72
# File 'lib/doodle/normalized_hash.rb', line 70

def []=(k,v)
  super(normalize_key(k), normalize_value(v))
end

#default(k) ⇒ Object



37
38
39
# File 'lib/doodle/normalized_hash.rb', line 37

def default(k)
  super(normalize_key(k))
end

#default=(value) ⇒ Object



41
42
43
# File 'lib/doodle/normalized_hash.rb', line 41

def default=(value)
  super(normalize_value(value))
end

#delete(k) ⇒ Object

Note that invert returns a new Hash. This is by design. If you want the new hash to have the same properties as its source, use something like:

h = StringKeyHash.new(h.invert)

  def invert
    super
  end


62
63
64
# File 'lib/doodle/normalized_hash.rb', line 62

def delete(k)
  super(normalize_key(k))
end

#fetch(k) ⇒ Object



49
50
51
# File 'lib/doodle/normalized_hash.rb', line 49

def fetch(k)
  super(normalize_key(k))
end

#has_value?(v) ⇒ Boolean Also known as: value?

Returns:

  • (Boolean)


81
82
83
# File 'lib/doodle/normalized_hash.rb', line 81

def has_value?(v)
  super(normalize_value(v))
end

#index(value) ⇒ Object



115
116
117
# File 'lib/doodle/normalized_hash.rb', line 115

def index(value)
  super(normalize_value(v))
end

#initialize(arg = {}, &block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/doodle/normalized_hash.rb', line 13

def initialize(arg = {}, &block)
  if block_given?
    original_block = block
    # this is unfortunate
    block = proc { |h, k|
      #p [:block, h, k]
      res = normalize_value(original_block[h, normalize_key(k)])
      #p [:block_self, self, res]
      each do |k, v|
        #p [:init_block, k, v]
        self[k] = normalize_value(v)
      end
      #p [:block_res, self, res]
      res
    }
  end
  if arg.is_a?(Hash)
    super(&block)
    update(arg)
  else
    super(arg, &block)
  end
end

#key?(k) ⇒ Boolean Also known as: has_key?

Returns:

  • (Boolean)


74
75
76
# File 'lib/doodle/normalized_hash.rb', line 74

def key?(k)
  super(normalize_key(k))
end

#merge(other) ⇒ Object



100
101
102
# File 'lib/doodle/normalized_hash.rb', line 100

def merge(other)
  self.dup.update(other)
end

#replace(other) ⇒ Object



110
111
112
113
# File 'lib/doodle/normalized_hash.rb', line 110

def replace(other)
  self.clear
  update(other)
end

#store(k, v) ⇒ Object



45
46
47
# File 'lib/doodle/normalized_hash.rb', line 45

def store(k,v)
  super(normalize_key(k), normalize_value(v))
end

#update(other, &block) ⇒ Object Also known as: merge!



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/doodle/normalized_hash.rb', line 86

def update(other, &block)
  if block_given?
    # {|key, oldval, newval| block}
    super(other) { |key, oldval, newval|
      normalize_value(block.call(key, oldval, newval))
    }
  else
    other.each do |k,v|
      store(k,v)
    end
  end
end

#values_at(*keys) ⇒ Object Also known as: indices, indexes



104
105
106
# File 'lib/doodle/normalized_hash.rb', line 104

def values_at(*keys)
  super(*keys.map{ |k| normalize_key(k)})
end