Class: Wrapture::ParamSpec
- Inherits:
-
Object
- Object
- Wrapture::ParamSpec
- Defined in:
- lib/wrapture/param_spec.rb
Overview
A description of a parameter used in a generated function.
Instance Attribute Summary collapse
-
#type ⇒ Object
readonly
The type of the parameter.
Class Method Summary collapse
-
.new_list(spec_list) ⇒ Object
Returns a list of new ParamSpecs based on the provided array of parameter specification hashes.
-
.normalize_param_list(spec_list) ⇒ Object
Returns a normalized copy of a list of parameter hash specifications in place.
-
.normalize_spec_hash(spec) ⇒ Object
Returns a normalized copy of the hash specification of a parameter in
spec. -
.normalize_spec_hash!(spec) ⇒ Object
Normalizes the hash specification of a parameter in
specin place. -
.signature(param_list, owner) ⇒ Object
A string with a comma-separated list of parameters (using resolved type) and names, fit for use in a function signature or declaration.
Instance Method Summary collapse
-
#doc ⇒ Object
A Comment holding the parameter documentation.
-
#includes ⇒ Object
A list of includes needed for this parameter.
-
#initialize(spec) ⇒ ParamSpec
constructor
Creates a parameter specification based on the provided hash spec.
-
#name ⇒ Object
The name of the parameter.
-
#signature(owner) ⇒ Object
The parameter type and name, suitable for use in a function signature or declaration.
-
#variadic? ⇒ Boolean
True if this parameter is variadic (the name is equal to ‘…’).
Constructor Details
#initialize(spec) ⇒ ParamSpec
Creates a parameter specification based on the provided hash spec.
96 97 98 99 |
# File 'lib/wrapture/param_spec.rb', line 96 def initialize(spec) @spec = ParamSpec.normalize_spec_hash(spec) @type = TypeSpec.new(@spec['type']) end |
Instance Attribute Details
#type ⇒ Object (readonly)
The type of the parameter.
93 94 95 |
# File 'lib/wrapture/param_spec.rb', line 93 def type @type end |
Class Method Details
.new_list(spec_list) ⇒ Object
Returns a list of new ParamSpecs based on the provided array of parameter specification hashes.
28 29 30 |
# 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) ⇒ Object
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.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# 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) ⇒ Object
Returns a normalized copy of the hash specification of a parameter in spec. See normalize_spec_hash! for details.
59 60 61 |
# 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) ⇒ Object
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.
66 67 68 69 70 71 72 73 74 75 76 77 78 |
# 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) ⇒ Object
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.
84 85 86 87 88 89 90 |
# 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 |
Instance Method Details
#doc ⇒ Object
A Comment holding the parameter documentation.
102 103 104 105 106 107 108 |
# 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 ⇒ Object
A list of includes needed for this parameter.
111 112 113 |
# File 'lib/wrapture/param_spec.rb', line 111 def includes @spec['includes'].dup.concat(@type.includes) end |
#name ⇒ Object
The name of the parameter.
116 117 118 |
# File 'lib/wrapture/param_spec.rb', line 116 def name @spec['name'] end |
#signature(owner) ⇒ Object
The parameter type and name, suitable for use in a function signature or declaration. owner must be the FunctionSpec that the parameter belongs to.
123 124 125 |
# File 'lib/wrapture/param_spec.rb', line 123 def signature(owner) @type.resolve(owner).variable(name) end |
#variadic? ⇒ Boolean
True if this parameter is variadic (the name is equal to ‘…’).
128 129 130 |
# File 'lib/wrapture/param_spec.rb', line 128 def variadic? @type.variadic? end |