Sha256: d0a76aa7b879805ab1b8ebf10e4adfb97e00b5054c356e4232766576c76bc5e2

Contents?: true

Size: 904 Bytes

Versions: 72

Compression:

Stored size: 904 Bytes

Contents

require 'yaml'

module Itamae
  class Config
    CONFIG_MATCHER = /(-c|--config) +([^ ]+)/

    def initialize(options)
      @options = options
    end

    def load
      return @options unless config_given?

      configs, options = parse_options
      configs.each do |config|
        options += load_config(config)
      end
      options
    end

    private

    def parse_options
      configs = []
      parsed_option = joined_options.gsub(CONFIG_MATCHER).each do |match|
        configs << Regexp.last_match[2]
        next ''
      end
      [configs, parsed_option.split(' ')]
    end

    def load_config(config)
      YAML.load(open(config)).inject([]) do |options, (key, value)|
        options + ["--#{key}", value.to_s]
      end
    end

    def config_given?
      joined_options =~ CONFIG_MATCHER
    end

    def joined_options
      @option ||= @options.join(' ')
    end
  end
end

Version data entries

72 entries across 72 versions & 1 rubygems

Version Path
itamae-1.1.8 lib/itamae/config.rb
itamae-1.1.7 lib/itamae/config.rb
itamae-1.1.5 lib/itamae/config.rb
itamae-1.1.4 lib/itamae/config.rb
itamae-1.1.3 lib/itamae/config.rb
itamae-1.1.2 lib/itamae/config.rb
itamae-1.1.1 lib/itamae/config.rb
itamae-1.1.0 lib/itamae/config.rb
itamae-1.0.13 lib/itamae/config.rb
itamae-1.0.12 lib/itamae/config.rb
itamae-1.0.11 lib/itamae/config.rb
itamae-1.0.10 lib/itamae/config.rb