Class: MetaTypes::MetaPropertyType

Inherits:
Object
  • Object
show all
Defined in:
lib/meta_types/meta_property_type.rb

Constant Summary collapse

Instances =
{
  integer: new('integer', 'Integer'),
  string:  new('string',  'String'),
  boolean: new('boolean', 'Boolean'),
  float:   new('float',   'Float'),
  text:    new('text',    'Text'),
  date:    new('date',    'Date')
}
BoolTrueReps =
%w{1 t true}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sid, name) ⇒ MetaPropertyType

Returns a new instance of MetaPropertyType.



6
7
8
9
# File 'lib/meta_types/meta_property_type.rb', line 6

def initialize(sid, name)
  self.sid = sid
  self.name = name
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/meta_types/meta_property_type.rb', line 4

def name
  @name
end

#sidObject

Returns the value of attribute sid.



3
4
5
# File 'lib/meta_types/meta_property_type.rb', line 3

def sid
  @sid
end

Class Method Details

.find(sid) ⇒ Object Also known as: []



49
50
51
# File 'lib/meta_types/meta_property_type.rb', line 49

def find(sid)
  Instances[sid.to_sym] || raise("No MetaPropertyType with sid '#{sid}' found.")
end

.sidsObject



55
# File 'lib/meta_types/meta_property_type.rb', line 55

def sids() %w{integer string boolean float text date} end

Instance Method Details

#cast(sval) ⇒ Object

cast is called to transform the string-value from the db into the desired ruby type



24
25
26
27
28
29
30
31
32
33
# File 'lib/meta_types/meta_property_type.rb', line 24

def cast(sval)
  case sid
  when 'integer' then sval.to_i
  when 'string', 'text' then sval
  when 'boolean' then BoolTrueReps.member?(sval.to_s)
  when 'float'   then sval.to_f
  when 'date'    then sval && Date.parse(sval)
  else raise "Don't know how to handle MetaPropertyType with sid '#{sid}'."
  end
end

#parse(sval) ⇒ Object

parse is called to transform the given ruby type into the db string format



37
38
39
40
41
42
43
44
45
46
# File 'lib/meta_types/meta_property_type.rb', line 37

def parse(sval)
  case sid
  when 'integer' then sval.to_s
  when 'string', 'text' then sval.to_s
  when 'boolean' then BoolTrueReps.member?(sval.to_s).to_s
  when 'float'   then sval.to_s
  when 'date'    then sval.presence && (sval.is_a?(String) ? Date.parse(sval) : sval).strftime("%Y-%m-%d")
  else raise "Don't know how to handle MetaPropertyType with sid '#{sid}'."
  end
end