Class: Argy::Parameter Abstract
- Inherits:
-
Object
- Object
- Argy::Parameter
- Defined in:
- lib/argy/parameter.rb
Overview
This class is abstract.
Subclasses must implement #label
Instance Attribute Summary collapse
-
#default ⇒ Object
readonly
The default value for the parameter.
-
#desc ⇒ String
readonly
The description for the parameter.
-
#name ⇒ String
readonly
The name of the parameter.
-
#type ⇒ String
readonly
The type of the parameter.
Instance Method Summary collapse
-
#coerce(value) ⇒ Object
Coerces a value to the correct type.
-
#initialize(name, desc: nil, type: :string, default: nil, required: false) ⇒ Parameter
constructor
Create a new Parameter.
-
#label ⇒ String
abstract
The display label for the paramter.
-
#required? ⇒ TrueClass, FalseClass
Check if the parameter is required.
-
#validate(value) ⇒ Object
Validates a value.
Constructor Details
#initialize(name, desc: nil, type: :string, default: nil, required: false) ⇒ Parameter
Create a new Parameter
29 30 31 32 33 34 35 |
# File 'lib/argy/parameter.rb', line 29 def initialize(name, desc: nil, type: :string, default: nil, required: false) @name = name @type = type @desc = desc @default = default @required = required end |
Instance Attribute Details
#default ⇒ Object (readonly)
The default value for the parameter
17 18 19 |
# File 'lib/argy/parameter.rb', line 17 def default @default end |
#desc ⇒ String (readonly)
The description for the parameter
21 22 23 |
# File 'lib/argy/parameter.rb', line 21 def desc @desc end |
#name ⇒ String (readonly)
The name of the parameter
9 10 11 |
# File 'lib/argy/parameter.rb', line 9 def name @name end |
#type ⇒ String (readonly)
The type of the parameter
13 14 15 |
# File 'lib/argy/parameter.rb', line 13 def type @type end |
Instance Method Details
#coerce(value) ⇒ Object
Coerces a value to the correct type.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/argy/parameter.rb', line 64 def coerce(value) case type when :string, :boolean value when :integer Integer(value) when :float Float(value) when :array value.split(",") when :pathname Pathname.new(value).(Dir.pwd) else raise "Invalid type: #{type.inspect}" unless type.respond_to?(:call) type.call(value) end rescue ArgumentError raise CoersionError, "`#{label}` received an invalid value" end |
#label ⇒ String
This method is abstract.
The display label for the paramter
40 41 42 |
# File 'lib/argy/parameter.rb', line 40 def label raise NotImplementedError, __method__ end |
#required? ⇒ TrueClass, FalseClass
Check if the parameter is required
46 47 48 |
# File 'lib/argy/parameter.rb', line 46 def required? @required end |
#validate(value) ⇒ Object
Validates a value.
53 54 55 56 57 58 59 |
# File 'lib/argy/parameter.rb', line 53 def validate(value) if required? && value.nil? raise ValidationError, "`#{label}` is a required parameter" end value end |