Repository navigation
Expand file tree
/
Copy pathresource_builder.rb
More file actions
83 lines (72 loc) · 2.3 KB
/
Copy pathresource_builder.rb
File metadata and controls
83 lines (72 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
class Representor
class ResourceBuilder
def initialize(resource_struct = nil)
@internal_struct = resource_struct || ResourceStruct.new
end
##
# Adds a meta link associated with the resource.
#
def add_meta_link(relation_type, uri)
@internal_struct[:meta_links][relation_type] = { href: uri }
end
##
# Adds metadata associated with the resource.
#
def add_meta_data(name, value)
@internal_struct[:meta_data][name, value]
end
##
# Adds a resource attribute.
#
def add_attribute(name, value)
@internal_struct[:attributes][name] = value
end
##
# Adds a resource transition. Yields a transition builder for more complex transitions.
#
def add_transition(relation_type, uri, options = {}, &block)
@internal_struct[:transitions][relation_type] = Transition.new(relation_type, uri, options, &block)
end
##
# Adds a resource transition as an element of an array transitions. Yields a transition builder for more
# complex transitions.
#
def add_transitions(relation_type, uri, options = {}, &block)
@internal_struct[:transtions][relation_type] ||= []
@internal_struct[:transtions][relation_type] << add_transition(relation_type, uri, options, &block)
end
##
# Adds an embedded resource. Yields a resource builder.
#
def add_resource(name, &block)
@internal_struct[:resources][name] = Representor.new(&block)
end
##
# Adds an embedded resource as an element of an array of resources. Yields a resource builder.
#
def add_resources(name, &block)
@internal_struct[:resources][name] ||= []
@internal_struct[:resources][name] << add_resource(name, &block)
end
##
# @return [Struct] The raw struct of the constructed for the resource.
def to_struct
@internal_struct.dup
end
##
# @return [Representor] A Hypermedia resource instance associated with the current state of the builder.
def to_resource
Representor.new(to_struct)
end
end
end
# @private
ResourceStruct = Struct.new(:meta_links, :meta_data, :attibutes, :transitions, :resources) do
def initialize
self[:meta_links] = {}
self[:meta_data] = {}
self[:attibutes] = {}
self[:transitions] = {}
self[:resources] = {}
end
end