Class: StrokeDB::LazyMappingHash

Inherits:
Hash show all
Defined in:
lib/strokedb/util/lazy_mapping_hash.rb

Direct Known Subclasses

HashSlotValue

Instance Method Summary collapse

Methods inherited from Hash

#_stroke_split_merge_result, #except, #reverse_merge, #reverse_merge!, #stringify_keys, #stroke_diff, #stroke_merge, #stroke_patch, #to_raw

Constructor Details

#initialize(original = {}, decoder = nil, encoder = nil) ⇒ LazyMappingHash

Returns a new instance of LazyMappingHash.



3
4
5
6
7
8
# File 'lib/strokedb/util/lazy_mapping_hash.rb', line 3

def initialize(original = {}, decoder = nil, encoder = nil)
  @decoder = decoder || proc {|v| v}
  @encoder = encoder || proc {|v| v}
  super(default)
  original.each {|k,v| self[k] = v } 
end

Instance Method Details

#[](k) ⇒ Object



21
22
23
# File 'lib/strokedb/util/lazy_mapping_hash.rb', line 21

def [](k)
  @encoder.call(_square_brackets(@decoder.call(k)))
end

#[]=(k, v) ⇒ Object



26
27
28
# File 'lib/strokedb/util/lazy_mapping_hash.rb', line 26

def []=(k,v)
  _square_brackets_set(@decoder.call(k),@decoder.call(v))
end

#_eachObject



30
# File 'lib/strokedb/util/lazy_mapping_hash.rb', line 30

alias :_each :each

#_keysObject



38
# File 'lib/strokedb/util/lazy_mapping_hash.rb', line 38

alias :_keys :keys

#_square_bracketsObject



20
# File 'lib/strokedb/util/lazy_mapping_hash.rb', line 20

alias :_square_brackets :[]

#_square_brackets_setObject



25
# File 'lib/strokedb/util/lazy_mapping_hash.rb', line 25

alias :_square_brackets_set :[]=

#_valuesObject



43
# File 'lib/strokedb/util/lazy_mapping_hash.rb', line 43

alias :_values :values

#classObject



48
49
50
# File 'lib/strokedb/util/lazy_mapping_hash.rb', line 48

def class
  Hash
end

#eachObject



31
32
33
34
35
36
# File 'lib/strokedb/util/lazy_mapping_hash.rb', line 31

def each
  e = @encoder
  _each do |k, v|
    yield(e.call(k), e.call(v))
  end
end

#keysObject



39
40
41
# File 'lib/strokedb/util/lazy_mapping_hash.rb', line 39

def keys
  _keys.map {|k| @encoder.call(k)}
end

#map_with(&block) ⇒ Object



10
11
12
13
# File 'lib/strokedb/util/lazy_mapping_hash.rb', line 10

def map_with(&block)
  @encoder = block
  self
end

#unmap_with(&block) ⇒ Object



15
16
17
18
# File 'lib/strokedb/util/lazy_mapping_hash.rb', line 15

def unmap_with(&block)
  @decoder = block
  self
end

#valuesObject



44
45
46
# File 'lib/strokedb/util/lazy_mapping_hash.rb', line 44

def values
  _values.map {|v| @encoder.call(v)}
end