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)|(numeric identifier|identifier)/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ OptionFamily

Returns a new instance of OptionFamily.



17
18
19
20
21
22
23
24
25
# File 'lib/hammer_cli/options/option_family.rb', line 17

def initialize(options = {})
  @all = []
  @children = []
  @options = options
  @creator = options[:creator] || self
  @prefix = options[:prefix]
  @root = options[:root] || options[:aliased_resource] || options[:referenced_resource]
  @creator.family_registry.register(self) if @creator != self
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



13
14
15
# File 'lib/hammer_cli/options/option_family.rb', line 13

def children
  @children
end

Instance Method Details

#adopt(child) ⇒ Object

Raises:

  • (ArgumentError)


100
101
102
103
104
105
106
# File 'lib/hammer_cli/options/option_family.rb', line 100

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



82
83
84
# File 'lib/hammer_cli/options/option_family.rb', line 82

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

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



92
93
94
95
96
97
98
# File 'lib/hammer_cli/options/option_family.rb', line 92

def child(switches, type, description, opts = {}, &block)
  child = new_member(switches, type, description, opts, &block)
  return unless child

  @children << child
  child
end

#descriptionObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/hammer_cli/options/option_family.rb', line 27

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(/\b.+\b/, types) }
  desc = @options[:description] || 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

#find_option(switch) ⇒ Object



116
117
118
# File 'lib/hammer_cli/options/option_family.rb', line 116

def find_option(switch)
  all.find { |m| m.handles?(switch) }
end

#formatsObject



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

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

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

#headObject



78
79
80
# File 'lib/hammer_cli/options/option_family.rb', line 78

def head
  @parent
end

#helpObject



44
45
46
# File 'lib/hammer_cli/options/option_family.rb', line 44

def help
  [help_lhs, help_rhs]
end

#help_lhsObject



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

def help_lhs
  return @parent&.help_lhs if @children.empty?

  types = all.map(&:value_formatter).map { |f| f.completion_type[:type].to_s.upcase }
  switch + ' ' + types.uniq.join('/')
end

#help_rhsObject



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

def help_rhs
  description || @parent.help[1]
end

#option(*args) ⇒ Object



112
113
114
# File 'lib/hammer_cli/options/option_family.rb', line 112

def option(*args)
  HammerCLI::Apipie::OptionDefinition.new(*args)
end

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

Raises:

  • (StandardError)


86
87
88
89
90
# File 'lib/hammer_cli/options/option_family.rb', line 86

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

#rootObject



108
109
110
# File 'lib/hammer_cli/options/option_family.rb', line 108

def root
  @root || @parent&.aliased_resource || @parent&.referenced_resource || common_root
end

#switchObject



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/hammer_cli/options/option_family.rb', line 65

def switch
  return if @parent.nil? && @children.empty?
  return @parent.switches.join(', ').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