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


118
119
120
121
122
123
124
125
126
127
128
# File 'lib/hal_client/representation_editor.rb', line 118

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

#dirty?Boolean

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

Returns:

  • (Boolean)


38
39
40
41
42
43
# File 'lib/hal_client/representation_editor.rb', line 38

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

  orig_repr.properties != new_repr.properties ||
    orig_repr.all_links != 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.



102
103
104
105
106
107
108
# File 'lib/hal_client/representation_editor.rb', line 102

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.



81
82
83
84
85
86
87
# File 'lib/hal_client/representation_editor.rb', line 81

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.



64
65
66
# File 'lib/hal_client/representation_editor.rb', line 64

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



135
136
137
# File 'lib/hal_client/representation_editor.rb', line 135

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



46
47
48
# File 'lib/hal_client/representation_editor.rb', line 46

def to_json
  MultiJson.dump(raw)
end