class Sfn::Config

Top level configuration

Constants

BOOLEAN

Only values allowed designating bool type

TRISTATE_BOOLEAN

Boolean type with nil included

Public Class Methods

_options_for(klass, shorts) click to toggle source

Provide options for config class

@param klass [Class] @param shorts [Array<String>] @return [Smash]

# File lib/sfn/config.rb, line 149
def self._options_for(klass, shorts)
  opts = Smash[
    [klass].map do |a|
      if a.ancestors.include?(Bogo::Config) && !a.attributes.empty?
        a.attributes
      end
    end.compact.reverse.inject(Smash.new) { |m, n| m.deep_merge(n) }.map do |name, info|
      next unless info[:description]
      short = info[:short_flag]
      if !short.to_s.empty? && shorts.include?(short)
        raise ArgumentError.new "Short flag already in use! (`#{short}` not available for `#{klass}`)"
      end
      unless short.to_s.empty?
        shorts << short
        info[:short] = short
      end
      info[:long] = name.tr("_", "-")
      info[:boolean] = [info[:type]].compact.flatten.all? { |t| BOOLEAN_VALUES.include?(t) }
      [name, info]
    end.compact
  ]
  opts
end
attribute(name, type, info = Smash.new) click to toggle source

Override attribute helper to detect Hash types and automatically add type conversion for CLI provided values + description update

@param name [String, Symbol] name of attribute @param type [Class, Array<Class>] valid types @param info [Hash] attribute information @return [Hash]

Calls superclass method
# File lib/sfn/config.rb, line 16
def self.attribute(name, type, info = Smash.new)
  if [type].flatten.any? { |t| t.ancestors.include?(Hash) }
    unless info[:coerce]
      info[:coerce] = lambda do |v|
        case v
        when String
          Smash[
            v.split(/,(?=[^,]*:)/).map do |item_pair|
              item_pair.split(/[=:]/, 2)
            end
          ]
        when Hash
          v.to_smash
        else
          v
        end
      end
      info[:description] ||= ""
      info[:description] << " (Key:Value[,Key:Value,...])"
    end
  end
  super(name, type, info)
end
options_for(klass) click to toggle source

Provide all options for config class (includes global configs)

@param klass [Class] @return [Smash]

# File lib/sfn/config.rb, line 139
def self.options_for(klass)
  shorts = ["h"] # always reserve `-h` for help
  _options_for(klass, shorts)
end