Sha256: 4cc7b98c412a2cee975b4c825005ae89ff22b7fdb567c3337cfc58e6b126e67f

Contents?: true

Size: 1009 Bytes

Versions: 2

Compression:

Stored size: 1009 Bytes

Contents

require 'active_support/core_ext'
require 'yaml'

module Csv2hash
  class YamlLoader

    attr_reader :conf
    attr_accessor :definition

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

    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
    end

    private

    def load_config_file file
      if file.to_s =~ /(?<ext>\.erb\.)/
        YAML.load(ERB.new(File.read(file)).result)
      else
        YAML.load_file(file)
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
csv2hash-0.6.5 lib/csv2hash/yaml_loader.rb
csv2hash-0.6.4 lib/csv2hash/yaml_loader.rb