class Wrapture::EnumSpec
A description of an enumeration.
Public Class Methods
new(spec)
click to toggle source
Creates an enumeration specification based on the provided hash spec.
# File lib/wrapture/enum_spec.rb, line 54 def initialize(spec) @spec = EnumSpec.normalize_spec_hash(spec) @doc = Comment.new(@spec.fetch('doc', nil)) end
normalize_spec_hash(spec)
click to toggle source
Returns a normalized copy of a hash specification of an enumeration in place. See normalize_spec_hash
! for details.
# File lib/wrapture/enum_spec.rb, line 26 def self.normalize_spec_hash(spec) normalize_spec_hash!(Marshal.load(Marshal.dump(spec))) end
normalize_spec_hash!(spec)
click to toggle source
Normalizes a hash specification of an enumeration in place. Normalization will remove duplicate entries in include lists and check for a name key.
# File lib/wrapture/enum_spec.rb, line 32 def self.normalize_spec_hash!(spec) unless spec.key?('name') raise MissingSpecKey, 'a name is required for enumerations' end if spec.key?('elements') unless spec['elements'].is_a?(Array) raise InvalidSpecKey, 'the elements key must be an array' end else raise MissingSpecKey, 'elements are required for enumerations' end spec['includes'] = Wrapture.normalize_includes(spec['includes']) spec['elements'].each do |element| element['includes'] = Wrapture.normalize_includes(element['includes']) end spec end
Public Instance Methods
generate_wrapper()
click to toggle source
Generates the wrapper definition file.
# File lib/wrapture/enum_spec.rb, line 61 def generate_wrapper filename = "#{@spec['name']}.hpp" File.open(filename, 'w') do |file| definition_contents do |line| file.puts(line) end end [filename] end
name()
click to toggle source
The name of the enumeration.
# File lib/wrapture/enum_spec.rb, line 74 def name @spec['name'] end
Private Instance Methods
definition_contents() { |"#ifndef #{header_guard}"| ... }
click to toggle source
Yields each line of the definition of the wrapper for this enum.
# File lib/wrapture/enum_spec.rb, line 81 def definition_contents indent = 0 yield "#ifndef #{header_guard}" yield "#define #{header_guard}" yield definition_includes.each { |filename| yield "#include <#{filename}>" } yield if @spec.key?('namespace') yield "namespace #{@spec['namespace']} {" yield indent += 2 end @doc.format_as_doxygen(max_line_length: 76) do |line| yield "#{' ' * indent}#{line}" end yield "#{' ' * indent}enum class #{name} {" indent += 2 elements = @spec['elements'] elements[0...-1].each do |element| element_doc(element) { |line| yield "#{' ' * indent}#{line}" } element_definition(element) { |line| yield "#{' ' * indent}#{line}," } end element_doc(elements.last) { |line| yield "#{' ' * indent}#{line}" } element_definition(elements.last) do |line| yield "#{' ' * indent}#{line}" end indent -= 2 yield "#{' ' * indent}};" yield yield '}' if @spec.key?('namespace') yield yield "#endif /* #{header_guard} */" end
definition_includes()
click to toggle source
A list of the includes needed for the definition of the enumeration.
# File lib/wrapture/enum_spec.rb, line 124 def definition_includes includes = @spec['includes'].dup @spec['elements'].each do |element| includes.concat(element['includes']) end includes.uniq end
element_definition(element) { |"#{element} = #{element}"| ... }
click to toggle source
Yields each line of the definition of an element.
# File lib/wrapture/enum_spec.rb, line 135 def element_definition(element) if element.key?('value') yield "#{element['name']} = #{element['value']}" else yield element['name'] end end
element_doc(element, &block)
click to toggle source
Calls the given block once for each line of the documentation for an element.
# File lib/wrapture/enum_spec.rb, line 145 def element_doc(element, &block) doc = Comment.new(element.fetch('doc', nil)) doc.format_as_doxygen(max_line_length: 74) { |line| block.call(line) } end
header_guard()
click to toggle source
The header guard for the enumeration.
# File lib/wrapture/enum_spec.rb, line 151 def header_guard "__#{@spec['name'].upcase}_HPP" end