Sha256: efcbd8a2dafb8d9a402aedf3b021dd3ecd1543a585e612e14a556f830a8587ec

Contents?: true

Size: 812 Bytes

Versions: 2

Compression:

Stored size: 812 Bytes

Contents

module CouchPotato
  module Persistence
    class TypeCaster #:nodoc:
      def cast(value, type)
        if type == :boolean
          cast_boolen(value)
        else
          cast_native(value, type)
        end
      end
      
      private
      
      def cast_boolen(value)
        if [FalseClass, TrueClass].include?(value.class) || value.nil?
          value
        elsif [0, '0'].include?(value)
          false
        else
          true
        end
      end
      
      def cast_native(value, type)
        if type && !value.instance_of?(type)
          if type == Fixnum
            value.to_s.scan(/\d/).join.to_i unless value.blank?
          else
            type.json_create value unless value.blank?
          end
        else
          value
        end
      end
      
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
couch_potato-0.2.26 lib/couch_potato/persistence/type_caster.rb
couch_potato-0.2.25 lib/couch_potato/persistence/type_caster.rb