Class: JsonTools::Patch

Inherits:
Object
  • Object
show all
Defined in:
lib/jsontools/jsontools.rb

Defined Under Namespace

Classes: FailedOperationError, InvalidPatchDocumentError

Constant Summary collapse

PATCH_OPERATIONS =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ops, with_predicates = false) ⇒ Patch

Returns a new instance of Patch.



138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/jsontools/jsontools.rb', line 138

def initialize ops, with_predicates=false
  # Parse JSON if necessary
  if ops.is_a?(String) || ops.respond_to?(:read)
    ops = JSON.load(ops)
  end
  fail unless Array === ops
  @ops = ops
  # Should we include the JSON Predicate operations?
  # Off by default
  extend Predicate if with_predicates
rescue
  raise InvalidPatchDocumentError
end

Class Method Details

.add(params, target) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/jsontools/jsontools.rb', line 181

def add params, target
  ptr = Pointer.new params['path']
  obj = ptr[target]
  fail if not (Array === obj || Hash === obj)
  if (Array === obj && ptr.last == '-') 
    obj.insert -1,params['value']
  else
    obj.insert JsonTools.fix_key(obj,ptr.last),params['value']
  end
rescue
   raise FailedOperationError
end

.copy(params, target) ⇒ Object



207
208
209
# File 'lib/jsontools/jsontools.rb', line 207

def copy params, target
  move_or_copy params, target, false
end

.move(params, target) ⇒ Object



203
204
205
# File 'lib/jsontools/jsontools.rb', line 203

def move params, target
  move_or_copy params, target, true
end

.move_or_copy(params, target, move = false) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
# File 'lib/jsontools/jsontools.rb', line 211

def move_or_copy params, target, move=false 
  from = Pointer.new params['from']
  to = Pointer.new params['path']
  fail if !from.exists?(target) #|| to.exists?(target)
  obj = from[target]
  val = obj[JsonTools.fix_key(obj,from.last)]
  remove(({'path'=>params['path']}), target) if move # we only remove it if we're doing a move operation
  add ({'path'=>params['to'],'value'=>val}), target
rescue
  raise FailedOperationError
end

.new_with_predicates(ops) ⇒ Object

Initialize a new Patch object with JSON Predicate Operations enabled



154
155
156
# File 'lib/jsontools/jsontools.rb', line 154

def self.new_with_predicates ops
  new ops, true
end

.remove(params, target) ⇒ Object



194
195
196
197
198
199
200
201
# File 'lib/jsontools/jsontools.rb', line 194

def remove params, target
  ptr = Pointer.new params['path']
  return if not ptr.exists? target #it's gone, just ignore.. TODO: might still need to throw an error, but we'll skip it for now
  obj  = ptr[target]
  obj.delete_at JsonTools.fix_key(obj,ptr.last)
rescue
  raise FailedOperationError
end

.replace(params, target) ⇒ Object



223
224
225
226
227
228
229
230
# File 'lib/jsontools/jsontools.rb', line 223

def replace params, target
  ptr = Pointer.new params['path']
  fail if not ptr.exists? target
  obj = ptr[target]
  obj[JsonTools.fix_key(obj,ptr.last)] = params['value']
rescue
  raise FailedOperationError
end

.test(params, target) ⇒ Object



232
233
234
235
236
237
238
239
240
# File 'lib/jsontools/jsontools.rb', line 232

def test params, target
  ptr = Pointer.new(params['path'])
  fail if not ptr.exists? target
  obj = ptr[target]
  val = obj[JsonTools.fix_key(obj,ptr.last)]
  fail unless val == params['value']
rescue
  raise FailedOperationError
end

Instance Method Details

#apply_to(target) ⇒ Object

Apply the patch to a copy of the given target hash. The new, modified hash will be returned.



172
173
174
# File 'lib/jsontools/jsontools.rb', line 172

def apply_to target
  apply_to! target.json_deep_copy
end

#apply_to!(target) ⇒ Object

Apply the patch to the given target hash object. Note that the target will be modified in place and changes will not be reversable in the case of failure.



162
163
164
165
166
167
# File 'lib/jsontools/jsontools.rb', line 162

def apply_to! target
  @ops.each_with_object(target) do |operation, target|
    op = operation['op'].to_sym if operation.key?('op')
    PATCH_OPERATIONS[op][operation, target] rescue raise 'Invalid Operation'
  end
end

#register_op(sym, op) ⇒ Object



249
250
251
# File 'lib/jsontools/jsontools.rb', line 249

def register_op sym, op
  PATCH_OPERATIONS[sym] = op
end