Class: HammerCLI::Options::OptionFamily
- Inherits:
-
Object
- Object
- HammerCLI::Options::OptionFamily
- 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
-
#children ⇒ Object
readonly
Returns the value of attribute children.
Instance Method Summary collapse
- #adopt(child) ⇒ Object
- #all ⇒ Object
- #child(switches, type, description, opts = {}, &block) ⇒ Object
- #description ⇒ Object
- #head ⇒ Object
-
#initialize(options = {}) ⇒ OptionFamily
constructor
A new instance of OptionFamily.
- #parent(switches, type, description, opts = {}, &block) ⇒ Object
- #switch ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ OptionFamily
10 11 12 13 14 15 16 17 18 |
# File 'lib/hammer_cli/options/option_family.rb', line 10 def initialize( = {}) @all = [] @children = [] = @creator = [:creator] || Class.new(HammerCLI::Apipie::Command) @prefix = [:prefix] @description = [:description] @root = [:root] || [:aliased_resource] || [:referenced_resource] end |
Instance Attribute Details
#children ⇒ Object (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 |
#all ⇒ Object
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 |
#description ⇒ Object
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 [:deprecation].class <= String format_deprecation_msg(@description, _('Deprecated: %{deprecated_msg}') % { deprecated_msg: [:deprecation] }) elsif [:deprecation].class <= Hash full_msg = [: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 |
#head ⇒ Object
49 50 51 |
# File 'lib/hammer_cli/options/option_family.rb', line 49 def head @parent end |
#parent(switches, type, description, opts = {}, &block) ⇒ Object
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 |
#switch ⇒ Object
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 |