Module: ActivityStreams::Spec::Defs
- Included in:
- ActivityStreams::Spec
- Defined in:
- lib/streams/activitystreams.rb
Overview
Defines the various utility methods used to build Spec modules
Instance Method Summary collapse
-
#def_absolute_iri(sym, name = nil) ⇒ Object
Define a property as being an absolute IRI.
- #def_boolean(sym, name = nil) ⇒ Object
-
#def_bound_float(sym, range, name = nil) ⇒ Object
Define a property as being a float with a bounded range.
-
#def_date_time(sym, name = nil) ⇒ Object
Define a property as being an ISO 8601 DateTime.
-
#def_iri(sym, name = nil) ⇒ Object
Define a property as being an IRI …
-
#def_non_negative_int(sym, name = nil) ⇒ Object
Define a property as being a non-negative fixnum.
-
#def_numeric(sym, name = nil, &block) ⇒ Object
Define a property as being a Numeric.
-
#def_object(sym, object_type = nil, name = nil) ⇒ Object
Define a property as being an ASObj.
-
#def_object_array(sym, object_type = nil, name = nil) ⇒ Object
Define a property as being an Array of ASObj’s.
-
#def_string(sym, name = nil, &block) ⇒ Object
Define a property as being a string, an additional block can be passed in to perform additional checking (e.g. regex matching, etc).
-
#def_string_array(sym, name = nil, &block) ⇒ Object
Define a property as being an Array of Strings, an additional block can be passed to perform additional checking.
Instance Method Details
#def_absolute_iri(sym, name = nil) ⇒ Object
Define a property as being an absolute IRI
431 432 433 434 435 436 437 438 439 440 441 |
# File 'lib/streams/activitystreams.rb', line 431 def def_absolute_iri sym, name=nil def_transform(sym) {|v| next nil if v == nil Addressable::URI.parse(v) } def_checker(sym) { |v| # v must be an absolute IRI !v || is_absolute_iri?(v) } def_alias sym, name if name end |
#def_boolean(sym, name = nil) ⇒ Object
553 554 555 556 557 558 559 560 561 562 563 564 565 |
# File 'lib/streams/activitystreams.rb', line 553 def def_boolean sym, name=nil def_transform(sym) {|v| next false if v == nil v ? true : false } def_checker(sym) { |v| v.one_of_type? TrueClass, FalseClass } def_alias sym, name if name module_eval %Q/def #{sym}() property(:'#{sym}', true) end/ module_eval %Q/def not_#{sym}() property(:'#{sym}', false) end/ end |
#def_bound_float(sym, range, name = nil) ⇒ Object
Define a property as being a float with a bounded range
589 590 591 592 593 594 |
# File 'lib/streams/activitystreams.rb', line 589 def def_bound_float sym, range, name=nil def_numeric(sym, name) {|v| next false if (range.respond_to?(:include?) && !range.include?(v)) true } end |
#def_date_time(sym, name = nil) ⇒ Object
Define a property as being an ISO 8601 DateTime
444 445 446 447 448 449 450 451 452 453 454 455 456 |
# File 'lib/streams/activitystreams.rb', line 444 def def_date_time sym, name=nil def_transform(sym) {|v| next v if v == nil || v.is_a?(Time) Time.parse(v.to_s).iso8601 rescue v } def_checker(sym) { |v| # v must be parseable as a time next true if v == nil || v.is_a?(Time) Time.parse(v.to_s) rescue next false true } def_alias sym, name if name end |
#def_iri(sym, name = nil) ⇒ Object
Define a property as being an IRI … does not have to be absolute
459 460 461 462 463 464 465 466 467 468 |
# File 'lib/streams/activitystreams.rb', line 459 def def_iri sym, name=nil def_transform(sym) {|v| next nil if v == nil Addressable::URI.parse(v)} def_checker(sym) { |v| # v must be parseable as a URI !v || Addressable::URI.parse(v) rescue false } def_alias sym, name if name end |
#def_non_negative_int(sym, name = nil) ⇒ Object
Define a property as being a non-negative fixnum
581 582 583 584 585 586 |
# File 'lib/streams/activitystreams.rb', line 581 def def_non_negative_int sym, name=nil def_numeric(sym, name) {|v| next false unless (v.is_a?(Fixnum) && v >= 0) true } end |
#def_numeric(sym, name = nil, &block) ⇒ Object
Define a property as being a Numeric
568 569 570 571 572 573 574 575 576 577 578 |
# File 'lib/streams/activitystreams.rb', line 568 def def_numeric sym, name=nil, &block def_checker(sym) { |v| next true if v == nil return false unless v.is_a? Numeric if block_given? next false unless block.call v end true } def_alias sym, name if name end |
#def_object(sym, object_type = nil, name = nil) ⇒ Object
Define a property as being an ASObj.
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 |
# File 'lib/streams/activitystreams.rb', line 488 def def_object sym, object_type=nil, name=nil def_transform(sym) def_checker(sym) { |v| next true if v == nil # v must be an instance of the given object_type if object_type next false if v.__object_type != object_type end # right now this is pretty strict... we only allow Hash or ASObj # instances to be passed. TODO: see if we can relax this to enable # more flexible duck typing ... v.one_of_type? Hash, ASObj } def_alias sym, name if name def_property(sym, object_type, name) if object_type end |
#def_object_array(sym, object_type = nil, name = nil) ⇒ Object
Define a property as being an Array of ASObj’s
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 |
# File 'lib/streams/activitystreams.rb', line 506 def def_object_array sym, object_type=nil, name=nil def_alias sym, name if name def_transform(sym) {|v| next nil if v == nil orig = self[sym] if v.is_a?(Array) next orig ? orig + v : v end orig ? orig << v : [v] } def_checker(sym) { |v| next true if v == nil # v must be either an array or enumerable and each item # must be either a Hash or ASObj that matches the given # object_type, if any next false unless (v.one_of_type?(Array, Enumerable) && !v.is_a?(Hash)) v.each {|x| return false unless x.one_of_type? ASObj, Hash return false if (object_type && x.__object_type != object_type) } true } end |
#def_string(sym, name = nil, &block) ⇒ Object
Define a property as being a string, an additional block can be passed in to perform additional checking (e.g. regex matching, etc)
472 473 474 475 476 477 478 479 480 481 482 483 484 485 |
# File 'lib/streams/activitystreams.rb', line 472 def def_string sym, name=nil, &block def_transform(sym) {|v| next nil if v == nil v.to_s } def_checker(sym) { |v| # v will be converted to a string, then checked against the optional # block... if the block returns false, raise a validation error next true if v == nil next block.call(v.to_s) if block_given? true } def_alias sym, name if name end |
#def_string_array(sym, name = nil, &block) ⇒ Object
Define a property as being an Array of Strings, an additional block can be passed to perform additional checking
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 |
# File 'lib/streams/activitystreams.rb', line 532 def def_string_array sym, name=nil, &block def_transform(sym) {|v| next nil if v == nil orig = self[sym] if v.one_of_type? Array, Enumerable add = v.map {|x| x.to_s} next orig ? orig + add : add end orig ? orig << v.to_s : [v.to_s] } def_checker(sym) { |v| next true if v == nil next false unless (v.one_of_type?(Array, Enumerable) && !v.is_a?(Hash)) v.each {|x| return false unless block.call(x) } if block_given? true } def_alias sym, name if name end |