Class: CouchbaseOrm::N1qlProxy

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

Instance Method Summary collapse

Constructor Details

#initialize(proxyfied) ⇒ N1qlProxy

Returns a new instance of N1qlProxy.



8
9
10
11
12
13
14
# File 'lib/couchbase-orm/proxies/n1ql_proxy.rb', line 8

def initialize(proxyfied)
  @proxyfied = proxyfied
  @results   = nil
  @query_str = nil
  @dirty     = true
  @mon       = Monitor.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &blk) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/couchbase-orm/proxies/n1ql_proxy.rb', line 44

def method_missing(method_name, *args, &blk)
  if @proxyfied.respond_to?(method_name)
    ret = @proxyfied.public_send(method_name, *args, &blk)
    if ret.is_a?(@proxyfied.class)
      mark_dirty!
      self
    else
      ret
    end
  else
    # Collection/query-result methods go to the realized results
    results.public_send(method_name, *args, &blk)
  end
end

Instance Method Details

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:



59
60
61
62
63
64
# File 'lib/couchbase-orm/proxies/n1ql_proxy.rb', line 59

def respond_to_missing?(method_name, include_private = false)
  @proxyfied.respond_to?(method_name, include_private) ||
    ResultsProxy.instance_methods.include?(method_name) ||
    Array.instance_methods.include?(method_name) || # common collection API
    super
end

#results(&block) ⇒ Object

Return cached results unless the underlying query changed. Yields each row through the optional block (mapping lazily when possible).



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/couchbase-orm/proxies/n1ql_proxy.rb', line 18

def results(&block)
  # Fast-path read without locking if is not dirty and results already cached
  cached = @results
  return cached if !@dirty && cached

  @mon.synchronize do
    # Double-check under the lock
    return @results if !@dirty && @results

    CouchbaseOrm.logger.debug { "Query - #{query_str}" }

    rows = @proxyfied.rows
    rows = rows.lazy.map(&block) if block # lazy map avoids intermediate array
    # ResultsProxy historically takes an Array; if yours accepts any Enumerable,
    # drop the `.to_a` to stream. Otherwise, keep `.to_a`:
    @results = ResultsProxy.new(rows.respond_to?(:to_a) ? rows.to_a : rows)
    @dirty   = false
    @results
  end
end

#to_sObject

Stable, memoized string form of the query (no per-call allocations).



40
41
42
# File 'lib/couchbase-orm/proxies/n1ql_proxy.rb', line 40

def to_s
  query_str
end