Class: HalClient::RepresentationEditor
- Inherits:
-
Object
- Object
- HalClient::RepresentationEditor
- Extended by:
- Forwardable
- Defined in:
- lib/hal_client/representation_editor.rb
Overview
Provides ability to edit a representation. Editing a representation is useful in writable APIs as a way to update resources.
This class will not actually modify the underlying representation in any way.
Example:
“‘ruby
altered_doc = HalClient::RepresentationEditor.new(some_doc)
.reject_relate("author") { |it| it["name"] = "John Plagiarist" }
“‘
Instance Method Summary collapse
-
#add_link(rel, target, opts = {}) ⇒ Object
Returns a RepresentationEditor exactly like this one except that is has an additional link to the specified target with the specified rel.
-
#initialize(a_representation, raw = a_representation.raw) ⇒ RepresentationEditor
constructor
Initialize a new representation editor.
-
#reject_embedded(rel, &blk) ⇒ Object
Returns a RepresentationEditor for a representation like the current one but without the specified embedded resources.
-
#reject_links(rel, &blk) ⇒ Object
Returns a RepresentationEditor for a representation like the current one but without the specified links.
-
#reject_related(rel, &blk) ⇒ Object
Returns a RepresentationEditor for a representation like the current one but without the specified links and/or embeddeds.
-
#set_property(key, value) ⇒ Object
Returns a RepresentationEditor exactly like this one except that is has an new or overwritten property value.
-
#to_json ⇒ Object
(also: #to_hal)
Returns the raw json representation of this representation.
Constructor Details
#initialize(a_representation, raw = a_representation.raw) ⇒ RepresentationEditor
Initialize a new representation editor.
a_representation - The representation from which you want to
start. This object will *not* be modified!
raw - Not for public use! Used internally for handling multi-
staged changes.
28 29 30 31 |
# File 'lib/hal_client/representation_editor.rb', line 28 def initialize(a_representation, raw = a_representation.raw) @orig_repr = a_representation @raw = raw end |
Instance Method Details
#add_link(rel, target, opts = {}) ⇒ Object
Returns a RepresentationEditor exactly like this one except that is has an additional link to the specified target with the specified rel.
rel - The type of relationship this link represents target - URL of the target of the link opts
:templated - is this link templated? Default: false
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/hal_client/representation_editor.rb', line 91 def add_link(rel, target, opts={}) templated = opts.fetch(:templated, false) link_obj = { "href" => target.to_s } link_obj = link_obj.merge("templated" => true) if templated with_new_link = Array(raw.fetch("_links", {}).fetch(rel, [])) + [link_obj] updated_links_section = raw.fetch("_links", {}).merge(rel => with_new_link) self.class.new(orig_repr, raw.merge("_links" => updated_links_section)) end |
#reject_embedded(rel, &blk) ⇒ Object
Returns a RepresentationEditor for a representation like the current one but without the specified embedded resources.
rel - The relationship type to remove or filter blk - When given only embedded resources for whom
the block returns true will be rejected.
Yields Representation of the target for each embedded.
75 76 77 78 79 80 81 |
# File 'lib/hal_client/representation_editor.rb', line 75 def (rel, &blk) reject_from_section("_embedded", rel, ->(e) {Representation.new(parsed_json: e, hal_client: hal_client)}, blk) end |
#reject_links(rel, &blk) ⇒ Object
Returns a RepresentationEditor for a representation like the current one but without the specified links.
rel - The relationship type to remove or filter blk - When given only links to resources for whom
the block returns true will be rejected.
Yields Representation of the target for each link.
59 60 61 62 63 64 65 |
# File 'lib/hal_client/representation_editor.rb', line 59 def reject_links(rel, &blk) reject_from_section("_links", rel, ->(l) {Representation.new(href: l["href"], hal_client: hal_client)}, blk) end |
#reject_related(rel, &blk) ⇒ Object
Returns a RepresentationEditor for a representation like the current one but without the specified links and/or embeddeds.
rel - The relationship type to remove or filter blk - When given only linked and embedded resource for whom
the block returns true will be rejected.
Yields Representation of the target for each link/embedded.
47 48 49 |
# File 'lib/hal_client/representation_editor.rb', line 47 def (rel, &blk) reject_links(rel, &blk).(rel, &blk) end |
#set_property(key, value) ⇒ Object
Returns a RepresentationEditor exactly like this one except that is has an new or overwritten property value
key - The name of the property value - Value to place in the property
108 109 110 |
# File 'lib/hal_client/representation_editor.rb', line 108 def set_property(key, value) self.class.new(orig_repr, raw.merge(key => value)) end |
#to_json ⇒ Object Also known as: to_hal
Returns the raw json representation of this representation
34 35 36 |
# File 'lib/hal_client/representation_editor.rb', line 34 def to_json MultiJson.dump(raw) end |