Module: ActiveMongo::Collection

Defined in:
lib/active_mongo/collection.rb

Class Method Summary collapse

Class Method Details

.included(mod) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/active_mongo/collection.rb', line 3

def self.included(mod)
  mod.instance_eval %{
    primary_key = ActiveRecord::ConnectionAdapters::Column.new('id', nil)
    primary_key.primary = true
    @columns = [primary_key]

    class_eval(<<-EOS)
      @@__with_timestamp = false

      alias :__respond_to? :respond_to?

      def attributes_with_quotes(include_primary_key = true, include_readonly_attributes = true, attribute_names = @attributes.keys)
        quoted = {}
        connection = self.class.connection

        __attributes = (attributes || {}).merge(@__attributes || {})
        __attributes.delete('id')
        __attributes.delete('_id')

        if @@__with_timestamp == :on
          %w(created_at updated_at).each {|i| __attributes.delete(i) }
        elsif @@__with_timestamp
          %w(created_on updated_on).each {|i| __attributes.delete(i) }
        else
          %w(created_on updated_on created_at updated_at).each do |i|
            __attributes.delete(i)
          end
        end

        __attributes.each do |name, value|
          quoted[name] = connection.quote(value)
        end

        quoted
      end

      def respond_to?(name, priv = false); true; end

      def method_missing(name, *args, &block)
        @__attributes ||= {}
        name = name.to_s

        if __respond_to?(name)
          super
        elsif name =~ /\\\\A(.+)=\\\\Z/ and args.length == 1
          @__attributes[$1] = args[0]
        elsif name =~ /[^=]\\\\Z/ and args.length == 0
          @__attributes[$1]
        else
          raise NoMethodError, "undefined method `\\\#{name}' for \#{name}"
          super
        end
      end
    EOS

    def with_timestamp(value = true)
      class_eval(<<-EOS)
        @@__with_timestamp = \#{value}
      EOS
    end
  }

  mod.instance_eval %{
    def collection
      coll = self.connection.raw_connection.collection(self.table_name)
      block_given? ? yield(coll) : coll
    end
  }
end