Class: Alf::Types::TypeCheck

Inherits:
Object
  • Object
show all
Defined in:
lib/alf/types/type_check.rb

Constant Summary collapse

Error =
Class.new(Alf::Error)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(heading, strict = true) ⇒ TypeCheck

Returns a new instance of TypeCheck


7
8
9
10
11
# File 'lib/alf/types/type_check.rb', line 7

def initialize(heading, strict = true)
  @heading   = heading
  @attr_list = heading.to_attr_list
  @strict    = strict
end

Instance Attribute Details

#headingObject (readonly)

Returns the value of attribute heading


12
13
14
# File 'lib/alf/types/type_check.rb', line 12

def heading
  @heading
end

Instance Method Details

#===(tuple) ⇒ Object


18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/alf/types/type_check.rb', line 18

def ===(tuple)
  return nil unless TupleLike===tuple
  case @attr_list <=> (AttrList.new(tuple.keys) rescue nil)
  when 1      # some attributes are missing
    return false if @strict
  when -1     # extra attributes are present
    return false
  when nil    # both missing and extra attributes
    return nil
  end
  tuple.to_hash.all?{|(k,v)| v.nil? or (@heading[k] === v) }
end

#check!(tuple) ⇒ Object


31
32
33
34
35
36
# File 'lib/alf/types/type_check.rb', line 31

def check!(tuple)
  check_type!(tuple)
  check_missing_attributes!(tuple)
  check_extra_attributes!(tuple)
  check_values!(tuple)
end

#error(msg, what = nil) ⇒ Object

Raises:


38
39
40
41
42
43
# File 'lib/alf/types/type_check.rb', line 38

def error(msg, what = nil)
  unless what.nil?
    msg << (what.size == 1 ? " `" : "s `") << what.join(', ') << "`"
  end
  raise Error, msg
end

#strict?Boolean

Returns:


14
15
16
# File 'lib/alf/types/type_check.rb', line 14

def strict?
  @strict
end