Sha256: 57d441ca50e72ce60ac369281b43b2300b3989b7da327211fa6a496f19e50de9

Contents?: true

Size: 1.2 KB

Versions: 9

Compression:

Stored size: 1.2 KB

Contents

module MongoModel
  module Attributes
    module Typecasting
      def []=(key, value)
        values_before_typecast[key] = value
        super(key, typecast(key, value))
      end
      
      # Check if key has a value that typecasts to true.
      #
      # attributes = Attributes::Store.new(:comments_count => Property.new(:comments_count, Integer))
      #
      # attributes[:comments_count] = 0
      # attributes.has?(:comments_count)
      # => false
      #
      # attributes[:comments_count] = 1
      # attributes.has?(:comments_count)
      # => true
      #
      def has?(key)
        value = self[key]
        boolean_typecast(key, value)
      end
      
      def before_type_cast(key)
        values_before_typecast[key]
      end
    
    private
      def typecast(key, value)
        unless value.nil?
          property = properties[key]
          property ? property.cast(value) : value
        end
      end
      
      def boolean_typecast(key, value)
        if property = properties[key]
          value ? property.boolean(value) : false
        else
          !!value
        end
      end
      
      def values_before_typecast
        @values_before_typecast ||= {}
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
mongomodel-0.2.2 lib/mongomodel/attributes/typecasting.rb
mongomodel-0.2.1 lib/mongomodel/attributes/typecasting.rb
mongomodel-0.2.0 lib/mongomodel/attributes/typecasting.rb
mongomodel-0.1.6 lib/mongomodel/attributes/typecasting.rb
mongomodel-0.1.5 lib/mongomodel/attributes/typecasting.rb
mongomodel-0.1.4 lib/mongomodel/attributes/typecasting.rb
mongomodel-0.1.3 lib/mongomodel/attributes/typecasting.rb
mongomodel-0.1.1 lib/mongomodel/attributes/typecasting.rb
mongomodel-0.1 lib/mongomodel/attributes/typecasting.rb