Sha256: a8667fad4d2587778136367a47c61534416fa0487e23d09f338ee69814a0fcb9

Contents?: true

Size: 1.36 KB

Versions: 10

Compression:

Stored size: 1.36 KB

Contents

require 'cassandra_object/scope/batches'
require 'cassandra_object/scope/finder_methods'
require 'cassandra_object/scope/query_methods'

module CassandraObject
  class Scope
    include Batches, FinderMethods, QueryMethods

    attr_accessor :klass
    attr_accessor :limit_value, :select_values, :where_values

    def initialize(klass)
      @klass = klass

      @limit_value = nil
      @select_values = []
      @where_values = []
    end

    private

      def scoping
        previous, klass.current_scope = klass.current_scope, self
        yield
      ensure
        klass.current_scope = previous
      end

      def method_missing(method_name, *args, &block)
        if klass.respond_to?(method_name)
          scoping { klass.send(method_name, *args, &block) }
        elsif Array.method_defined?(method_name)
          to_a.send(method_name, *args, &block)
        else
          super
        end
      end

      def instantiate_from_cql(cql_string, *args)
        results = []
        klass.execute_cql(cql_string, *args).fetch do |cql_row|
          results << instantiate_cql_row(cql_row)
        end
        results.compact!
        results
      end

      def instantiate_cql_row(cql_row)
        attributes = cql_row.to_hash
        key = attributes.delete('KEY')
        if attributes.any?
          klass.instantiate(key, attributes)
        end
      end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
gotime-cassandra_object-4.11.5 lib/cassandra_object/scope.rb
gotime-cassandra_object-4.11.4 lib/cassandra_object/scope.rb
gotime-cassandra_object-4.11.3 lib/cassandra_object/scope.rb
gotime-cassandra_object-4.11.2 lib/cassandra_object/scope.rb
gotime-cassandra_object-4.11.1 lib/cassandra_object/scope.rb
gotime-cassandra_object-4.11.0 lib/cassandra_object/scope.rb
gotime-cassandra_object-4.10.5 lib/cassandra_object/scope.rb
gotime-cassandra_object-4.10.4 lib/cassandra_object/scope.rb
gotime-cassandra_object-4.10.3 lib/cassandra_object/scope.rb
gotime-cassandra_object-4.10.2 lib/cassandra_object/scope.rb