Module: JsonTools::Predicate
- Defined in:
- lib/jsontools/jsontools.rb
Overview
Define the Predicate methods for use with the Patch object
Constant Summary collapse
- PREDICATES =
{}
Class Method Summary collapse
- .and(params, target) ⇒ Object
- .contains(params, target) ⇒ Object
- .defined(params, target) ⇒ Object
- .ends(params, target) ⇒ Object
- .extended(other) ⇒ Object
- .less(params, target) ⇒ Object
- .matches(params, target) ⇒ Object
- .more(params, target) ⇒ Object
- .not(params, target) ⇒ Object
- .number_check(params, target) {|val, test_val| ... } ⇒ Object
- .or(params, target) ⇒ Object
- .starts(params, target) ⇒ Object
- .string_check(params, target) {|val, test_val| ... } ⇒ Object
- .type(params, target) ⇒ Object
- .undefined(params, target) ⇒ Object
Class Method Details
.and(params, target) ⇒ Object
349 350 351 352 353 354 355 356 |
# File 'lib/jsontools/jsontools.rb', line 349 def self.and params, target preds = params['apply'] return false unless preds.all? {|pred| op = pred['op'].to_sym PREDICATES[op][pred,target] rescue return false } true end |
.contains(params, target) ⇒ Object
281 282 283 |
# File 'lib/jsontools/jsontools.rb', line 281 def self.contains params, target string_check(params,target) {|x,y| x.include? y } end |
.defined(params, target) ⇒ Object
285 286 287 288 |
# File 'lib/jsontools/jsontools.rb', line 285 def self.defined params, target ptr = Pointer.new params['path'] ptr.exists?(target) end |
.ends(params, target) ⇒ Object
290 291 292 |
# File 'lib/jsontools/jsontools.rb', line 290 def self.ends params, target string_check(params,target) {|x,y| x.end_with? y } end |
.extended(other) ⇒ Object
383 384 385 386 387 388 389 390 391 |
# File 'lib/jsontools/jsontools.rb', line 383 def self.extended other PREDICATES.each_pair {|x,y| other.register_op x, ->(params,target) { raise Patch::FailedOperationError unless y.call params,target } } end |
.less(params, target) ⇒ Object
305 306 307 |
# File 'lib/jsontools/jsontools.rb', line 305 def self.less params, target number_check(params,target) {|x,y| x < y} end |
.matches(params, target) ⇒ Object
294 295 296 297 298 299 300 301 302 303 |
# File 'lib/jsontools/jsontools.rb', line 294 def self.matches params, target ptr = Pointer.new params['path'] return false if !ptr.exists?(target) val = ptr.value target return false unless String === val ignore_case = params['ignore_case'] test_val = params['value'] regex = ignore_case ? Regexp.new(test_val, Regexp::IGNORECASE) : Regexp.new(test_val) regex.match val end |
.more(params, target) ⇒ Object
309 310 311 |
# File 'lib/jsontools/jsontools.rb', line 309 def self.more params, target number_check(params,target) {|x,y| x > y} end |
.not(params, target) ⇒ Object
358 359 360 361 362 363 364 365 |
# File 'lib/jsontools/jsontools.rb', line 358 def self.not params, target preds = params['apply'] return false unless preds.none? {|pred| op = pred['op'].to_sym PREDICATES[op][pred,target] rescue return false } true end |
.number_check(params, target) {|val, test_val| ... } ⇒ Object
272 273 274 275 276 277 278 279 |
# File 'lib/jsontools/jsontools.rb', line 272 def self.number_check params, target, &block ptr = Pointer.new params['path'] return false if !ptr.exists?(target) val = ptr.value target test_val = params['value'] return false unless (Numeric === val && Numeric === test_val) yield val, test_val end |
.or(params, target) ⇒ Object
367 368 369 370 371 372 373 374 |
# File 'lib/jsontools/jsontools.rb', line 367 def self.or params, target preds = params['apply'] return false unless preds.any? {|pred| op = pred['op'].to_sym PREDICATES[op][pred,target] rescue return false } true end |
.starts(params, target) ⇒ Object
313 314 315 |
# File 'lib/jsontools/jsontools.rb', line 313 def self.starts params, target string_check(params,target) {|x,y| x.start_with? y } end |
.string_check(params, target) {|val, test_val| ... } ⇒ Object
258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/jsontools/jsontools.rb', line 258 def self.string_check params, target, &block ptr = Pointer.new params['path'] return false if !ptr.exists?(target) val = ptr.value target return false unless String === val ignore_case = params['ignore_case'] test_val = params['value'] if ignore_case test_val.upcase! val.upcase! end yield val, test_val end |
.type(params, target) ⇒ Object
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
# File 'lib/jsontools/jsontools.rb', line 317 def self.type params, target ptr = Pointer.new params['path'] test_val = params['value'] if !ptr.exists? target test_val == 'undefined' else return false if !ptr.exists?(target) val = ptr.value target case test_val when 'number' Numeric === val when 'string' String === val when 'boolean' TrueClass === val || FalseClass === val when 'object' Hash === val when 'array' Array === val when 'null' NilClass === val else false end end end |