Class: OptParseValidator::OptBase
- Inherits:
-
Object
- Object
- OptParseValidator::OptBase
- Defined in:
- lib/opt_parse_validator/opts/base.rb
Overview
Base Option This Option should not be called, children should be used.
Direct Known Subclasses
OptArray, OptBoolean, OptChoice, OptCredentials, OptInteger, OptIntegerRange, OptPath, OptString, OptURI
Instance Attribute Summary collapse
-
#attrs ⇒ Object
readonly
Returns the value of attribute attrs.
-
#option ⇒ Object
readonly
Returns the value of attribute option.
-
#required ⇒ Object
writeonly
Sets the attribute required.
Instance Method Summary collapse
- #choices ⇒ Array<Mixed>
- #default ⇒ Mixed
-
#initialize(option, attrs = {}) ⇒ OptBase
constructor
A new instance of OptBase.
- #normalize(value) ⇒ Mixed
- #required? ⇒ Boolean
- #required_unless ⇒ Object
-
#to_long ⇒ String
The raw long option (e.g: –proxy).
- #to_s ⇒ String
- #to_sym ⇒ Symbol
- #validate(value) ⇒ Object
- #value_if_empty ⇒ Mixed
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 |
# File 'lib/opt_parse_validator/opts/base.rb', line 17 def initialize(option, attrs = {}) @option = option @attrs = attrs end |
Instance Attribute Details
#attrs ⇒ Object (readonly)
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 (readonly)
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 (writeonly)
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
#choices ⇒ Array<Mixed>
37 38 39 |
# File 'lib/opt_parse_validator/opts/base.rb', line 37 def choices attrs[:choices] end |
#default ⇒ Mixed
32 33 34 |
# File 'lib/opt_parse_validator/opts/base.rb', line 32 def default attrs[:default] end |
#normalize(value) ⇒ Mixed
64 65 66 67 68 69 70 71 72 |
# File 'lib/opt_parse_validator/opts/base.rb', line 64 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
23 24 25 |
# File 'lib/opt_parse_validator/opts/base.rb', line 23 def required? @required ||= attrs[:required] end |
#required_unless ⇒ Object
27 28 29 |
# File 'lib/opt_parse_validator/opts/base.rb', line 27 def required_unless @required_unless ||= [*attrs[:required_unless]] end |
#to_long ⇒ String
Returns The raw long option (e.g: –proxy).
87 88 89 90 91 92 93 94 95 |
# File 'lib/opt_parse_validator/opts/base.rb', line 87 def to_long option.each do |option_attr| if option_attr =~ /^--/ return option_attr.gsub(/ .*$/, '') .gsub(/\[[^\]]+\]/, '') end end nil end |
#to_s ⇒ String
98 99 100 |
# File 'lib/opt_parse_validator/opts/base.rb', line 98 def to_s to_sym.to_s end |
#to_sym ⇒ Symbol
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/opt_parse_validator/opts/base.rb', line 75 def to_sym unless @symbol long_option = to_long fail "Could not find option symbol for #{option}" unless long_option @symbol = long_option.gsub(/^--/, '').gsub(/-/, '_').to_sym end @symbol end |
#validate(value) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/opt_parse_validator/opts/base.rb', line 47 def validate(value) if value.nil? || value.to_s.empty? fail 'Empty option value supplied' if value_if_empty.nil? return value_if_empty end value end |
#value_if_empty ⇒ Mixed
42 43 44 |
# File 'lib/opt_parse_validator/opts/base.rb', line 42 def value_if_empty attrs[:value_if_empty] end |