Class: Sfn::Config

Inherits:
Bogo::Config
  • Object
show all
Defined in:
lib/sfn/config.rb,
lib/sfn/config/conf.rb,
lib/sfn/config/diff.rb,
lib/sfn/config/init.rb,
lib/sfn/config/lint.rb,
lib/sfn/config/list.rb,
lib/sfn/config/plan.rb,
lib/sfn/config/graph.rb,
lib/sfn/config/print.rb,
lib/sfn/config/trace.rb,
lib/sfn/config/create.rb,
lib/sfn/config/events.rb,
lib/sfn/config/export.rb,
lib/sfn/config/import.rb,
lib/sfn/config/update.rb,
lib/sfn/config/destroy.rb,
lib/sfn/config/inspect.rb,
lib/sfn/config/promote.rb,
lib/sfn/config/realize.rb,
lib/sfn/config/describe.rb,
lib/sfn/config/validate.rb

Overview

Top level configuration

Defined Under Namespace

Classes: Conf, Create, Describe, Destroy, Diff, Events, Export, Graph, Import, Init, Inspect, Lint, List, Plan, Print, Promote, Realize, Trace, Update, Validate

Constant Summary collapse

BOOLEAN =

Only values allowed designating bool type

BOOLEAN_VALUES = [TrueClass, FalseClass].freeze
TRISTATE_BOOLEAN =

Boolean type with nil included

(BOOLEAN + [NilClass]).freeze

Class Method Summary collapse

Class Method Details

._options_for(klass, shorts) ⇒ Smash

Provide options for config class

Parameters:

  • klass (Class)
  • shorts (Array<String>)

Returns:

  • (Smash)


149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# 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) ⇒ Hash

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

Parameters:

  • name (String, Symbol)

    name of attribute

  • type (Class, Array<Class>)

    valid types

  • info (Hash) (defaults to: Smash.new)

    attribute information

Returns:

  • (Hash)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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) ⇒ Smash

Provide all options for config class (includes global configs)

Parameters:

  • klass (Class)

Returns:

  • (Smash)


139
140
141
142
# File 'lib/sfn/config.rb', line 139

def self.options_for(klass)
  shorts = ["h"] # always reserve `-h` for help
  _options_for(klass, shorts)
end