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)



246
247
248
249
250
251
252
253
254
# File 'lib/couchbase-orm/relation.rb', line 246

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



121
122
123
# File 'lib/couchbase-orm/relation.rb', line 121

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

#allObject



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

def all
    CouchbaseOrm_Relation.new(**initializer_arguments)
end

#countObject Also known as: size, length



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

def count
    query.count
end

#delete_allObject



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

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:



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

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

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



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

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



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

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

#firstObject



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

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



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

def ids
    query.to_a
end

#lastObject



81
82
83
84
85
86
# File 'lib/couchbase-orm/relation.rb', line 81

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



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

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

#not(**conds) ⇒ Object



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

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

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



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

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

#pluck(*fields) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/couchbase-orm/relation.rb', line 96

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



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

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

#scopingObject



155
156
157
158
159
160
161
162
# File 'lib/couchbase-orm/relation.rb', line 155

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

#strict_loadingObject



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

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

#strict_loading?Boolean

Returns:



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

def strict_loading?
    !!@strict_loading
end

#to_aryObject Also known as: to_a



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

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_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



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

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



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



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

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



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

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