Class: Inferior::Core::Parameter
- Inherits:
-
Object
- Object
- Inferior::Core::Parameter
- Defined in:
- lib/inferior/core/parameter.rb
Defined Under Namespace
Classes: UnsupportedTypeError
Constant Summary collapse
- REQUIRED =
:required- OPTIONAL =
:optional- SPLAT =
:splat- KEYWORD_REQUIRED =
:keyword_required- KEYWORD_OPTIONAL =
:keyword_optional- KEYWORD_SPLAT =
:keyword_splat- BLOCK =
:block- TYPES =
Set[ REQUIRED, OPTIONAL, SPLAT, KEYWORD_REQUIRED, KEYWORD_OPTIONAL, KEYWORD_SPLAT, BLOCK, ].freeze
- MAPPING =
{ req: REQUIRED, opt: OPTIONAL, rest: SPLAT, keyreq: KEYWORD_REQUIRED, key: KEYWORD_OPTIONAL, keyrest: KEYWORD_SPLAT, block: BLOCK, }.freeze
- Error =
Class.new(Error)
- InvalidNameError =
Class.new(Error)
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #definition ⇒ Object
- #eql?(other) ⇒ Boolean
-
#initialize(name:, type:) ⇒ Parameter
constructor
A new instance of Parameter.
Constructor Details
#initialize(name:, type:) ⇒ Parameter
Returns a new instance of Parameter.
82 83 84 85 |
# File 'lib/inferior/core/parameter.rb', line 82 def initialize(name:, type:) @name = name @type = type end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
80 81 82 |
# File 'lib/inferior/core/parameter.rb', line 80 def name @name end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
80 81 82 |
# File 'lib/inferior/core/parameter.rb', line 80 def type @type end |
Class Method Details
.build!(name:, type:) ⇒ Object
56 57 58 59 60 |
# File 'lib/inferior/core/parameter.rb', line 56 def build!(name:, type:) validate_type!(type) validate_name!(name, type) new(name: name, type: type) end |
.parse!(ruby_internal_type, ruby_internal_name) ⇒ Object
62 63 64 65 |
# File 'lib/inferior/core/parameter.rb', line 62 def parse!(ruby_internal_type, ruby_internal_name) type = MAPPING.fetch(ruby_internal_type) build!(type: type, name: ruby_internal_name) end |
Instance Method Details
#definition ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/inferior/core/parameter.rb', line 91 def definition case type when REQUIRED name when OPTIONAL "#{name} = DEFAULT_VALUE" when SPLAT ["*", name].compact.join when KEYWORD_REQUIRED "#{name}:" when KEYWORD_OPTIONAL "#{name}: DEFAULT_VALUE" when KEYWORD_SPLAT ["**", name].compact.join when BLOCK ["&", name == "&" ? nil : name].compact.join end end |
#eql?(other) ⇒ Boolean
87 88 89 |
# File 'lib/inferior/core/parameter.rb', line 87 def eql?(other) name.eql?(other.name) && type.eql?(other.name) end |