Class: TypeDefinition

Inherits:
RestObject show all
Defined in:
lib/rally_rest_api/typedef.rb

Overview

:nodoc:

Instance Attribute Summary

Attributes inherited from RestObject

#rally_rest

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RestObject

#<=>, #==, #body, #delete, #elements, #eql?, #hash, #initialize, #marshal_dump, #marshal_load, #method_missing, #name, #oid, #parse_document, #ref, #refresh, #save!, #to_hash, #type, #type=, #typedef, #underscore, #update

Constructor Details

This class inherits a constructor from RestObject

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RestObject

Class Method Details

.cached_type_definition(workspace_domain_object, type = workspace_domain_object.type) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/rally_rest_api/typedef.rb', line 8

def self.cached_type_definition(workspace_domain_object, type = workspace_domain_object.type)
  key = make_key(workspace_domain_object.workspace, type)

  unless cached_type_definitions.key? key
    cached_type_definitions[key] = get_type_definition(workspace_domain_object.workspace, type)
  end
  cached_type_definitions[key]
end

.cached_type_definitionsObject



4
5
6
# File 'lib/rally_rest_api/typedef.rb', line 4

def self.cached_type_definitions
  @@cached_type_definitions ||= {}
end

.get_type_definition(workspace, type) ⇒ Object



17
18
19
# File 'lib/rally_rest_api/typedef.rb', line 17

def self.get_type_definition(workspace, type)
  get_type_definitions(workspace).find { |td| td.element_name == type }
end

.get_type_definitions(workspace) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/rally_rest_api/typedef.rb', line 21

def self.get_type_definitions(workspace)
  # This is a hack - having to do with parse_collections_as_hash?
  typedefs = case workspace.type_definitions
             when Hash then workspace.type_definitions.values.flatten
             when Array then workspace.type_definitions
      end
  # end hack  
  typedefs.map { |td| TypeDefinition.new(td.rally_rest, td.body) }
end

.make_key(workspace, type) ⇒ Object



31
32
33
34
35
# File 'lib/rally_rest_api/typedef.rb', line 31

def self.make_key(workspace, type)
  # Parent typedefs don't have a workspace, so key them appropriately
  ref = workspace.ref rescue ""
  type + ref
end

Instance Method Details

#attributes(include_parent = false) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rally_rest_api/typedef.rb', line 76

def attributes(include_parent = false)
  return symbol_keyed_attributes(self.elements[:attributes]) unless include_parent

  typedef = self
  all_attributes = {}
  until typedef.nil?
    all_attributes.merge! typedef.attributes(false)
    typedef = typedef.parent
  end
  all_attributes
end

#cached_parentObject



93
94
95
96
97
98
99
100
101
# File 'lib/rally_rest_api/typedef.rb', line 93

def cached_parent
  type_no_spaces = self.elements[:parent].name.gsub(" ", "")
  key = TypeDefinition.make_key(self.workspace, type_no_spaces)
  typedef = TypeDefinition.cached_type_definitions[key]
  if typedef.nil?
    typedef = TypeDefinition.cached_type_definitions[key] = TypeDefinition.new(self.rally_rest, self.elements[:parent].body)
  end
  typedef
end

#collect_attributes(include_parent = false) ⇒ Object



63
64
65
66
# File 'lib/rally_rest_api/typedef.rb', line 63

def collect_attributes(include_parent = false)
  values = self.attributes(include_parent).find_all { |element_name, attrdef| yield element_name, attrdef }
  Hash[*values.flatten]
end

#collection_attributes(include_parent = false) ⇒ Object



51
52
53
# File 'lib/rally_rest_api/typedef.rb', line 51

def collection_attributes(include_parent = false)
  collect_attributes(include_parent) { |element_name, attrdef| attrdef.attribute_type == "COLLECTION" }
end

#constrained_attributesObject



47
48
49
# File 'lib/rally_rest_api/typedef.rb', line 47

def constrained_attributes
  collect_attributes { |element_name, attrdef| attrdef.constrained == "true" }
end

#custom_attributesObject



37
38
39
# File 'lib/rally_rest_api/typedef.rb', line 37

def custom_attributes
  collect_attributes { |element_name, attrdef| attrdef.custom == "true" }
end

#custom_constrained_attributesObject Also known as: custom_dropdown_attributes

custom and constrained, i.e. a custom dropdown



42
43
44
# File 'lib/rally_rest_api/typedef.rb', line 42

def custom_constrained_attributes
  collect_attributes { |element_name, attrdef| attrdef.custom == "true" && attrdef.constrained == "true" }
end

#object_attributes(include_parent = false) ⇒ Object



55
56
57
# File 'lib/rally_rest_api/typedef.rb', line 55

def object_attributes(include_parent = false)
  collect_attributes(include_parent) { |element_name, attrdef|  attrdef.attribute_type == "OBJECT" }
end

#parentObject



88
89
90
91
# File 'lib/rally_rest_api/typedef.rb', line 88

def parent
  return nil if self.elements[:parent].nil?
  cached_parent
end

#reference_attributes(include_parent = false) ⇒ Object



59
60
61
# File 'lib/rally_rest_api/typedef.rb', line 59

def reference_attributes(include_parent = false)
  collection_attributes(include_parent).merge object_attributes(include_parent)
end

#symbol_keyed_attributes(attribute_hash) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/rally_rest_api/typedef.rb', line 68

def symbol_keyed_attributes(attribute_hash)
  return {} unless attribute_hash  # some typedefs will have no attributes
  attribute_hash.values.inject({}) do |hash, attrdef|
    hash[underscore(attrdef.element_name).intern] = AttributeDefinition.new(self.rally_rest, attrdef)
    hash
  end
end

#type_as_symbolObject



103
104
105
# File 'lib/rally_rest_api/typedef.rb', line 103

def type_as_symbol
  underscore(element_name).intern
end