Class: HalClient::RepresentationEditor

Inherits:
Object
  • Object
show all
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 Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



34
35
36
# File 'lib/hal_client/representation_editor.rb', line 34

def raw
  @raw
end

Instance Method Details

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

Raises:

  • (ArgumentError)


124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/hal_client/representation_editor.rb', line 124

def add_link(rel, target, opts={})
  raise ArgumentError, "target must not be nil or empty" if target.nil? || target.empty?
  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

#dirty?Boolean

Returns true if this, or any previous, editor actually changed the hal representation.


Anonymous entries are hard to deal with in a logically clean way. We fudge it a bit by treating anonymous resources with the same raw value as equal.

Returns:

  • (Boolean)


43
44
45
46
47
48
49
# File 'lib/hal_client/representation_editor.rb', line 43

def dirty?
  new_repr = Representation.new(parsed_json: raw)

  orig_repr.properties != new_repr.properties ||
    sans_anon(orig_repr.all_links) != sans_anon(new_repr.all_links) ||
    raw_anons(orig_repr.all_links) != raw_anons(new_repr.all_links)
end

#reject_embedded(rel, ignore: [], &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.

Options

ignore - one or more categories of things to ignore. Valid
  values are: :broken_links. Default: []

Yields Representation of the target for each embedded.



108
109
110
111
112
113
114
# File 'lib/hal_client/representation_editor.rb', line 108

def reject_embedded(rel, ignore: [], &blk)
  reject_from_section("_embedded",
                      rel,
                      ->(e) {Representation.new(parsed_json: e,
                                                hal_client: hal_client)},
                      ignoring(ignore, blk))
end

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.

Options

ignore - one or more categories of things to ignore. Valid
  values are: :broken_links. Default: []

Yields Representation of the target for each link.



87
88
89
90
91
92
93
# File 'lib/hal_client/representation_editor.rb', line 87

def reject_links(rel, ignore: [], &blk)
  reject_from_section("_links",
                      rel,
                      ->(l) {Representation.new(href: l["href"],
                                                hal_client: hal_client)},
                      ignoring(ignore, blk))
end

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.

Options

ignore - one or more categories of things to ignore. Valid
  values are: :broken_links. Default: []

Yields Representation of the target for each link/embedded.



70
71
72
# File 'lib/hal_client/representation_editor.rb', line 70

def reject_related(rel, ignore: [], &blk)
  reject_links(rel, ignore: ignore, &blk).reject_embedded(rel, ignore: ignore, &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



142
143
144
# File 'lib/hal_client/representation_editor.rb', line 142

def set_property(key, value)
  self.class.new(orig_repr, raw.merge(key => value))
end

#to_jsonObject Also known as: to_hal

Returns the raw json representation of this representation



52
53
54
# File 'lib/hal_client/representation_editor.rb', line 52

def to_json
  MultiJson.dump(raw)
end