Sha256: fde71d3d5a3513cff53b624e1167afcf32db9c956e83ad01a6d6f9c6eb81c0db

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

# encoding=utf-8

require 'yaml'

require_relative 'env_opts'
require_relative 'object_present'
require_relative 'tap'

include Tap #; tap_config

# read configuration YAML file during initalization
# value priority: menu default < configuration file < environment variable < command argument
#
class YamlEnvOpts < EnvOpts
  def initialize(opts_raw = {}, argv: ARGV, configuration_yaml_filespec: '', values: nil)
    super(opts_raw || {}, nil)
    per_values(values)
    configure_yaml_filespec(configuration_yaml_filespec)
    set_keys_value_per_environment_as_cast(opts_raw)
    parse(argv)

    self # rubocop:disable Lint/Void
  end

  private

  def apply_configuration(settings)
    settings.tap_inspect 'settings'
    return if settings.nil?

    settings.each do |name, value|
      set_key_value_as_cast(name, value)
    end.tap_inspect
  end

  def configure_yaml_filespec(configuration_yaml_filespec)
    apply_configuration(read_configuration_yaml(configuration_yaml_filespec))
  end

  def per_values(settings)
    settings.tap_inspect 'settings'
    return self if settings.nil?
    return self if settings.count.zero?

    settings.each do |name, value|
      set_key_value_as_cast(name, value)
    end

    self
  end

  def read_configuration_yaml(configuration_yaml_filespec)
    configuration_yaml_filespec.tap_inspect 'configuration_yaml_filespec'
    return unless configuration_yaml_filespec.present?

    YAML.safe_load(File.read(configuration_yaml_filespec) || '').tap_inspect
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
markdown_exec-1.3.2 lib/yaml_env_opts.rb