Class: Lev::Outputs

Inherits:
Hashie::Mash
  • Object
show all
Defined in:
lib/lev/outputs.rb

Instance Method Summary collapse

Constructor Details

#initialize(source_hash = nil, default = nil, &blk) ⇒ Outputs

Returns a new instance of Outputs.



4
5
6
7
# File 'lib/lev/outputs.rb', line 4

def initialize(source_hash = nil, default = nil, &blk)
  @array_created = {}
  super(source_hash, default, &blk)
end

Instance Method Details

#add(name, value) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/lev/outputs.rb', line 9

def add(name, value)
  if self[name].nil?
    self[name] = value
  elsif @array_created[name]
    self[name].push value
  else
    @array_created[name] = true
    self[name] = [self[name], value]
  end
end

#eachObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/lev/outputs.rb', line 20

def each
  self.each_key do |key|
    key = key.to_sym
    if @array_created[key]
      self[key].each { |value| yield key, value }
    else
      yield key, self[key]
    end
  end
end

#transfer_to(other_outputs, &name_mapping_block) ⇒ Object



31
32
33
34
35
36
# File 'lib/lev/outputs.rb', line 31

def transfer_to(other_outputs, &name_mapping_block)
  self.each do |name, value|
    new_name = block_given? ? name_mapping_block.call(name) : name
    other_outputs.add(new_name, value)
  end
end