class Wrapture::ParamSpec

A description of a parameter used in a generated function.

Attributes

type[R]

The type of the parameter.

Public Class Methods

new(spec) click to toggle source

Creates a parameter specification based on the provided hash spec.

# File lib/wrapture/param_spec.rb, line 96
def initialize(spec)
  @spec = ParamSpec.normalize_spec_hash(spec)
  @type = TypeSpec.new(@spec['type'])
end
new_list(spec_list) click to toggle source

Returns a list of new ParamSpecs based on the provided array of parameter specification hashes.

# File lib/wrapture/param_spec.rb, line 28
def self.new_list(spec_list)
  spec_list.map { |spec| new(spec) }
end
normalize_param_list(spec_list) click to toggle source

Returns a normalized copy of a list of parameter hash specifications in place.

Multiple variadic parameters (named '…') will be removed and only the first used. If the variadic parameter is not last, it will be moved to the end of the list.

# File lib/wrapture/param_spec.rb, line 38
def self.normalize_param_list(spec_list)
  if spec_list.nil?
    []
  elsif spec_list.count { |spec| spec['name'] == '...' }.zero?
    spec_list.map { |spec| normalize_spec_hash(spec) }
  else
    error_msg = "'...' may not be the only parameter"
    raise(InvalidSpecKey, error_msg) if spec_list.count == 1

    i = spec_list.find_index { |spec| spec['name'] == '...' }
    var = spec_list[i]

    spec_list
      .reject { |spec| spec['name'] == '...' }
      .map { |spec| normalize_spec_hash(spec) }
      .push(var)
  end
end
normalize_spec_hash(spec) click to toggle source

Returns a normalized copy of the hash specification of a parameter in spec. See normalize_spec_hash! for details.

# File lib/wrapture/param_spec.rb, line 59
def self.normalize_spec_hash(spec)
  normalize_spec_hash!(Marshal.load(Marshal.dump(spec)))
end
normalize_spec_hash!(spec) click to toggle source

Normalizes the hash specification of a parameter in spec in place. Normalization will remove duplicate entries from include lists and validate that required key values are set.

# File lib/wrapture/param_spec.rb, line 66
def self.normalize_spec_hash!(spec)
  Comment.validate_doc(spec['doc']) if spec.key?('doc')
  spec['includes'] = Wrapture.normalize_includes(spec['includes'])

  spec['type'] = '...' if spec['name'] == '...'

  unless spec.key?('type')
    missing_type_msg = 'parameters must have a type key defined'
    raise(MissingSpecKey, missing_type_msg)
  end

  spec
end
signature(param_list, owner) click to toggle source

A string with a comma-separated list of parameters (using resolved type) and names, fit for use in a function signature or declaration. param_list must be a list of ParamSpec instances, and owner must be the FunctionSpec that the parameters belong to.

# File lib/wrapture/param_spec.rb, line 84
def self.signature(param_list, owner)
  if param_list.empty?
    'void'
  else
    param_list.map { |param| param.signature(owner) }.join(', ')
  end
end

Public Instance Methods

doc() click to toggle source

A Comment holding the parameter documentation.

# File lib/wrapture/param_spec.rb, line 102
def doc
  if @spec.key?('doc')
    Comment.new("@param #{@spec['name']} #{@spec['doc']}")
  else
    Comment.new
  end
end
includes() click to toggle source

A list of includes needed for this parameter.

# File lib/wrapture/param_spec.rb, line 111
def includes
  @spec['includes'].dup.concat(@type.includes)
end
name() click to toggle source

The name of the parameter.

# File lib/wrapture/param_spec.rb, line 116
def name
  @spec['name']
end
signature(owner) click to toggle source

The parameter type and name, suitable for use in a function signature or declaration. owner must be the FunctionSpec that the parameter belongs to.

# File lib/wrapture/param_spec.rb, line 123
def signature(owner)
  @type.resolve(owner).variable(name)
end
variadic?() click to toggle source

True if this parameter is variadic (the name is equal to '…').

# File lib/wrapture/param_spec.rb, line 128
def variadic?
  @type.variadic?
end