Class: HammerCLI::Options::OptionFamily

Inherits:
Object
  • Object
show all
Defined in:
lib/hammer_cli/options/option_family.rb

Constant Summary collapse

IDS_REGEX =
/\s?([Ii][Dd][s]?)\W|([Ii][Dd][s]?\Z)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ OptionFamily

Returns a new instance of OptionFamily.



10
11
12
13
14
15
16
17
18
# File 'lib/hammer_cli/options/option_family.rb', line 10

def initialize(options = {})
  @all = []
  @children = []
  @options = options
  @creator = options[:creator] || Class.new(HammerCLI::Apipie::Command)
  @prefix = options[:prefix]
  @description = options[:description]
  @root = options[:root] || options[:aliased_resource] || options[:referenced_resource]
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



6
7
8
# File 'lib/hammer_cli/options/option_family.rb', line 6

def children
  @children
end

Instance Method Details

#adopt(child) ⇒ Object



69
70
71
72
# File 'lib/hammer_cli/options/option_family.rb', line 69

def adopt(child)
  child.family = self
  @children << child
end

#allObject



53
54
55
# File 'lib/hammer_cli/options/option_family.rb', line 53

def all
  @children + [@parent].compact
end

#child(switches, type, description, opts = {}, &block) ⇒ Object



63
64
65
66
67
# File 'lib/hammer_cli/options/option_family.rb', line 63

def child(switches, type, description, opts = {}, &block)
  child = new_member(switches, type, description, opts, &block)
  @children << child
  child
end

#descriptionObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/hammer_cli/options/option_family.rb', line 20

def description
  types = all.map(&:type).map { |s| s.split('_').last.to_s }
             .map(&:capitalize).join('/')
  @description ||= @parent.help[1].gsub(IDS_REGEX) { |w| w.gsub(/\w+/, types) }
  if @options[:deprecation].class <= String
    format_deprecation_msg(@description, _('Deprecated: %{deprecated_msg}') % { deprecated_msg: @options[:deprecation] })
  elsif @options[:deprecation].class <= Hash
    full_msg = @options[:deprecation].map do |flag, msg|
      _('%{flag} is deprecated: %{deprecated_msg}') % { flag: flag, deprecated_msg: msg }
    end.join(', ')
    format_deprecation_msg(@description, full_msg)
  else
    @description
  end
end

#headObject



49
50
51
# File 'lib/hammer_cli/options/option_family.rb', line 49

def head
  @parent
end

#parent(switches, type, description, opts = {}, &block) ⇒ Object

Raises:

  • (StandardError)


57
58
59
60
61
# File 'lib/hammer_cli/options/option_family.rb', line 57

def parent(switches, type, description, opts = {}, &block)
  raise StandardError, 'Option family can have only one parent' if @parent

  @parent = new_member(switches, type, description, opts, &block)
end

#switchObject



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/hammer_cli/options/option_family.rb', line 36

def switch
  return if @parent.nil? && @children.empty?
  return @parent.help_lhs.strip if @children.empty?

  switch_start = main_switch.each_char
                            .zip(*all.map(&:switches).flatten.map(&:each_char))
                            .select { |a, b| a == b }.transpose.first.join
  suffixes = all.map do |m|
    m.switches.map { |s| s.gsub(switch_start, '') }
  end.flatten.reject(&:empty?).sort { |x, y| x.size <=> y.size }
  "#{switch_start}[#{suffixes.join('|')}]"
end