Sha256: bfc115394dd5ea0f7e282ed4c732f47aef0ccececa9fa0d93deccdf863bd43ab

Contents?: true

Size: 1.07 KB

Versions: 2

Compression:

Stored size: 1.07 KB

Contents

require 'ostruct'
module Figtree
  class IniConfig < OpenStruct
    def initialize(ini, override = :none)
      # cheat to allow a parsed hash in
      parsed_subgroups = ini.is_a?(Hash) ?
        ini : subgroups_from(ini, override)
      super(parsed_subgroups)
    end

    private
    def subgroups_from(ini_file, override)
      figgy_transform(
        figgy_parse(
          File.read(ini_file)
        ),
        override
      ).reduce({}, :merge!)
    end

    def figgy_parse(str)
      Parser.new.parse(str)
    rescue Parslet::ParseFailed => failure
      STDERR.puts "\nInvalid ini file.\n" +
        "Error: #{failure.cause.ascii_tree}" +
        "Please correct the file and retry."
        raise
    end

    def figgy_transform(tree, override)
      Transformer.new.apply(tree, override: override)
    rescue => e
      STDERR.puts "\nInvalid transformation rule.\n" +
        "Error: #{e}" +
        "Please correct your transformer rule and retry."
        raise TransformFailed
    end
  end

  class Subgroup < OpenStruct
  end

  class TransformFailed < Exception
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
figtree-1.2.0 lib/figtree/ini_config.rb
figtree-1.0.1 lib/figtree/ini_config.rb