Class: HammerCLI::Options::OptionFamily

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

Constant Summary collapse

IDS_REGEX =
/(\A[Ii][Dd][s]?)|\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
# 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]
  @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

Raises:

  • (ArgumentError)


75
76
77
78
79
80
81
# File 'lib/hammer_cli/options/option_family.rb', line 75

def adopt(child)
  raise ArgumentError, 'Parent cannot be a child within the same family' if child == @parent
  raise ArgumentError, 'Child is already in the family' if @children.include?(child)

  child.family = self
  @children << child
end

#allObject



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

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

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



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

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

#descriptionObject



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

def description
  types = all.map(&:type).map { |s| s.split('_').last.to_s }
             .map(&:downcase).join('/')
  parent_desc = @parent.help[1].gsub(IDS_REGEX) { |w| w.gsub(/\w+/, types) }
  desc = parent_desc.strip.empty? ? @options[:description] : parent_desc
  if @options[:deprecation].class <= String
    format_deprecation_msg(desc, _('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(desc, full_msg)
  else
    desc
  end
end

#formatsObject



36
37
38
39
40
# File 'lib/hammer_cli/options/option_family.rb', line 36

def formats
  return [@options[:format].class] if @options[:format]

  all.map(&:value_formatter).map(&:class).uniq
end

#headObject



55
56
57
# File 'lib/hammer_cli/options/option_family.rb', line 55

def head
  @parent
end

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

Raises:

  • (StandardError)


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

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



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/hammer_cli/options/option_family.rb', line 42

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