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.
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.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/branch_io_cli/configuration/option.rb', line 20 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.
11 12 13 |
# File 'lib/branch_io_cli/configuration/option.rb', line 11 def aliases @aliases end |
#argument_optional ⇒ Object
Returns the value of attribute argument_optional.
10 11 12 |
# File 'lib/branch_io_cli/configuration/option.rb', line 10 def argument_optional @argument_optional end |
#confirm_symbol ⇒ Object
Returns the value of attribute confirm_symbol.
13 14 15 |
# File 'lib/branch_io_cli/configuration/option.rb', line 13 def confirm_symbol @confirm_symbol end |
#convert_proc ⇒ Object
Returns the value of attribute convert_proc.
16 17 18 |
# File 'lib/branch_io_cli/configuration/option.rb', line 16 def convert_proc @convert_proc end |
#default_value ⇒ Object
Returns the value of attribute default_value.
8 9 10 |
# File 'lib/branch_io_cli/configuration/option.rb', line 8 def default_value @default_value end |
#description ⇒ Object
Returns the value of attribute description.
7 8 9 |
# File 'lib/branch_io_cli/configuration/option.rb', line 7 def description @description end |
#env_name ⇒ Object
Returns the value of attribute env_name.
5 6 7 |
# File 'lib/branch_io_cli/configuration/option.rb', line 5 def env_name @env_name end |
#example ⇒ Object
Returns the value of attribute example.
9 10 11 |
# File 'lib/branch_io_cli/configuration/option.rb', line 9 def example @example end |
#label ⇒ Object
Returns the value of attribute label.
17 18 19 |
# File 'lib/branch_io_cli/configuration/option.rb', line 17 def label @label end |
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'lib/branch_io_cli/configuration/option.rb', line 4 def name @name end |
#negatable ⇒ Object
Returns the value of attribute negatable.
12 13 14 |
# File 'lib/branch_io_cli/configuration/option.rb', line 12 def negatable @negatable end |
#skip_confirmation ⇒ Object
Returns the value of attribute skip_confirmation.
18 19 20 |
# File 'lib/branch_io_cli/configuration/option.rb', line 18 def skip_confirmation @skip_confirmation end |
#type ⇒ Object
Returns the value of attribute type.
6 7 8 |
# File 'lib/branch_io_cli/configuration/option.rb', line 6 def type @type end |
#valid_values_proc ⇒ Object
Returns the value of attribute valid_values_proc.
14 15 16 |
# File 'lib/branch_io_cli/configuration/option.rb', line 14 def valid_values_proc @valid_values_proc end |
#validate_proc ⇒ Object
Returns the value of attribute validate_proc.
15 16 17 |
# File 'lib/branch_io_cli/configuration/option.rb', line 15 def validate_proc @validate_proc end |
Instance Method Details
#convert(value) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/branch_io_cli/configuration/option.rb', line 63 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
79 80 81 82 83 84 85 86 87 |
# File 'lib/branch_io_cli/configuration/option.rb', line 79 def display_value(value) if type.nil? value ? "yes" : "no" elsif value.nil? "(none)" else value.to_s end end |
#env_value ⇒ Object
59 60 61 |
# File 'lib/branch_io_cli/configuration/option.rb', line 59 def env_value convert(ENV[env_name]) if env_name end |
#ui_type ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/branch_io_cli/configuration/option.rb', line 49 def ui_type if type.nil? "Boolean" elsif type == Array "Comma-separated list" else type.to_s end end |
#valid?(value) ⇒ Boolean
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/branch_io_cli/configuration/option.rb', line 89 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
45 46 47 |
# File 'lib/branch_io_cli/configuration/option.rb', line 45 def valid_values return valid_values_proc.call if valid_values_proc && valid_values_proc.kind_of?(Proc) end |