Class: YARD::CodeObjects::Chef::AttributeObject

Inherits:
ChefObject
  • Object
show all
Defined in:
lib/yard-chef/code_objects/attribute_object.rb

Overview

An AttributeObject represents a cookbook or a resource attribute. See wiki.opscode.com/display/chef/Attributes

Instance Attribute Summary

Attributes inherited from ChefObject

#docstring_type

Instance Method Summary collapse

Methods inherited from ChefObject

#children_by_type, #cookbooks, register, register_element

Constructor Details

#initialize(namespace, name) ⇒ AttributeObject

Creates a new instance of the AttributeObject.

belongs

Parameters:

  • namespace (NamespaceObject)

    namespace to which the attribute

  • name (String)

    name of the attribute



40
41
42
43
44
# File 'lib/yard-chef/code_objects/attribute_object.rb', line 40

def initialize(namespace, name)
  super(namespace, name)
  @kind_of = ''
  @default = ''
end

Instance Method Details

#to_jsonString

Returns JSON representation of attribute name

Returns:

  • (String)

    JSON string



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/yard-chef/code_objects/attribute_object.rb', line 49

def to_json
  json_string = "    {\n"
  # default[:cluster][:config][:openstack][:volume_default_type]
  if name =~ /(.+?)(\[.+\])/
    # "default"
    attribute_type = Regexp.last_match(1)
    # [:cluster][:config][:openstack][:volume_default_type]
    json_string += "      \"#{attribute_type}_attributes\": {\n      ...\n"
    deepness = 2
    attrs_tree = Regexp.last_match(2).split('[')
    while (attr = attrs_tree.shift)
      next if attr.empty?
      attr = attr.gsub(/[:"'](.+?)["']?\]/, '\\1')
      # Indent
      json_string += (' ' * (deepness + 2) * 2)
      # Attr name
      json_string += "\"#{attr}\""
      if attrs_tree.empty?
        json_string += ": \"VALUE\"\n"
      else
        # New branch
        json_string += ": {\n" unless attrs_tree.empty?
        deepness += 1
      end
    end

    # Closing brackets
    deepness -= 1
    deepness.times do |d|
      # Indent
      json_string += (' ' * (deepness - d + 2) * 2)
      # Attr name
      json_string += "}\n"
    end
  end
  json_string + "      ...\n    }\n"
end