Class: Gom::Core::Primitive
- Inherits:
-
Object
- Object
- Gom::Core::Primitive
- Defined in:
- lib/gom/core/primitive.rb
Constant Summary collapse
- Types =
{ "Symbol" => :symbol, "Fixnum" => :integer, "Bignum" => :integer, "BigDecimal" => :decimal, "Float" => :float, "Date" => :date, "DateTime" => :datetime, "Time" => :datetime, "TrueClass" => :boolean, "FalseClass" => :boolean, "URI::HTTP" => :uri, "URI::HTTPS" => :uri, "URI::Generic" => :uri, }
- Parsers =
{ :date => Proc.new { |date| ::Date.parse(date) }, :datetime => Proc.new { |time| ::DateTime.parse(time) }, :float => Proc.new { |txt| txt.to_f }, :integer => Proc.new { |txt| txt.to_i }, :uri => Proc.new { |s| URI.parse(s) }, }
- Formatters =
Hash.new(Proc.new{|o|o.to_s}).update( :string => Proc.new { |s| s.to_s }, :date => Proc.new { |date| date.strftime('%Y-%m-%d') }, #.to_s(:db) }, :datetime => Proc.new { |time| # back and forth, trying to 'normalize' the myriad of time formats DateTime.parse(time.to_s).strftime #DateTime.parse(time.to_s).xmlschema #time.xmlschema } )
Class Method Summary collapse
-
.decode(txt, type = :string) ⇒ Object
text, type -> value.
-
.encode(value) ⇒ Object
value -> text, type.
Class Method Details
.decode(txt, type = :string) ⇒ Object
text, type -> value
44 45 46 47 |
# File 'lib/gom/core/primitive.rb', line 44 def self.decode txt, type = :string parser = type && Parsers[type.to_sym] parser && parser.call(txt) || txt end |
.encode(value) ⇒ Object
value -> text, type
50 51 52 53 54 |
# File 'lib/gom/core/primitive.rb', line 50 def self.encode value type = Types[value.class.name] || :string formatter = Formatters[type] [ formatter.call(value), type] end |