Class: JSON::Hunk
- Inherits:
-
Object
- Object
- JSON::Hunk
- Defined in:
- lib/json/patch.rb
Instance Attribute Summary collapse
-
#op ⇒ Object
readonly
Returns the value of attribute op.
-
#path ⇒ Object
Returns the value of attribute path.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(attributes = {}) ⇒ Hunk
constructor
A new instance of Hunk.
-
#resolve_path(object) ⇒ Object
Given a root object, resolve the paths in this patch.
Constructor Details
#initialize(attributes = {}) ⇒ Hunk
Returns a new instance of Hunk.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/json/patch.rb', line 31 def initialize(attributes={}) op = case when attributes.has_key?('add') then 'add' when attributes.has_key?('remove') then 'remove' when attributes.has_key?('replace') then 'replace' end if op @op = op.to_sym else raise ArgumentError end self.path = attributes[op] @value = attributes['value'] end |
Instance Attribute Details
#op ⇒ Object (readonly)
Returns the value of attribute op.
29 30 31 |
# File 'lib/json/patch.rb', line 29 def op @op end |
#path ⇒ Object
Returns the value of attribute path.
29 30 31 |
# File 'lib/json/patch.rb', line 29 def path @path end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
29 30 31 |
# File 'lib/json/patch.rb', line 29 def value @value end |
Instance Method Details
#==(other) ⇒ Object
65 66 67 |
# File 'lib/json/patch.rb', line 65 def ==(other) op == other.op and path == other.path and value = other.value end |
#resolve_path(object) ⇒ Object
Given a root object, resolve the paths in this patch. Return a tuple of the object and element to be modified.
For example (from the specs):
OpenStruct.new(:foo => 1, :bar => OpenStruct.new(:baz => 2)) JSON::Hunk.new(‘add’ => ‘/bar/baz’).resolve_path(obj).should == [obj.bar, :baz]
59 60 61 62 63 |
# File 'lib/json/patch.rb', line 59 def resolve_path(object) path_to_element = path[0..-2] element = path[-1] [path_to_element.inject(object) {|o, e| o.send(e) }, element] end |