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/graph.rb,
lib/sfn/config/print.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/describe.rb,
lib/sfn/config/validate.rb

Overview

Top level configuration

Direct Known Subclasses

Describe, Destroy, Events, Export, Import, Init, Inspect, List, Promote, Validate

Defined Under Namespace

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

Constant Summary collapse

BOOLEAN_VALUES =

Only values allowed designating bool type

[TrueClass, FalseClass]

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)


134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/sfn/config.rb', line 134

def self._options_for(klass, shorts)
  Smash[
    ([klass] + klass.ancestors).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
  ]
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)


124
125
126
127
# File 'lib/sfn/config.rb', line 124

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