Module: Typisch::Typed
- Included in:
- TypedStruct
- Defined in:
- lib/typisch/typed.rb
Overview
A module which classes or modules can be extended with in order to help register an object type for that class or module, associate it with the class, and auto-define attributes on the class for the properties of its type.
To be used like so:
class Foo
include Typisch::Typed
register_type do
property :foo, :bar
...
end
end
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #type ⇒ Object
- #type_check(full_check = false) ⇒ Object
- #type_check_property(name, full_check = false) ⇒ Object
Class Method Details
.included(klass) ⇒ Object
18 19 20 21 |
# File 'lib/typisch/typed.rb', line 18 def self.included(klass) super klass.send(:extend, ClassMethods) end |
Instance Method Details
#type ⇒ Object
23 24 25 |
# File 'lib/typisch/typed.rb', line 23 def type self.class.type end |
#type_check(full_check = false) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/typisch/typed.rb', line 27 def type_check(full_check=false) if full_check self.class.type === self or raise TypeError, "failed to type-check" else self.class.type.property_names.each {|a| type_check_property(a, false)} end end |
#type_check_property(name, full_check = false) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/typisch/typed.rb', line 35 def type_check_property(name, full_check=false) type = self.class.type_of(name) or raise NameError, "no typed property #{name} in #{self.class}" value = send(name) if full_check type === send(value) else type.shallow_check_type(value) end or raise TypeError, "property #{name} was expected to be of type #{type}, got instance of #{value.class}" end |