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.
14 15 16 17 18 19 |
# File 'lib/alba/type.rb', line 14 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
7 8 9 |
# File 'lib/alba/type.rb', line 7 def auto_convert=(value) @auto_convert = value end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/alba/type.rb', line 6 def name @name end |
Instance Method Details
#auto_convert_with(converter) ⇒ Object
Enable auto convert with given converter
40 41 42 43 |
# File 'lib/alba/type.rb', line 40 def auto_convert_with(converter) @converter = converter @auto_convert = true end |
#check(value) ⇒ Boolean
Type check
25 26 27 |
# File 'lib/alba/type.rb', line 25 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
34 35 36 |
# File 'lib/alba/type.rb', line 34 def convert(obj) @auto_convert ? @converter.call(obj) : raise(TypeError) end |