Module: ActiveCassandra::CF

Defined in:
lib/active_cassandra/cf.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/active_cassandra/cf.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)
      @@__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')

        if @@__timestamp == :on
          %w(created_at updated_at).each {|i| __attributes.delete(i) }
        elsif @@__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 timestamp(value = true)
      class_eval(<<-EOS)
        @@__timestamp = \#{value}
      EOS
    end

    def identifier(value)
      unless value.kind_of?(Proc) and value.arity <= 0 
        raise ArgumentError, "Incorrect identifier: \#{value}"
      end

      @__identifier = value
    end

    def __identify
      if @__identifier
        @__identifier.call
      else
        SimpleUUID::UUID.new.to_guid
      end
    end
  }

  mod.instance_eval %{
    def cassandra_client
      client = self.connection.raw_connection
      block_given? ? yield(client) : client
    end
  }
end