Class: Dialekt::Util::CallSignature

Inherits:
Object
  • Object
show all
Defined in:
lib/dialekt/util/call_signature.rb

Overview

Call signature information for Proc objects

Defined Under Namespace

Classes: Parameter

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parameters:, extra_parameters:, options:, extra_options:) ⇒ CallSignature

Returns a new instance of CallSignature.



53
54
55
56
57
58
# File 'lib/dialekt/util/call_signature.rb', line 53

def initialize(parameters:, extra_parameters:, options:, extra_options:)
  @parameters = parameters.dup.freeze
  @extra_parameters = extra_parameters
  @options = options.dup.freeze
  @extra_options = extra_options
end

Instance Attribute Details

#extra_optionsObject (readonly)

Returns the value of attribute extra_options.



51
52
53
# File 'lib/dialekt/util/call_signature.rb', line 51

def extra_options
  @extra_options
end

#extra_parametersObject (readonly)

Returns the value of attribute extra_parameters.



51
52
53
# File 'lib/dialekt/util/call_signature.rb', line 51

def extra_parameters
  @extra_parameters
end

#optionsObject (readonly)

Returns the value of attribute options.



51
52
53
# File 'lib/dialekt/util/call_signature.rb', line 51

def options
  @options
end

#parametersObject (readonly)

Returns the value of attribute parameters.



51
52
53
# File 'lib/dialekt/util/call_signature.rb', line 51

def parameters
  @parameters
end

Class Method Details

.create(signature:) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/dialekt/util/call_signature.rb', line 26

def create(signature:)
  parameters = []
  options = {}
  extra_parameters = nil
  extra_options = nil

  signature.each do |type, name|
    case type
    when :req, :opt
      parameters << Parameter.new(name: name, optional: type == :opt)
    when :rest
      extra_parameters = Parameter.new(name: name, optional: true)
    when :keyreq, :key
      options[name] = Parameter.new(name: name, optional: type == :key)
    when :keyrest
      extra_options = Parameter.new(name: name, optional: true)
    else
      raise ArgumentError, "Illegal type #{type} in signature #{PP.singleline_pp(signature, StringIO.new).string}"
    end
  end

  new(parameters: parameters, extra_parameters: extra_parameters, options: options, extra_options: extra_options)
end

Instance Method Details

#optional_parameter_countObject



64
65
66
# File 'lib/dialekt/util/call_signature.rb', line 64

def optional_parameter_count
  @optional_parameter_count ||= @parameters.count(&:optional?)
end

#required_parameter_countObject



60
61
62
# File 'lib/dialekt/util/call_signature.rb', line 60

def required_parameter_count
  @required_parameter_count ||= @parameters.count { |p| !p.optional? }
end