Class: Qipowl::Mappers::Mapper

Inherits:
Object
  • Object
show all
Defined in:
lib/qipowl/core/mapper.rb

Overview

Operates +mapping+ for loaded +YAML+ rules files.

  • For top level sections, each section name should have the corresponding method within Mapping class; default is [:includes] which is processed with #includes method which is simply loads rules from the respective file

Mapping may be loaded from YAML file, as well as be merged against other YAML file, hash or Ruler instance.

Direct Known Subclasses

BowlerMapper

Instance Method Summary collapse

Constructor Details

#initialize(input = nil) ⇒ Mapper

Returns a new instance of Mapper.



22
23
24
25
# File 'lib/qipowl/core/mapper.rb', line 22

def initialize input = nil
  @hash = {}
  merge!(input) unless input.nil?
end

Instance Method Details

#load_yaml(input) ⇒ Object (private)



50
51
52
# File 'lib/qipowl/core/mapper.rb', line 50

def load_yaml input
  IO === input ? YAML.load_stream(input) : load_yaml_file("#{input.downcase}")
end

#load_yaml_file(name) ⇒ Object (private)



43
44
45
46
47
48
# File 'lib/qipowl/core/mapper.rb', line 43

def load_yaml_file name
  [*Qipowl.bowlers].each { |b|
    (return YAML.load_file("#{b}/#{name}.yaml")) rescue next
  }
  nil
end

#merge!(input) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/qipowl/core/mapper.rb', line 29

def merge! input
  map = load_yaml(input) if input.is_one_of?(String, IO)
  raise ArgumentError.new "Invalid map (#{input} @ #{Qipowl.bowlers}) for merge in Mapper.\nCurrent dir: [#{Dir.pwd}].\n" \
    unless map.respond_to? :to_hash

  incs = map.delete(:includes)

  @entities_dirty = true
  @hash.rmerge!(map.to_hash)
  incs.each { |inc|
    merge! inc
  } rescue NoMethodError # FIXME WTF rescueing here?
end

#to_hashObject



26
27
28
# File 'lib/qipowl/core/mapper.rb', line 26

def to_hash
  @hash
end