Sha256: 3665ee5fd20b9f4feef4d52b4c7f7238b8fe95be1f90283c659d48ebb20aac1e

Contents?: true

Size: 891 Bytes

Versions: 10

Compression:

Stored size: 891 Bytes

Contents

require 'json'

module PropertySets
  module Casting

    def self.read(type, value)
      return nil if value.nil?

      case type
        when :string
          value
        when :datetime
          Time.parse(value).in_time_zone
        when :float
          value.to_f
        when :integer
          value.to_i
        when :boolean
          ![ "false", "0", "", "off", "n" ].member?(value.to_s.downcase)
        when :serialized
          # deserialization happens in the model
          value
      end
    end

    def self.write(type, value)
      return nil if value.nil?

      case type
        when :datetime
          if value.is_a?(String)
            value
          else
            value.in_time_zone("UTC").to_s
          end
        when :serialized
          # write the object directly.
          value
        else
          value.to_s
      end
    end

  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
property_sets-2.0.0 lib/property_sets/casting.rb
property_sets-1.0.5 lib/property_sets/casting.rb
property_sets-1.0.4 lib/property_sets/casting.rb
property_sets-1.0.0 lib/property_sets/casting.rb
property_sets-0.8.3 lib/property_sets/casting.rb
property_sets-0.8.2 lib/property_sets/casting.rb
property_sets-0.8.1 lib/property_sets/casting.rb
property_sets-0.8.0 lib/property_sets/casting.rb
property_sets-0.7.2 lib/property_sets/casting.rb
property_sets-0.7.1 lib/property_sets/casting.rb