Sha256: 793a5eea7978467a4e95b47af580d7e6d74096a903a6f4f83113cbe0f1a1fc65

Contents?: true

Size: 1017 Bytes

Versions: 4

Compression:

Stored size: 1017 Bytes

Contents

# frozen_string_literal: true

module EacRubyUtils
  module Console
    class DocoptRunner
      attr_reader :settings

      private

      def setting_value(key, required = true)
        %i[setting_from_argument setting_from_constant].each do |method|
          value = send(method, key)
          return value if value
        end
        return nil unless required
        raise "Setting \"#{key}\" not found. Declare #{setting_constant(key, true)} constant, " \
          "pass #{key.to_sym} option to #{self.class.name}.new() method."
      end

      def setting_from_argument(key)
        settings[key]
      end

      def setting_from_constant(key)
        constant_name = setting_constant(key)
        begin
          self.class.const_get(constant_name)
        rescue NameError
          nil
        end
      end

      def setting_constant(key, fullname = false)
        name = key.to_s.underscore.upcase
        name = "#{self.class.name}::#{name}" if fullname
        name
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
eac_ruby_utils-0.12.0 lib/eac_ruby_utils/console/docopt_runner/_settings.rb
eac_ruby_utils-0.11.0 lib/eac_ruby_utils/console/docopt_runner/_settings.rb
eac_ruby_utils-0.10.1 lib/eac_ruby_utils/console/docopt_runner/_settings.rb
eac_ruby_utils-0.10.0 lib/eac_ruby_utils/console/docopt_runner/_settings.rb