Class: Alba::Type
- Inherits:
-
Object
- Object
- Alba::Type
- Defined in:
- lib/alba/type.rb
Overview
Representing type itself, combined with TypedAttribute
Instance Attribute Summary collapse
-
#auto_convert ⇒ Object
writeonly
Sets the attribute auto_convert.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#auto_convert_with(converter) ⇒ Object
Enable auto convert with given converter.
-
#check(value) ⇒ Boolean
Type check.
-
#convert(obj) ⇒ Object
Type convert If @auto_convert is true, @convert proc is called with obj Otherwise, it raises an exception that is caught by TypedAttribute.
-
#initialize(name, check:, converter:, auto_convert: false) ⇒ Type
constructor
A new instance of Type.
Constructor Details
#initialize(name, check:, converter:, auto_convert: false) ⇒ Type
Returns a new instance of Type.
12 13 14 15 16 17 |
# File 'lib/alba/type.rb', line 12 def initialize(name, check:, converter:, auto_convert: false) @name = name @check = check @converter = converter @auto_convert = auto_convert end |
Instance Attribute Details
#auto_convert=(value) ⇒ Object (writeonly)
Sets the attribute auto_convert
5 6 7 |
# File 'lib/alba/type.rb', line 5 def auto_convert=(value) @auto_convert = value end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/alba/type.rb', line 4 def name @name end |
Instance Method Details
#auto_convert_with(converter) ⇒ Object
Enable auto convert with given converter
38 39 40 41 |
# File 'lib/alba/type.rb', line 38 def auto_convert_with(converter) @converter = converter @auto_convert = true end |
#check(value) ⇒ Boolean
Type check
23 24 25 |
# File 'lib/alba/type.rb', line 23 def check(value) @check == false ? false : @check.call(value) end |
#convert(obj) ⇒ Object
Type convert If @auto_convert is true, @convert proc is called with obj Otherwise, it raises an exception that is caught by Alba::TypedAttribute
32 33 34 |
# File 'lib/alba/type.rb', line 32 def convert(obj) @auto_convert ? @converter.call(obj) : raise(TypeError) end |