Class: CkuruTools::TypedArray

Inherits:
Array
  • Object
show all
Defined in:
lib/ckuru-tools/typed_array.rb

Overview

A typed Array guarantees that all elements of an array conform to a certain signature

Instantion requires that the first argument be the type:

new = CkuruTools::TypedArray.new(AuraVisualize::SqlConstruct,
                                 AuraVisualize::SelectConstruct.new(:name => "1"),
                                 AuraVisualize::SelectConstruct.new(:name => "2"))

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ TypedArray

Returns a new instance of TypedArray.

Raises:

  • (ArgumentError)


29
30
31
32
33
34
35
36
37
# File 'lib/ckuru-tools/typed_array.rb', line 29

def initialize(*args)
  args.reverse!
  @required_type = args.pop
  args.reverse!
  raise ArgumentError.new("argument must be a class not #{required_type.class}") unless required_type.is_a? Class
  @required_type = required_type
  super(args)
  validate
end

Instance Attribute Details

#required_typeObject

required



11
12
13
# File 'lib/ckuru-tools/typed_array.rb', line 11

def required_type
  @required_type
end

Instance Method Details

#<<(val) ⇒ Object



19
20
21
22
# File 'lib/ckuru-tools/typed_array.rb', line 19

def <<(val)
  super
  validate
end

#push(val) ⇒ Object



24
25
26
27
# File 'lib/ckuru-tools/typed_array.rb', line 24

def push(val)
  super
  validate
end

#validateObject



13
14
15
16
17
# File 'lib/ckuru-tools/typed_array.rb', line 13

def validate
  self.each do |e|
    raise ArgumentError.new("all elements of this Array must be of type #{required_type}, not #{e.class}") unless e.is_a? required_type
  end
end