Class: TableQuery::Type
- Inherits:
-
Object
- Object
- TableQuery::Type
- Defined in:
- lib/table-query/type.rb
Overview
Type is a class for data conversion.
Constant Summary collapse
- BOOL =
field type
bool
Type.define(:bool) { to_ruby {|val| val == "true"} from_ruby {|val| val.to_s} }
- INT =
field type
int
Type.define(:int) { to_ruby {|val| val.to_i} from_ruby {|val| val.to_s} }
- FLOAT =
field type
float
Type.define(:float) { to_ruby {|val| val.to_f} from_ruby {|val| val.to_s} }
- STRING =
field type
string
Type.define(:string) { to_ruby {|val| val} from_ruby {|val| val.to_s} }
- SYMBOL =
field type
symbol
Type.define(:symbol) { to_ruby {|val| val.to_sym} from_ruby {|val| val.to_s} }
- TIME =
field type
time
Type.define(:time) { to_ruby {|val| DateTime.parse(val)} from_ruby {|val| val.strftime("%Y-%m-%d %H:%M:%S")} }
- DATE =
field type
date
Type.define(:date) { to_ruby {|val| Date.parse(val)} from_ruby {|val| val.strftime("%Y-%m-%d")} }
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Return the type name.
Class Method Summary collapse
-
.define(name, &b) ⇒ Type
Define new type with the block.
Instance Method Summary collapse
-
#convert_from_ruby(val) ⇒ String
Convert the ruby object to data.
-
#convert_to_ruby(val) ⇒ Object
Convert the data to ruby object.
-
#from_ruby(&b) ⇒ void
Define a converter ruby object to data.
-
#initialize(name) ⇒ Type
constructor
Create new type.
-
#to_ruby(&b) ⇒ void
Define a converter data to ruby object.
Constructor Details
#initialize(name) ⇒ Type
Create new type.
19 20 21 |
# File 'lib/table-query/type.rb', line 19 def initialize(name) @name = name end |
Instance Attribute Details
#name ⇒ Object (readonly)
Return the type name.
13 14 15 |
# File 'lib/table-query/type.rb', line 13 def name @name end |
Class Method Details
.define(name, &b) ⇒ Type
Define new type with the block.
8 9 10 |
# File 'lib/table-query/type.rb', line 8 def self.define(name, &b) new(name).tap {|x| x.instance_eval(&b)} end |
Instance Method Details
#convert_from_ruby(val) ⇒ String
Convert the ruby object to data.
57 58 59 |
# File 'lib/table-query/type.rb', line 57 def convert_from_ruby(val) @from_ruby.call(val) end |
#convert_to_ruby(val) ⇒ Object
Convert the data to ruby object.
47 48 49 |
# File 'lib/table-query/type.rb', line 47 def convert_to_ruby(val) @to_ruby.call(val) end |
#from_ruby(&b) ⇒ void
This method returns an undefined value.
Define a converter ruby object to data.
37 38 39 |
# File 'lib/table-query/type.rb', line 37 def from_ruby(&b) @from_ruby = b end |
#to_ruby(&b) ⇒ void
This method returns an undefined value.
Define a converter data to ruby object.
28 29 30 |
# File 'lib/table-query/type.rb', line 28 def to_ruby(&b) @to_ruby = b end |