Class: HypertextApplicationLanguage::Representation

Inherits:
Object
  • Object
show all
Defined in:
lib/hypertext_application_language/representation.rb

Overview

Represents a resource. This includes sub-resources which also have their own representation. Representations have links, properties and sub-resources.

Resource is the name of a representation embedded within another super-representation. Representations have zero or resources. They will appear in the rendered results as embedded resources.

Constant Summary collapse

'_links'.freeze
EMBEDDED =
'_embedded'.freeze

Instance Attribute Summary collapse

Namespaces collapse

Links collapse

Properties collapse

Representations collapse

Instance Method Summary collapse

Constructor Details

#initializeRepresentation

Returns a new instance of Representation.



24
25
26
27
28
29
# File 'lib/hypertext_application_language/representation.rb', line 24

def initialize
  @namespace_manager = NamespaceManager.new
  @links = []
  @properties = {}
  @representations_for_rel = {}
end

Instance Attribute Details

Array of links.



16
17
18
# File 'lib/hypertext_application_language/representation.rb', line 16

def links
  @links
end

#propertiesObject

Returns the value of attribute properties.



18
19
20
# File 'lib/hypertext_application_language/representation.rb', line 18

def properties
  @properties
end

#representations_for_relObject

Hash of string-array pairs. The arrays contain embedded representations, zero or more.



22
23
24
# File 'lib/hypertext_application_language/representation.rb', line 22

def representations_for_rel
  @representations_for_rel
end

Instance Method Details



44
45
46
# File 'lib/hypertext_application_language/representation.rb', line 44

def link
  link_for(Link::SELF_REL)
end


48
49
50
# File 'lib/hypertext_application_language/representation.rb', line 48

def link_for(href_or_rel)
  links_for(href_or_rel).first
end

Answers the representation’s links selected by either a hypertext reference or by a relation.



54
55
56
57
# File 'lib/hypertext_application_language/representation.rb', line 54

def links_for(href_or_rel)
  rel = @namespace_manager.curie(href_or_rel) || href_or_rel
  @links.select { |link| link.rel == rel }
end

#namespacesObject



33
34
35
# File 'lib/hypertext_application_language/representation.rb', line 33

def namespaces
  @namespace_manager.namespaces
end

#representationsObject

Takes the array values from the representations by relation, then flattens the array of arrays of representations. The result becomes an array of representations, all of them but without their relation to the super-representation that having been stripped away.



86
87
88
# File 'lib/hypertext_application_language/representation.rb', line 86

def representations
  @representations_for_rel.values.flatten
end

#value_for(name, default_value = nil) ⇒ Object



71
72
73
# File 'lib/hypertext_application_language/representation.rb', line 71

def value_for(name, default_value=nil)
  @properties[name] || default_value
end

Adds a link to this representation.

Ruby does not support argument overloading. If there is just one argument, assume that it is a Link instance. If not, if more than one argument, assume that they are String instances.



64
65
66
67
# File 'lib/hypertext_application_language/representation.rb', line 64

def with_link(*args)
  @links.push(args.length == 1 ? args.first : Link.new(*args))
  self
end

#with_namespace(name, ref) ⇒ Object



37
38
39
40
# File 'lib/hypertext_application_language/representation.rb', line 37

def with_namespace(name, ref)
  @namespace_manager.with_namespace(name, ref)
  self
end

#with_property(name, value) ⇒ Object



75
76
77
78
# File 'lib/hypertext_application_language/representation.rb', line 75

def with_property(name, value)
  @properties[name] = value
  self
end

#with_representation(rel, representation) ⇒ Object

Associates a given embedded representation with this representation by a given relation.



92
93
94
95
# File 'lib/hypertext_application_language/representation.rb', line 92

def with_representation(rel, representation)
  (@representations_for_rel[rel] ||= []).push(representation)
  self
end