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, query_options: query_options = {}) ⇒ CouchbaseOrm_Relation

Returns a new instance of CouchbaseOrm_Relation.



6
7
8
9
10
11
12
13
14
15
16
17
# 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, query_options: query_options = {})
    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
    @query_options = query_options || {}
    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)



251
252
253
254
255
256
257
258
259
# File 'lib/couchbase-orm/relation.rb', line 251

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



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

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

#allObject



156
157
158
# File 'lib/couchbase-orm/relation.rb', line 156

def all
    CouchbaseOrm_Relation.new(**initializer_arguments)
end

#countObject Also known as: size, length



93
94
95
# File 'lib/couchbase-orm/relation.rb', line 93

def count
    query.count
end

#delete_allObject



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

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

#empty?Boolean

Returns:



97
98
99
# File 'lib/couchbase-orm/relation.rb', line 97

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

#execute(n1ql_query, params = []) ⇒ Object



40
41
42
43
44
# File 'lib/couchbase-orm/relation.rb', line 40

def execute(n1ql_query, params = [])
    result = @model.cluster.query(n1ql_query, build_query_options(positional_parameters: params))
    CouchbaseOrm.logger.debug { "Relation query: #{n1ql_query} params: #{params.inspect} return #{result.rows.to_a.length} rows" }
    N1qlProxy.new(result)
end

#find_by(**conds) ⇒ Object



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

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

#firstObject



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

def first
    n1ql_query, params = self.limit(1).to_n1ql_with_params
    result = @model.cluster.query(n1ql_query, build_query_options(positional_parameters: params))
    return unless (first_id = result.rows.to_a.first)

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

#idsObject



62
63
64
# File 'lib/couchbase-orm/relation.rb', line 62

def ids
    query.to_a
end

#lastObject



86
87
88
89
90
91
# File 'lib/couchbase-orm/relation.rb', line 86

def last
    n1ql_query, params = to_n1ql_with_params
    result = @model.cluster.query(n1ql_query, build_query_options(positional_parameters: params))
    last_id = result.rows.to_a.last
    @model.find(last_id, with_strict_loading: @strict_loading) if last_id
end

#limit(limit) ⇒ Object



152
153
154
# File 'lib/couchbase-orm/relation.rb', line 152

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

#not(**conds) ⇒ Object



144
145
146
# File 'lib/couchbase-orm/relation.rb', line 144

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

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



148
149
150
# File 'lib/couchbase-orm/relation.rb', line 148

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

#pluck(*fields) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/couchbase-orm/relation.rb', line 101

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



46
47
48
49
50
# File 'lib/couchbase-orm/relation.rb', line 46

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

#scopingObject



160
161
162
163
164
165
166
167
# File 'lib/couchbase-orm/relation.rb', line 160

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

#strict_loadingObject



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

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

#strict_loading?Boolean

Returns:



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

def strict_loading?
    !!@strict_loading
end

#to_aryObject Also known as: to_a



116
117
118
119
120
# File 'lib/couchbase-orm/relation.rb', line 116

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

#to_n1qlObject



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

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

#to_n1ql_with_paramsObject



31
32
33
34
35
36
37
38
# File 'lib/couchbase-orm/relation.rb', line 31

def to_n1ql_with_params
    bucket_name = @model.bucket.name
    params = []
    where = build_where_with_params(params)
    order = build_order
    limit = build_limit
    ["select raw meta().id from `#{bucket_name}` where #{where} order by #{order} #{limit}", params]
end

#to_sObject



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

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

#update_all(**cond) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/couchbase-orm/relation.rb', line 52

def update_all(**cond)
    bucket_name = @model.bucket.name
    params = []
    where = build_where_with_params(params)
    limit = build_limit
    update = build_update_with_params(params, **cond)
    n1ql_query = "update `#{bucket_name}` set #{update} where #{where} #{limit}"
    execute(n1ql_query, params)
end

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



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

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

#with(opts = {}) ⇒ Object



74
75
76
# File 'lib/couchbase-orm/relation.rb', line 74

def with(opts = {})
    CouchbaseOrm_Relation.new(**initializer_arguments.merge(query_options: @query_options.merge(opts)))
end