class YARD::CodeObjects::Chef::AttributeObject

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

Public Class Methods

new(namespace, name) click to toggle source

Creates a new instance of the AttributeObject.

@param namespace [NamespaceObject] namespace to which the attribute belongs @param name [String] name of the attribute

@return [AttributeObject] the newly created AttribteObject

Calls superclass method
# File lib/yard-chef/code_objects/attribute_object.rb, line 40
def initialize(namespace, name)
  super(namespace, name)
  @kind_of = ''
  @default = ''
end

Public Instance Methods

to_json() click to toggle source

Returns JSON representation of attribute name

@return [String] JSON string

# 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