Sha256: 08ec89fef957a5156265a0af0a6ffcfb327df3636a20e12b1f95296d1f80a54d

Contents?: true

Size: 906 Bytes

Versions: 10

Compression:

Stored size: 906 Bytes

Contents

module CouchPotato
  module Persistence
    class TypeCaster #:nodoc:
      def cast(value, type)
        if type == :boolean
          cast_boolean(value)
        else
          cast_native(value, type)
        end
      end

      private

      def cast_boolean(value)
        if [FalseClass, TrueClass].include?(value.class) || value.nil?
          value
        elsif [0, '0', 'false'].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

10 entries across 10 versions & 2 rubygems

Version Path
couch_potato-0.6.0 lib/couch_potato/persistence/type_caster.rb
couch_potato-0.5.7 lib/couch_potato/persistence/type_caster.rb
couch_potato-rails2-0.5.10 lib/couch_potato/persistence/type_caster.rb
couch_potato-rails2-0.5.9 lib/couch_potato/persistence/type_caster.rb
couch_potato-rails2-0.5.8 lib/couch_potato/persistence/type_caster.rb
couch_potato-rails2-0.5.7 lib/couch_potato/persistence/type_caster.rb
couch_potato-rails2-0.5.6 lib/couch_potato/persistence/type_caster.rb
couch_potato-0.5.6 lib/couch_potato/persistence/type_caster.rb
couch_potato-0.5.5 lib/couch_potato/persistence/type_caster.rb
couch_potato-0.5.4 lib/couch_potato/persistence/type_caster.rb