Class: PuppetStrings::Yard::CodeObjects::DataType

Inherits:
Base
  • Object
show all
Defined in:
lib/puppet-strings/yard/code_objects/data_type.rb

Overview

Implements the Puppet DataType code object.

Instance Method Summary collapse

Methods inherited from Base

new

Constructor Details

#initialize(name) ⇒ void

Initializes a Puppet class code object.

Parameters:

  • The (String)

    name of the Data Type



27
28
29
30
# File 'lib/puppet-strings/yard/code_objects/data_type.rb', line 27

def initialize(name)
  super(PuppetStrings::Yard::CodeObjects::DataTypes.instance, name)
  @defaults = {}
end

Instance Method Details

#add_function(name, return_type, parameter_types) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/puppet-strings/yard/code_objects/data_type.rb', line 65

def add_function(name, return_type, parameter_types)
  meth_obj = YARD::CodeObjects::MethodObject.new(self, name, :class)

  # Add return tag
  meth_obj.add_tag(YARD::Tags::Tag.new(:return, '', return_type))

  # Add parameters
  parameter_types.each_with_index do |param_type, index|
    meth_obj.add_tag(YARD::Tags::Tag.new(:param, '', [param_type], "param#{index + 1}"))
  end

  meths << meth_obj
end

#add_parameter(name, type, default) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/puppet-strings/yard/code_objects/data_type.rb', line 45

def add_parameter(name, type, default)
  tag = docstring.tags(:param).find { |item| item.name == name }
  if tag.nil?
    tag = YARD::Tags::Tag.new(:param, '', nil, name)
    docstring.add_tag(tag)
  end
  type = [type] unless type.is_a?(Array)
  tag.types = type if tag.types.nil?
  set_parameter_default(name, default)
end

#functionsObject



79
80
81
# File 'lib/puppet-strings/yard/code_objects/data_type.rb', line 79

def functions
  meths
end

#parametersObject



61
62
63
# File 'lib/puppet-strings/yard/code_objects/data_type.rb', line 61

def parameters
  docstring.tags(:param).map { |tag| [tag.name, defaults[tag.name]] }
end

#set_parameter_default(param_name, default) ⇒ Object



56
57
58
59
# File 'lib/puppet-strings/yard/code_objects/data_type.rb', line 56

def set_parameter_default(param_name, default)
  defaults.delete(param_name)
  defaults[param_name] = default unless default.nil?
end

#sourceObject

Gets the source of the code object.

Returns:

  • Returns the source of the code object.



40
41
42
43
# File 'lib/puppet-strings/yard/code_objects/data_type.rb', line 40

def source
  # Not implemented, but would be nice!
  nil
end

#to_hashHash

Converts the code object to a hash representation.

Returns:

  • (Hash)

    Returns a hash representation of the code object.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/puppet-strings/yard/code_objects/data_type.rb', line 85

def to_hash
  hash = {}
  hash[:name] = name
  hash[:file] = file
  hash[:line] = line
  hash[:docstring] = PuppetStrings::Yard::Util.docstring_to_hash(docstring, %i[param option enum return example])
  hash[:defaults] = defaults unless defaults.nil? || defaults.empty?
  hash[:source] = source unless source.nil? || source.empty?
  hash[:functions] = functions.map do |func|
    {
      name: func.name,
      signature: func.signature,
      docstring: PuppetStrings::Yard::Util.docstring_to_hash(func.docstring, %i[param option enum return example])
    }
  end
  hash
end

#typeObject

Gets the type of the code object.

Returns:

  • Returns the type of the code object.



34
35
36
# File 'lib/puppet-strings/yard/code_objects/data_type.rb', line 34

def type
  :puppet_data_type
end