Class: BranchIOCLI::Configuration::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/branch_io_cli/configuration/option.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Option

Returns a new instance of Option.

Raises:

  • (ArgumentError)


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(options)
  @name = options[:name]
  @env_name = options[:env_name]
  @type = options[:type]
  @description = options[:description]
  @default_value = options[:default_value]
  @example = options[:example]
  @argument_optional = options[:argument_optional] || false
  @aliases = options[:aliases] || []
  @aliases = [@aliases] unless @aliases.kind_of?(Array)
  @negatable = options[:negatable]
  @negatable = options[:type].nil? if options[:negatable].nil?
  @confirm_symbol = options[:confirm_symbol] || @name
  @valid_values_proc = options[:valid_values_proc]
  @validate_proc = options[:validate_proc]
  @convert_proc = options[:convert_proc]
  @label = options[:label] || @name.to_s.capitalize.gsub(/_/, ' ')
  @skip_confirmation = options[: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

#aliasesObject

Returns the value of attribute aliases.



22
23
24
# File 'lib/branch_io_cli/configuration/option.rb', line 22

def aliases
  @aliases
end

#argument_optionalObject

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_symbolObject

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_procObject

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_valueObject

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

#descriptionObject

Returns the value of attribute description.



18
19
20
# File 'lib/branch_io_cli/configuration/option.rb', line 18

def description
  @description
end

#env_nameObject

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

#exampleObject

Returns the value of attribute example.



20
21
22
# File 'lib/branch_io_cli/configuration/option.rb', line 20

def example
  @example
end

#labelObject

Returns the value of attribute label.



28
29
30
# File 'lib/branch_io_cli/configuration/option.rb', line 28

def label
  @label
end

#nameObject

Returns the value of attribute name.



15
16
17
# File 'lib/branch_io_cli/configuration/option.rb', line 15

def name
  @name
end

#negatableObject

Returns the value of attribute negatable.



23
24
25
# File 'lib/branch_io_cli/configuration/option.rb', line 23

def negatable
  @negatable
end

#skip_confirmationObject

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

#typeObject

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_procObject

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_procObject

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_optionsObject



4
5
6
7
8
9
10
11
12
13
# File 'lib/branch_io_cli/configuration/option.rb', line 4

def self.global_options
  [
    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_valueObject



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_typeObject



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

Returns:

  • (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_valuesObject



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