Class: HalClient::RepresentationSet
- Inherits:
-
Object
- Object
- HalClient::RepresentationSet
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/hal_client/representation_set.rb
Overview
A collection HAL representations
Instance Method Summary collapse
-
#form(form_id = "default") ⇒ Object
Returns the specified ‘Form`.
-
#initialize(reprs) ⇒ RepresentationSet
constructor
A new instance of RepresentationSet.
-
#patch(data, options = {}) ⇒ Object
Patch a ‘Representation` or `String` to the resource.
-
#post(data, options = {}) ⇒ Object
Post a ‘Representation` or `String` to the resource.
-
#put(data, options = {}) ⇒ Object
Put a ‘Representation` or `String` to the resource.
-
#related(link_rel, options = {}) ⇒ Object
Returns representations of resources related via the specified link rel or the specified default value.
-
#related?(link_rel) ⇒ Boolean
(also: #has_related?)
Returns true if any member representation contains a link (including embedded links) whose rel is ‘link_rel`.
Constructor Details
#initialize(reprs) ⇒ RepresentationSet
Returns a new instance of RepresentationSet.
10 11 12 |
# File 'lib/hal_client/representation_set.rb', line 10 def initialize(reprs) @reprs = reprs end |
Instance Method Details
#form(form_id = "default") ⇒ Object
Returns the specified ‘Form`
form_id - the string or symbol id of the form of interest. Default: ‘“default”`
Raises ‘KeyError` if the specified form doesn’t exist, or if there are duplicates.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/hal_client/representation_set.rb', line 81 def form(form_id="default") self .map { |r| begin r.form(form_id) rescue KeyError nil end } .compact .tap do |fs| raise KeyError, "Duplicate `#{form_id}` forms exist" if fs.count > 1 end .first end |
#patch(data, options = {}) ⇒ Object
Patch a ‘Representation` or `String` to the resource.
NOTE: This only works for a single representation.
data - a ‘String` or an object that responds to `#to_hal` options - set of options to pass to `HalClient#patch`
70 71 72 73 |
# File 'lib/hal_client/representation_set.rb', line 70 def patch(data, ={}) raise NotImplementedError, "We only patchs to singular resources." if count > 1 first.patch(data, ) end |
#post(data, options = {}) ⇒ Object
Post a ‘Representation` or `String` to the resource.
NOTE: This only works for a single representation.
data - a ‘String` or an object that responds to `#to_hal` options - set of options to pass to `HalClient#post`
48 49 50 51 |
# File 'lib/hal_client/representation_set.rb', line 48 def post(data, ={}) raise NotImplementedError, "We only posts to singular resources." if count > 1 first.post(data, ) end |
#put(data, options = {}) ⇒ Object
Put a ‘Representation` or `String` to the resource.
NOTE: This only works for a single representation.
data - a ‘String` or an object that responds to `#to_hal` options - set of options to pass to `HalClient#put`
59 60 61 62 |
# File 'lib/hal_client/representation_set.rb', line 59 def put(data, ={}) raise NotImplementedError, "We only puts to singular resources." if count > 1 first.put(data, ) end |
#related(link_rel, options = {}) ⇒ Object
Returns representations of resources related via the specified
link rel or the specified default value.
name_or_rel - The name of property or link rel of interest options - optional keys and values with which to expand any
templated links that are encountered
default_proc - an option proc that will be called with ‘name`
to produce default value if the specified property or link does not
exist
Raises KeyError if the specified link does not exist
and no default_proc is provided.
28 29 30 31 |
# File 'lib/hal_client/representation_set.rb', line 28 def (link_rel, ={}) raise KeyError unless link_rel RepresentationSet.new flat_map{|it| it.(link_rel, ){[]}.to_a } end |
#related?(link_rel) ⇒ Boolean Also known as:
Returns true if any member representation contains a link (including embedded links) whose rel is ‘link_rel`.
link_rel - The link rel of interest
37 38 39 |
# File 'lib/hal_client/representation_set.rb', line 37 def (link_rel) any? {|it| it.(link_rel) } end |