Sha256: b6e6feecf7769e785f6ca6c166c1c50e8b5d424bad1167daf6d9ff9c7280eac7
Contents?: true
Size: 1.08 KB
Versions: 1
Compression:
Stored size: 1.08 KB
Contents
require 'bigdecimal' require 'time' require 'date' module Ohm module Typecast def self.included(base) base.extend Macros end module Macros def attribute(name, type = :string) define_method(name) do typecast(read_local(name), type) end define_method(:"#{name}=") do |value| write_local(name, value && value.to_s) end attributes << name unless attributes.include?(name) end end protected def typecast(val, type) return val if val.to_s.empty? case type when :integer then Integer(val) when :float then Float(val) when :decimal then BigDecimal(val) when :time ret = Time.parse(val) ret.to_s == val ? ret : val when :date then Date.parse(val) when :string then val end # All of the casting methods used above raises an ArgumentError # if it fails to parse the value properly. If this happens, # the least surprising behavior is to return the original value rescue ArgumentError val end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
ohm-contrib-0.0.3 | lib/ohm/contrib/typecast.rb |