class Oval::Collection
Declare container (e.g. array or hash).
**Example 1**: Desclare array of arbitrary elements:
“‘ruby Collection “`
or
“‘ruby Collection[Array,Anything] “`
or
“‘ruby Collection[InstanceOf,Anything “`
**Example 2**: Declare Array of Strings:
“‘ruby Collection[Array,InstanceOf] “`
**Example 3**: Desclare any Hash:
“‘ruby Collection “`
**Example 4**: Desclare Hash with Symbol keys and Fixnum values
“‘ruby Collection[Hash,{Symbol => Fixnum}] “`
Attributes
class_decl[R]
item_decl[R]
item_validator[R]
Public Class Methods
[](class_decl,item_decl = Oval::Anything[])
click to toggle source
# File lib/oval/collection.rb, line 57 def self.[](class_decl,item_decl = Oval::Anything[]) new(class_decl,item_decl) end
klass(class_decl)
click to toggle source
# File lib/oval/collection.rb, line 66 def self.klass(class_decl) class_decl.is_a?(Oval::SubclassOf) ? class_decl.klass : class_decl end
new(class_decl, item_decl = Oval::Anything[])
click to toggle source
# File lib/oval/collection.rb, line 61 def initialize(class_decl, item_decl = Oval::Anything[]) self.class_decl = class_decl self.item_decl = item_decl end
Private Class Methods
validate_class_decl(decl)
click to toggle source
# File lib/oval/collection.rb, line 107 def self.validate_class_decl(decl) klass = self.klass(decl) unless klass.is_a?(Class) and ((klass<=Hash) or (klass<=Array)) raise Oval::DeclError, "Invalid collection class declarator " + "#{decl.inspect}. Should be a (subclass of) Hash or Array" end end
Public Instance Methods
it_should()
click to toggle source
# File lib/oval/collection.rb, line 53 def it_should "be #{klass} whose item should #{self.class.it_should(item_validator)}" end
klass()
click to toggle source
# File lib/oval/collection.rb, line 70 def klass self.class.klass(class_decl) end
validate(collection, subject = nil)
click to toggle source
# File lib/oval/collection.rb, line 46 def validate(collection, subject = nil) class_subject = subject.nil? ? nil : "#{subject}.class" self.class.validate(collection.class, class_decl, class_subject) i = 0 collection.each { |item| item_validator.validate(item, i, subject); i+= 1} end
Private Instance Methods
bind_item_validator(item_decl)
click to toggle source
# File lib/oval/collection.rb, line 90 def bind_item_validator(item_decl) @item_validator = select_item_validator[item_decl] end
class_decl=(decl)
click to toggle source
# File lib/oval/collection.rb, line 80 def class_decl=(decl) self.class.validate_class_decl(decl) @class_decl = decl end
item_decl=(decl)
click to toggle source
# File lib/oval/collection.rb, line 85 def item_decl=(decl) bind_item_validator(decl) @item_decl = decl end
select_item_validator()
click to toggle source
# File lib/oval/collection.rb, line 94 def select_item_validator if klass.is_a?(Class) and klass <= Hash Oval::HashItem elsif klass.is_a?(Class) and klass <= Array Oval::ArrayItem else # well, we also may have klass that is not a class, but I'm too lazy to # handle all possible exceptions, raise RuntimeError, "Invalid class #{klass.inspect} assigned to klass. " + "It seems like we have a bug in #{self.class.name}" end end