Sha256: 63acc01d2519c8f29b407bea4271faa6dadde8f079985c8dd8db798a2cae014c

Contents?: true

Size: 1.56 KB

Versions: 22

Compression:

Stored size: 1.56 KB

Contents

module PopulateMe
  module DocumentMixins
    module Typecasting

      # This module deals with typecasting the fields
      # when they are received as strings,
      # generally from a form or a csv file

      def typecast k, v
        unless self.class.fields.key?(k)
          return WebUtils.automatic_typecast(v) 
        end
        f = self.class.fields[k].dup
        meth = "typecast_#{f[:type]}".to_sym
        unless respond_to? meth
          return WebUtils.automatic_typecast(v, [f[:type],:nil])
        end
        __send__ meth, k, v
      end

      def typecast_integer k, v
        v.to_i
      end

      def typecast_price k, v
        return nil if WebUtils.blank?(v)
        WebUtils.parse_price(v)
      end

      def typecast_select k, v
        if v.is_a?(Array)
          v.reject{|str| str=='nil' }
        else
          v
        end
      end

      def typecast_date k, v
        if v[/\d\d(\/|-)\d\d(\/|-)\d\d\d\d/]
          Date.parse v
        elsif v[/\d\d\d\d(\/|-)\d\d(\/|-)\d\d/]
          Date.parse v
        else
          nil
        end
      end

      def typecast_datetime k, v
        if v[/\d\d(\/|-)\d\d(\/|-)\d\d\d\d \d\d?:\d\d?:\d\d?/]
          d,m,y,h,min,s = v.split(/[-:\s\/]/)
          Time.utc(y,m,d,h,min,s)
        else
          nil
        end
      end

      def typecast_attachment k, v
        attached = self.attachment k
        if WebUtils.blank? v
          attached.delete_all
          return nil
        elsif v.is_a?(Hash)&&v.key?(:tempfile)
          return attached.create v
        end
      end

    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
populate-me-0.23.0 lib/populate_me/document_mixins/typecasting.rb
populate-me-0.22.1 lib/populate_me/document_mixins/typecasting.rb
populate-me-0.22.0 lib/populate_me/document_mixins/typecasting.rb
populate-me-0.21.0 lib/populate_me/document_mixins/typecasting.rb
populate-me-0.20.0 lib/populate_me/document_mixins/typecasting.rb
populate-me-0.19.0 lib/populate_me/document_mixins/typecasting.rb
populate-me-0.18.1 lib/populate_me/document_mixins/typecasting.rb
populate-me-0.18.0 lib/populate_me/document_mixins/typecasting.rb
populate-me-0.17.0 lib/populate_me/document_mixins/typecasting.rb
populate-me-0.16.0 lib/populate_me/document_mixins/typecasting.rb
populate-me-0.15.0 lib/populate_me/document_mixins/typecasting.rb
populate-me-0.14.0 lib/populate_me/document_mixins/typecasting.rb
populate-me-0.13.2 lib/populate_me/document_mixins/typecasting.rb
populate-me-0.13.1 lib/populate_me/document_mixins/typecasting.rb
populate-me-0.13.0 lib/populate_me/document_mixins/typecasting.rb
populate-me-0.12.0 lib/populate_me/document_mixins/typecasting.rb
populate-me-0.11.0 lib/populate_me/document_mixins/typecasting.rb
populate-me-0.10.1 lib/populate_me/document_mixins/typecasting.rb
populate-me-0.10.0 lib/populate_me/document_mixins/typecasting.rb
populate-me-0.9.2 lib/populate_me/document_mixins/typecasting.rb