Class: Oval::Collection

Inherits:
Base
  • Object
show all
Defined in:
lib/oval/collection.rb

Overview

Declare container (e.g. array or hash).

Example 1: Desclare array of arbitrary elements:

Collection[Array]

or

Collection[Array,Anything[]]

or

Collection[InstanceOf[Array],Anything[]]

Example 2: Declare Array of Strings:

Collection[Array,InstanceOf[String]]

Example 3: Desclare any Hash:

Collection[Hash]

Example 4: Desclare Hash with Symbol keys and Fixnum values

Collection[Hash,{Symbol => Fixnum}]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

ensure_equal, it_should, validate

Constructor Details

#initialize(class_decl, item_decl = Oval::Anything[]) ⇒ Collection

Returns a new instance of Collection.



61
62
63
64
# 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

Instance Attribute Details

#class_declObject

Returns the value of attribute class_decl.



74
75
76
# File 'lib/oval/collection.rb', line 74

def class_decl
  @class_decl
end

#item_declObject

Returns the value of attribute item_decl.



75
76
77
# File 'lib/oval/collection.rb', line 75

def item_decl
  @item_decl
end

#item_validatorObject (readonly)

Returns the value of attribute item_validator.



76
77
78
# File 'lib/oval/collection.rb', line 76

def item_validator
  @item_validator
end

Class Method Details

.[](class_decl, item_decl = Oval::Anything[]) ⇒ Object



57
58
59
# File 'lib/oval/collection.rb', line 57

def self.[](class_decl,item_decl = Oval::Anything[])
  new(class_decl,item_decl)
end

.klass(class_decl) ⇒ Object



66
67
68
# File 'lib/oval/collection.rb', line 66

def self.klass(class_decl)
  class_decl.is_a?(Oval::SubclassOf) ? class_decl.klass : class_decl
end

Instance Method Details

#it_shouldObject



53
54
55
# File 'lib/oval/collection.rb', line 53

def it_should
  "be #{klass} whose item should #{self.class.it_should(item_validator)}"
end

#klassObject



70
71
72
# File 'lib/oval/collection.rb', line 70

def klass
  self.class.klass(class_decl)
end

#validate(collection, subject = nil) ⇒ Object



46
47
48
49
50
51
# 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