Class: Coco::Options::Option
- Inherits:
-
Object
- Object
- Coco::Options::Option
- Includes:
- Item
- Defined in:
- lib/coco/options/option.rb
Instance Attribute Summary collapse
-
#default ⇒ Object
Returns the value of attribute default.
-
#from ⇒ Object
readonly
Returns the value of attribute from.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#initialize(name, required: false, value: :undefined, from: nil, **kwargs) ⇒ Option
constructor
A new instance of Option.
- #required? ⇒ Boolean
- #undefined? ⇒ Boolean
- #validate_required! ⇒ Object
- #value ⇒ Object
- #value=(val) ⇒ Object
Methods included from Item
#alias, #html_alias, #private?, #public?
Constructor Details
#initialize(name, required: false, value: :undefined, from: nil, **kwargs) ⇒ Option
Returns a new instance of Option.
9 10 11 12 13 14 15 16 17 |
# File 'lib/coco/options/option.rb', line 9 def initialize(name, required: false, value: :undefined, from: nil, **kwargs) @name = name.to_sym @required = required @value = value @from = Array(from) @private = kwargs.fetch(:private, false) @default = kwargs.fetch(:default, nil) @alias = kwargs.fetch(:alias, nil) end |
Instance Attribute Details
#default ⇒ Object
Returns the value of attribute default.
6 7 8 |
# File 'lib/coco/options/option.rb', line 6 def default @default end |
#from ⇒ Object (readonly)
Returns the value of attribute from.
7 8 9 |
# File 'lib/coco/options/option.rb', line 7 def from @from end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/coco/options/option.rb', line 7 def name @name end |
Instance Method Details
#required? ⇒ Boolean
25 26 27 |
# File 'lib/coco/options/option.rb', line 25 def required? @required == true end |
#undefined? ⇒ Boolean
29 30 31 |
# File 'lib/coco/options/option.rb', line 29 def undefined? @value == :undefined end |
#validate_required! ⇒ Object
19 20 21 22 23 |
# File 'lib/coco/options/option.rb', line 19 def validate_required! if required? && undefined? raise ArgumentError, "The `#{name}` option requires a value" end end |
#value ⇒ Object
33 34 35 |
# File 'lib/coco/options/option.rb', line 33 def value undefined? ? default : @value end |
#value=(val) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/coco/options/option.rb', line 37 def value=(val) if from.any? && !from.include?(val) raise ArgumentError, "`#{val}` is not a valid #{name} value.\n\tAvailable options: #{from}" end @value = val end |