Sha256: 62e569d0368da7c88c250b77334401e1dc78b3769cc9c4d16ef397275c3107f5

Contents?: true

Size: 1.31 KB

Versions: 16

Compression:

Stored size: 1.31 KB

Contents

module Mongoo
  class InvalidAttributeValue < Exception; end
  
  class AttributeSanitizer
    class << self
      def sanitize(field_type, val)
        return val if val.nil? || field_type.nil?
        
        case field_type.to_sym
        when :string then
          val.is_a?(String) ? val : val.to_s
        when :symbol then
          val.is_a?(Symbol) ? val : val.to_sym
        when :integer then
          val.is_a?(Fixnum) ? val : val.to_i
        when :float then
          val.is_a?(Float) ? val : val.to_f
        when :array then
          val.is_a?(Array) ? val : [val]
        when :bson_object_id then
          val.is_a?(BSON::ObjectId) ? val : BSON::ObjectId(val)
        when :hash then
          val.is_a?(Hash) ? val : raise(InvalidAttributeValue, val.inspect)
        when :time then
          Time.parse(val.to_s)
          # val.is_a?(Time) ? val : Time.parse(val)
        when :bool then
          if [true,false].include?(val)
            val
          elsif ["t","1","true","y","yes"].include?(val.to_s.downcase)
            true
          elsif ["f","0","false","n","no"].include?(val.to_s.downcase)
            false
          end
        when :html_escaped_string then
          ERB::Util.html_escape(val.to_s)
        end # case
      end # sanitize
    end # << self
  end # AttributeSanitizer
end # Mongoo

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
mongoo-0.4.4 lib/mongoo/attribute_sanitizer.rb
mongoo-0.4.3 lib/mongoo/attribute_sanitizer.rb
mongoo-0.4.2 lib/mongoo/attribute_sanitizer.rb
mongoo-0.4.1 lib/mongoo/attribute_sanitizer.rb
mongoo-0.4.0 lib/mongoo/attribute_sanitizer.rb
mongoo-0.3.1 lib/mongoo/attribute_sanitizer.rb
mongoo-0.3.0 lib/mongoo/attribute_sanitizer.rb
mongoo-0.2.4 lib/mongoo/attribute_sanitizer.rb
mongoo-0.2.3 lib/mongoo/attribute_sanitizer.rb
mongoo-0.2.2 lib/mongoo/attribute_sanitizer.rb
mongoo-0.2.1 lib/mongoo/attribute_sanitizer.rb
mongoo-0.2.0 lib/mongoo/attribute_sanitizer.rb
mongoo-0.1.5 lib/mongoo/attribute_sanitizer.rb
mongoo-0.1.4 lib/mongoo/attribute_sanitizer.rb
mongoo-0.1.3 lib/mongoo/attribute_sanitizer.rb
mongoo-0.1.2 lib/mongoo/attribute_sanitizer.rb