Class: Csv2hash::YamlLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/csv2hash/yaml_loader.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ YamlLoader

Returns a new instance of YamlLoader.



10
11
12
13
# File 'lib/csv2hash/yaml_loader.rb', line 10

def initialize file
  @conf = load_config_file file
  self.conf.deep_symbolize_keys!
end

Instance Attribute Details

#confObject (readonly)

Returns the value of attribute conf.



7
8
9
# File 'lib/csv2hash/yaml_loader.rb', line 7

def conf
  @conf
end

#definitionObject

Returns the value of attribute definition.



8
9
10
# File 'lib/csv2hash/yaml_loader.rb', line 8

def definition
  @definition
end

Class Method Details

.load!(file) ⇒ Object



15
16
17
# File 'lib/csv2hash/yaml_loader.rb', line 15

def self.load! file
  new(file).tap &:load!
end

Instance Method Details

#load!Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/csv2hash/yaml_loader.rb', line 19

def load!
  mapping         = self.conf.fetch(:mapping)
  header_size     = self.conf.fetch(:header_size).to_i
  structure_rules = self.conf.fetch(:structure_rules)

  self.definition = Main.generate_definition self.conf.fetch(:name) do
    set_type            { mapping }
    set_header_size     { header_size }
    set_structure_rules { structure_rules }
  end
  self.conf.fetch(:rules).each do |rule|
    definition.cells << Cell.new(rule)
  end

  Main[self.conf.fetch(:name)] = self.definition
  nil
end