Class: BranchIOCLI::Configuration::Option

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Option

Returns a new instance of Option.

Raises:

  • (ArgumentError)


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(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.



11
12
13
# File 'lib/branch_io_cli/configuration/option.rb', line 11

def aliases
  @aliases
end

#argument_optionalObject

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_symbolObject

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_procObject

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_valueObject

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

#descriptionObject

Returns the value of attribute description.



7
8
9
# File 'lib/branch_io_cli/configuration/option.rb', line 7

def description
  @description
end

#env_nameObject

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

#exampleObject

Returns the value of attribute example.



9
10
11
# File 'lib/branch_io_cli/configuration/option.rb', line 9

def example
  @example
end

#labelObject

Returns the value of attribute label.



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

def label
  @label
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/branch_io_cli/configuration/option.rb', line 4

def name
  @name
end

#negatableObject

Returns the value of attribute negatable.



12
13
14
# File 'lib/branch_io_cli/configuration/option.rb', line 12

def negatable
  @negatable
end

#skip_confirmationObject

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

#typeObject

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_procObject

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_procObject

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_valueObject



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_typeObject



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_valuesObject



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