Class: TableQuery::Type

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Type

Create new type.

Parameters:

  • name (Symbol)

    type name



19
20
21
# File 'lib/table-query/type.rb', line 19

def initialize(name)
  @name = name
end

Instance Attribute Details

#nameObject (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.

Returns:

  • (Type)

    type object



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.

Parameters:

  • val (Object)

    ruby object

Returns:

  • (String)

    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.

Parameters:

  • val (String)

    the data

Returns:

  • (Object)

    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.

Parameters:

  • b (Proc)

    ruby object to table data converter



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.

Parameters:

  • b (Proc)

    table data to ruby object converter



28
29
30
# File 'lib/table-query/type.rb', line 28

def to_ruby(&b)
  @to_ruby = b
end