Class: OptParseValidator::OptBase
- Inherits:
-
Object
- Object
- OptParseValidator::OptBase
show all
- Defined in:
- lib/opt_parse_validator/opts/base.rb
Overview
Base Option This Option should not be called, children should be used.
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(option, attrs = {}) ⇒ OptBase
Note:
The :default and :normalize ‘logics’ are done in OptParseValidator::OptParser#add_option
Returns a new instance of OptBase.
17
18
19
20
21
22
|
# File 'lib/opt_parse_validator/opts/base.rb', line 17
def initialize(option, attrs = {})
@option = option
@attrs = attrs
append_help_messages if respond_to?(:append_help_messages)
end
|
Instance Attribute Details
#attrs ⇒ Object
Returns the value of attribute attrs.
6
7
8
|
# File 'lib/opt_parse_validator/opts/base.rb', line 6
def attrs
@attrs
end
|
#option ⇒ Object
Returns the value of attribute option.
6
7
8
|
# File 'lib/opt_parse_validator/opts/base.rb', line 6
def option
@option
end
|
#required=(value) ⇒ Object
Sets the attribute required
5
6
7
|
# File 'lib/opt_parse_validator/opts/base.rb', line 5
def required=(value)
@required = value
end
|
Instance Method Details
#append_help_messages ⇒ Void
25
26
27
|
# File 'lib/opt_parse_validator/opts/base.rb', line 25
def append_help_messages
option << "Default: #{default}" if default
end
|
#choices ⇒ Array<Mixed>
44
45
46
|
# File 'lib/opt_parse_validator/opts/base.rb', line 44
def choices
attrs[:choices]
end
|
#default ⇒ Mixed
39
40
41
|
# File 'lib/opt_parse_validator/opts/base.rb', line 39
def default
attrs[:default]
end
|
#help_messages ⇒ Array<String>
110
111
112
113
114
115
116
|
# File 'lib/opt_parse_validator/opts/base.rb', line 110
def help_messages
first_message_index = option.index { |e| e[0] != '-' }
return [] unless first_message_index
option[first_message_index..-1]
end
|
#normalize(value) ⇒ Mixed
Apply each methods from attrs to the value if possible User input should not be used in this attrs
e.g: normalize: :to_sym will return the symbol of the value
normalize: [:to_sym, :upcase] Will return the upercased symbol
71
72
73
74
75
76
77
78
79
|
# File 'lib/opt_parse_validator/opts/base.rb', line 71
def normalize(value)
[*attrs[:normalize]].each do |method|
next unless method.is_a?(Symbol)
value = value.send(method) if value.respond_to?(method)
end
value
end
|
#required? ⇒ Boolean
30
31
32
|
# File 'lib/opt_parse_validator/opts/base.rb', line 30
def required?
@required ||= attrs[:required]
end
|
#required_unless ⇒ Object
34
35
36
|
# File 'lib/opt_parse_validator/opts/base.rb', line 34
def required_unless
@required_unless ||= [*attrs[:required_unless]]
end
|
#to_long ⇒ String
94
95
96
97
98
99
100
101
102
|
# File 'lib/opt_parse_validator/opts/base.rb', line 94
def to_long
option.each do |option_attr|
if option_attr =~ /^--/
return option_attr.gsub(/ .*$/, '')
.gsub(/\[[^\]]+\]/, '')
end
end
nil
end
|
#to_s ⇒ String
105
106
107
|
# File 'lib/opt_parse_validator/opts/base.rb', line 105
def to_s
to_sym.to_s
end
|
#to_sym ⇒ Symbol
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/opt_parse_validator/opts/base.rb', line 82
def to_sym
unless @symbol
long_option = to_long
fail Error, "Could not find option symbol for #{option}" unless long_option
@symbol = long_option.gsub(/^--/, '').tr('-', '_').to_sym
end
@symbol
end
|
#validate(value) ⇒ Object
54
55
56
57
58
59
60
|
# File 'lib/opt_parse_validator/opts/base.rb', line 54
def validate(value)
if value.nil? || value.to_s.empty?
fail Error, 'Empty option value supplied' if value_if_empty.nil?
return value_if_empty
end
value
end
|
#value_if_empty ⇒ Mixed
49
50
51
|
# File 'lib/opt_parse_validator/opts/base.rb', line 49
def value_if_empty
attrs[:value_if_empty]
end
|