Class: Inferior::Core::Parameter

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#nameObject (readonly)

Returns the value of attribute name.



80
81
82
# File 'lib/inferior/core/parameter.rb', line 80

def name
  @name
end

#typeObject (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

#definitionObject



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

Returns:

  • (Boolean)


87
88
89
# File 'lib/inferior/core/parameter.rb', line 87

def eql?(other)
  name.eql?(other.name) && type.eql?(other.name)
end