Class: TenderHash::Map

Inherits:
Object
  • Object
show all
Defined in:
lib/tender_hash/map.rb

Instance Method Summary collapse

Constructor Details

#initialize(hash = nil, &block) ⇒ Map

Returns a new instance of Map.



4
5
6
7
8
# File 'lib/tender_hash/map.rb', line 4

def initialize(hash=nil, &block)
  @old_hash = hash || {}
  @rules = []
  instance_exec(&block) if block_given?
end

Instance Method Details

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



18
19
20
# File 'lib/tender_hash/map.rb', line 18

def key(key, options={})
  map_key(key, key, options)
end

#map_key(old, new, options = {}) ⇒ Object



14
15
16
# File 'lib/tender_hash/map.rb', line 14

def map_key(old, new, options={})
  @rules << Rule.new(old, new, options)
end

#scope(key, &block) ⇒ Object



22
23
24
25
26
# File 'lib/tender_hash/map.rb', line 22

def scope(key, &block)
  scope = ScopeRule.new(key, &block)
  @rules << scope
  scope.map
end

#source=(hash) ⇒ Object



10
11
12
# File 'lib/tender_hash/map.rb', line 10

def source=(hash)
  @old_hash = hash
end

#to_hObject



28
29
30
31
32
33
34
# File 'lib/tender_hash/map.rb', line 28

def to_h
  {}.tap do |new_hash|
    @rules.each do |rule|
      rule.apply(@old_hash, new_hash)
    end
  end
end