Class: Alf::Types::TypeCheck
- Inherits:
-
Object
- Object
- Alf::Types::TypeCheck
- Defined in:
- lib/alf/types/type_check.rb
Constant Summary collapse
Instance Attribute Summary collapse
-
#heading ⇒ Object
readonly
Returns the value of attribute heading.
Instance Method Summary collapse
- #===(tuple) ⇒ Object
- #check!(tuple) ⇒ Object
- #error(msg, what = nil) ⇒ Object
-
#initialize(heading, strict = true) ⇒ TypeCheck
constructor
A new instance of TypeCheck.
- #strict? ⇒ Boolean
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
#heading ⇒ Object (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
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 |