Sha256: 28e7adecf761dc0cbb8f6fb5d1c37215b55e76ed9f958cdc04a5c66f0c55fb39

Contents?: true

Size: 1006 Bytes

Versions: 7

Compression:

Stored size: 1006 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

    def self.deserialize(value)
      return nil if value.nil? || value == "null"
      JSON.parse(value)
    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
property_sets-2.5.0 lib/property_sets/casting.rb
property_sets-2.4.0 lib/property_sets/casting.rb
property_sets-2.3.0 lib/property_sets/casting.rb
property_sets-2.2.1 lib/property_sets/casting.rb
property_sets-2.2.0 lib/property_sets/casting.rb
property_sets-2.1.0 lib/property_sets/casting.rb
property_sets-2.0.1 lib/property_sets/casting.rb