Class: CouchbaseOrm::Relation::CouchbaseOrm_Relation

Inherits:
Object
  • Object
show all
Defined in:
lib/couchbase-orm/relation.rb

Instance Method Summary collapse

Constructor Details

#initialize(model:, where: where = nil, order: order = nil, limit: limit = nil, _not: _not = false, strict_loading: strict_loading = false) ⇒ CouchbaseOrm_Relation

Returns a new instance of CouchbaseOrm_Relation.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/couchbase-orm/relation.rb', line 6

def initialize(model:, where: where = nil, order: order = nil, limit: limit = nil, _not: _not = false, strict_loading: strict_loading = false)
    CouchbaseOrm::logger.debug "CouchbaseOrm_Relation init: #{model} where:#{where.inspect} not:#{_not.inspect} order:#{order.inspect} limit: #{limit} strict_loading: #{strict_loading}"
    @model = model
    @limit = limit
    @where = []
    @order = {}
    @order = merge_order(**order) if order
    @where = merge_where(where, _not) if where
    @strict_loading = strict_loading
    CouchbaseOrm::logger.debug "- #{to_s}"
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



228
229
230
231
232
233
234
235
236
# File 'lib/couchbase-orm/relation.rb', line 228

def method_missing(method, *args, &block)
    if @model.respond_to?(method)
        scoping {
            @model.public_send(method, *args, &block)
        }
    else
        super
    end
end

Instance Method Details

#[](*args) ⇒ Object



109
110
111
# File 'lib/couchbase-orm/relation.rb', line 109

def [](*args)
    to_ary[*args]
end

#allObject



139
140
141
# File 'lib/couchbase-orm/relation.rb', line 139

def all
    CouchbaseOrm_Relation.new(**initializer_arguments)
end

#countObject Also known as: size, length



76
77
78
# File 'lib/couchbase-orm/relation.rb', line 76

def count
    query.count
end

#delete_allObject



113
114
115
116
117
# File 'lib/couchbase-orm/relation.rb', line 113

def delete_all
    CouchbaseOrm::logger.debug{ "Delete all: #{self}" }
    ids = query.to_a
    CouchbaseOrm::Connection.bucket.default_collection.remove_multi(ids) unless ids.empty?
end

#empty?Boolean

Returns:



80
81
82
# File 'lib/couchbase-orm/relation.rb', line 80

def empty?
    limit(1).count == 0
end

#execute(n1ql_query) ⇒ Object



30
31
32
33
34
# File 'lib/couchbase-orm/relation.rb', line 30

def execute(n1ql_query)
    result = @model.cluster.query(n1ql_query, Couchbase::Options::Query.new(scan_consistency: CouchbaseOrm::N1ql.config[:scan_consistency]))
    CouchbaseOrm.logger.debug { "Relation query: #{n1ql_query} return #{result.rows.to_a.length} rows with scan_consistency : #{CouchbaseOrm::N1ql.config[:scan_consistency]}" }
    N1qlProxy.new(result)
end

#find_by(**conds) ⇒ Object



123
124
125
# File 'lib/couchbase-orm/relation.rb', line 123

def find_by(**conds)
    CouchbaseOrm_Relation.new(**initializer_arguments.merge(where: merge_where(conds))).first
end

#firstObject



63
64
65
66
67
68
# File 'lib/couchbase-orm/relation.rb', line 63

def first
    result = @model.cluster.query(self.limit(1).to_n1ql, Couchbase::Options::Query.new(scan_consistency: CouchbaseOrm::N1ql.config[:scan_consistency]))
    return unless (first_id = result.rows.to_a.first)

    @model.find(first_id, with_strict_loading: @strict_loading)
end

#idsObject



51
52
53
# File 'lib/couchbase-orm/relation.rb', line 51

def ids
    query.to_a
end

#lastObject



70
71
72
73
74
# File 'lib/couchbase-orm/relation.rb', line 70

def last
    result = @model.cluster.query(to_n1ql, Couchbase::Options::Query.new(scan_consistency: CouchbaseOrm::N1ql.config[:scan_consistency]))
    last_id = result.rows.to_a.last
    @model.find(last_id, with_strict_loading: @strict_loading) if last_id
end

#limit(limit) ⇒ Object



135
136
137
# File 'lib/couchbase-orm/relation.rb', line 135

def limit(limit)
    CouchbaseOrm_Relation.new(**initializer_arguments.merge(limit: limit))
end

#not(**conds) ⇒ Object



127
128
129
# File 'lib/couchbase-orm/relation.rb', line 127

def not(**conds)
    CouchbaseOrm_Relation.new(**initializer_arguments.merge(where: merge_where(conds, _not: true)))
end

#order(*lorder, **horder) ⇒ Object



131
132
133
# File 'lib/couchbase-orm/relation.rb', line 131

def order(*lorder, **horder)
    CouchbaseOrm_Relation.new(**initializer_arguments.merge(order: merge_order(*lorder, **horder)))
end

#pluck(*fields) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/couchbase-orm/relation.rb', line 84

def pluck(*fields)
    map do |model|
        if fields.length == 1
            model.send(fields.first)
        else
            fields.map do |field|
                model.send(field)
            end
        end
    end
end

#queryObject



36
37
38
39
40
# File 'lib/couchbase-orm/relation.rb', line 36

def query
    CouchbaseOrm::logger.debug("Query: #{self}")
    n1ql_query = to_n1ql
    execute(n1ql_query)
end

#scopingObject



143
144
145
146
147
148
149
150
# File 'lib/couchbase-orm/relation.rb', line 143

def scoping
    scopes = (Thread.current[@model.name] ||= [])
    scopes.push(self)
    result = yield
ensure
    scopes.pop
    result
end

#strict_loadingObject



55
56
57
# File 'lib/couchbase-orm/relation.rb', line 55

def strict_loading
    CouchbaseOrm_Relation.new(**initializer_arguments.merge(strict_loading: true))
end

#strict_loading?Boolean

Returns:



59
60
61
# File 'lib/couchbase-orm/relation.rb', line 59

def strict_loading?
    !!@strict_loading
end

#to_aryObject Also known as: to_a



99
100
101
102
103
# File 'lib/couchbase-orm/relation.rb', line 99

def to_ary
    ids = query.results
    return [] if ids.empty?
    Array(ids && @model.find(ids, with_strict_loading: @strict_loading))
end

#to_n1qlObject



22
23
24
25
26
27
28
# File 'lib/couchbase-orm/relation.rb', line 22

def to_n1ql
    bucket_name = @model.bucket.name
    where = build_where
    order = build_order
    limit = build_limit
    "select raw meta().id from `#{bucket_name}` where #{where} order by #{order} #{limit}"
end

#to_sObject



18
19
20
# File 'lib/couchbase-orm/relation.rb', line 18

def to_s
    "CouchbaseOrm_Relation: #{@model} where:#{@where.inspect} order:#{@order.inspect} limit: #{@limit} strict_loading: #{@strict_loading}"
end

#update_all(**cond) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/couchbase-orm/relation.rb', line 42

def update_all(**cond)
    bucket_name = @model.bucket.name
    where = build_where
    limit = build_limit
    update = build_update(**cond)
    n1ql_query = "update `#{bucket_name}` set #{update} where #{where} #{limit}"
    execute(n1ql_query)
end

#where(string_cond = nil, **conds) ⇒ Object



119
120
121
# File 'lib/couchbase-orm/relation.rb', line 119

def where(string_cond=nil, **conds)
    CouchbaseOrm_Relation.new(**initializer_arguments.merge(where: merge_where(conds)+string_where(string_cond)))
end