Class: BranchIOCLI::Configuration::Option
- Inherits:
-
Object
- Object
- BranchIOCLI::Configuration::Option
- Defined in:
- lib/branch_io_cli/configuration/option.rb
Instance Attribute Summary collapse
-
#aliases ⇒ Object
Returns the value of attribute aliases.
-
#argument_optional ⇒ Object
Returns the value of attribute argument_optional.
-
#confirm_symbol ⇒ Object
Returns the value of attribute confirm_symbol.
-
#convert_proc ⇒ Object
Returns the value of attribute convert_proc.
-
#default_value ⇒ Object
Returns the value of attribute default_value.
-
#description ⇒ Object
Returns the value of attribute description.
-
#env_name ⇒ Object
Returns the value of attribute env_name.
-
#example ⇒ Object
Returns the value of attribute example.
-
#label ⇒ Object
Returns the value of attribute label.
-
#name ⇒ Object
Returns the value of attribute name.
-
#negatable ⇒ Object
Returns the value of attribute negatable.
-
#skip_confirmation ⇒ Object
Returns the value of attribute skip_confirmation.
-
#type ⇒ Object
Returns the value of attribute type.
-
#valid_values_proc ⇒ Object
Returns the value of attribute valid_values_proc.
-
#validate_proc ⇒ Object
Returns the value of attribute validate_proc.
Class Method Summary collapse
Instance Method Summary collapse
- #convert(value) ⇒ Object
- #display_value(value) ⇒ Object
- #env_value ⇒ Object
-
#initialize(options) ⇒ Option
constructor
A new instance of Option.
- #ui_type ⇒ Object
- #valid?(value) ⇒ Boolean
- #valid_values ⇒ Object
Constructor Details
#initialize(options) ⇒ Option
Returns a new instance of Option.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/branch_io_cli/configuration/option.rb', line 31 def initialize() @name = [:name] @env_name = [:env_name] @type = [:type] @description = [:description] @default_value = [:default_value] @example = [:example] @argument_optional = [:argument_optional] || false @aliases = [:aliases] || [] @aliases = [@aliases] unless @aliases.kind_of?(Array) @negatable = [:negatable] @negatable = [:type].nil? if [:negatable].nil? @confirm_symbol = [:confirm_symbol] || @name @valid_values_proc = [:valid_values_proc] @validate_proc = [:validate_proc] @convert_proc = [:convert_proc] @label = [:label] || @name.to_s.capitalize.gsub(/_/, ' ') @skip_confirmation = [:skip_confirmation] raise ArgumentError, "Use :validate_proc or :valid_values_proc, but not both." if @valid_values_proc && @validate_proc @env_name = "BRANCH_#{@name.to_s.upcase}" if @env_name.nil? @argument_optional ||= @negatable end |
Instance Attribute Details
#aliases ⇒ Object
Returns the value of attribute aliases.
22 23 24 |
# File 'lib/branch_io_cli/configuration/option.rb', line 22 def aliases @aliases end |
#argument_optional ⇒ Object
Returns the value of attribute argument_optional.
21 22 23 |
# File 'lib/branch_io_cli/configuration/option.rb', line 21 def argument_optional @argument_optional end |
#confirm_symbol ⇒ Object
Returns the value of attribute confirm_symbol.
24 25 26 |
# File 'lib/branch_io_cli/configuration/option.rb', line 24 def confirm_symbol @confirm_symbol end |
#convert_proc ⇒ Object
Returns the value of attribute convert_proc.
27 28 29 |
# File 'lib/branch_io_cli/configuration/option.rb', line 27 def convert_proc @convert_proc end |
#default_value ⇒ Object
Returns the value of attribute default_value.
19 20 21 |
# File 'lib/branch_io_cli/configuration/option.rb', line 19 def default_value @default_value end |
#description ⇒ Object
Returns the value of attribute description.
18 19 20 |
# File 'lib/branch_io_cli/configuration/option.rb', line 18 def description @description end |
#env_name ⇒ Object
Returns the value of attribute env_name.
16 17 18 |
# File 'lib/branch_io_cli/configuration/option.rb', line 16 def env_name @env_name end |
#example ⇒ Object
Returns the value of attribute example.
20 21 22 |
# File 'lib/branch_io_cli/configuration/option.rb', line 20 def example @example end |
#label ⇒ Object
Returns the value of attribute label.
28 29 30 |
# File 'lib/branch_io_cli/configuration/option.rb', line 28 def label @label end |
#name ⇒ Object
Returns the value of attribute name.
15 16 17 |
# File 'lib/branch_io_cli/configuration/option.rb', line 15 def name @name end |
#negatable ⇒ Object
Returns the value of attribute negatable.
23 24 25 |
# File 'lib/branch_io_cli/configuration/option.rb', line 23 def negatable @negatable end |
#skip_confirmation ⇒ Object
Returns the value of attribute skip_confirmation.
29 30 31 |
# File 'lib/branch_io_cli/configuration/option.rb', line 29 def skip_confirmation @skip_confirmation end |
#type ⇒ Object
Returns the value of attribute type.
17 18 19 |
# File 'lib/branch_io_cli/configuration/option.rb', line 17 def type @type end |
#valid_values_proc ⇒ Object
Returns the value of attribute valid_values_proc.
25 26 27 |
# File 'lib/branch_io_cli/configuration/option.rb', line 25 def valid_values_proc @valid_values_proc end |
#validate_proc ⇒ Object
Returns the value of attribute validate_proc.
26 27 28 |
# File 'lib/branch_io_cli/configuration/option.rb', line 26 def validate_proc @validate_proc end |
Class Method Details
.global_options ⇒ Object
4 5 6 7 8 9 10 11 12 13 |
# File 'lib/branch_io_cli/configuration/option.rb', line 4 def self. [ new( name: :confirm, description: "Enable or disable many prompts", default_value: true, skip_confirmation: true ) ] end |
Instance Method Details
#convert(value) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/branch_io_cli/configuration/option.rb', line 74 def convert(value) return convert_proc.call(value) if convert_proc if type == Array value = value.split(",") if value.kind_of?(String) elsif type == String && value.kind_of?(String) value = value.strip value = nil if value.empty? elsif type.nil? value = true if value.kind_of?(String) && value =~ /^[ty]/i value = false if value.kind_of?(String) && value =~ /^[fn]/i end value end |
#display_value(value) ⇒ Object
90 91 92 93 94 95 96 97 98 |
# File 'lib/branch_io_cli/configuration/option.rb', line 90 def display_value(value) if type.nil? value ? "yes" : "no" elsif value.nil? "(none)" else value.to_s end end |
#env_value ⇒ Object
70 71 72 |
# File 'lib/branch_io_cli/configuration/option.rb', line 70 def env_value convert(ENV[env_name]) if env_name end |
#ui_type ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'lib/branch_io_cli/configuration/option.rb', line 60 def ui_type if type.nil? "Boolean" elsif type == Array "Comma-separated list" else type.to_s end end |
#valid?(value) ⇒ Boolean
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/branch_io_cli/configuration/option.rb', line 100 def valid?(value) return validate_proc.call(value) if validate_proc return true if value.nil? if valid_values && type != Array valid_values.include? value elsif valid_values value.all? { |v| valid_values.include?(v) } elsif type value.kind_of? type else value == true || value == false end end |
#valid_values ⇒ Object
56 57 58 |
# File 'lib/branch_io_cli/configuration/option.rb', line 56 def valid_values return valid_values_proc.call if valid_values_proc && valid_values_proc.kind_of?(Proc) end |