class Wrapture::ConstantSpec

A description of a constant.

Public Class Methods

new(spec) click to toggle source

Creates a constant spec based on the provided hash spec

The hash must have the following keys:

name

the name of the constant

type

the type of the constant

value

the value to assign to the constant

includes

a list of includes that need to be added in order for this

constant to be valid (for example, includes for the type and value).

The following keys are optional:

doc

a string containing the documentation for this constant

# File lib/wrapture/constant_spec.rb, line 50
def initialize(spec)
  @spec = ConstantSpec.normalize_spec_hash(spec)
  @doc = @spec.key?('doc') ? Comment.new(@spec['doc']) : nil
  @type = TypeSpec.new(@spec['type'])
end
normalize_spec_hash(spec) click to toggle source

Normalizes a hash specification of a constant. Normalization will check for things like invalid keys, duplicate entries in include lists, and will set missing keys to their default value (for example, an empty list if no includes are given).

# File lib/wrapture/constant_spec.rb, line 28
def self.normalize_spec_hash(spec)
  Comment.validate_doc(spec['doc']) if spec.key?('doc')

  normalized = spec.dup

  normalized['version'] = Wrapture.spec_version(spec)
  normalized['includes'] = Wrapture.normalize_includes spec['includes']

  normalized
end

Public Instance Methods

declaration() { |line| ... } click to toggle source

Yields each line of the declaration of this constant, including any documentation.

# File lib/wrapture/constant_spec.rb, line 68
def declaration
  @doc&.format_as_doxygen(max_line_length: 76) { |line| yield line }
  yield "static const #{@type.variable(@spec['name'])};"
end
declaration_includes() click to toggle source

A list of includes needed for the declaration of this constant.

# File lib/wrapture/constant_spec.rb, line 57
def declaration_includes
  @spec['includes'].dup
end
definition(class_name) click to toggle source

The definition of this constant.

# File lib/wrapture/constant_spec.rb, line 74
def definition(class_name)
  expanded_name = "#{class_name}::#{@spec['name']}"
  "const #{@spec['type']} #{expanded_name} = #{@spec['value']}"
end
definition_includes() click to toggle source

A list of includes needed for the definition of this constant.

# File lib/wrapture/constant_spec.rb, line 62
def definition_includes
  @spec['includes'].dup
end