Sha256: 6cc9c7ada51bbf005675d9ae219a344250665658c3d41abd289135fda017197a

Contents?: true

Size: 913 Bytes

Versions: 8

Compression:

Stored size: 913 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?
          elsif type == Float
            value.to_s.scan(/\d+\.?\d*/).join.to_f unless value.blank?
          else
            type.json_create value unless value.blank?
          end
        else
          value
        end
      end
      
    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
davber_couch_potato-0.3.0 lib/couch_potato/persistence/type_caster.rb
couch_potato-0.3.0 lib/couch_potato/persistence/type_caster.rb
couch_potato-0.2.32 lib/couch_potato/persistence/type_caster.rb
couch_potato-0.2.31 lib/couch_potato/persistence/type_caster.rb
couch_potato-0.2.30 lib/couch_potato/persistence/type_caster.rb
couch_potato-0.2.29 lib/couch_potato/persistence/type_caster.rb
couch_potato-0.2.28 lib/couch_potato/persistence/type_caster.rb
couch_potato-0.2.27 lib/couch_potato/persistence/type_caster.rb