Module: Rfm::Scope

Defined in:
lib/rfm/utilities/scope.rb

Constant Summary collapse

SCOPE =
Proc.new {[]}

Instance Method Summary collapse

Instance Method Details

#apply_scope(*args) ⇒ Object

Mix scope requests with user requests (sorta like a cross-join of requests).



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rfm/utilities/scope.rb', line 30

def apply_scope(*args)
  opts = (args.size > 1 && args.last.is_a?(Hash) ? args.pop : {})
  scope = [opts.delete(:scope) || self::SCOPE.call(opts.delete(:scope_args))].flatten
  return [args].flatten(1).push(opts) if !(args[0].is_a?(Array) || args[0].is_a?(Hash)) || scope.size < 1
  scoped_requests = []
  scope.each do |scope_req|
    [args].flatten.each do |req|
      scoped_requests.push(req[:omit] ? req : req.merge(scope_req))
    end
  end
  scoped_query = [scoped_requests, opts]
  scoped_query
end

#count(*args) ⇒ Object



24
25
26
27
# File 'lib/rfm/utilities/scope.rb', line 24

def count(*args)
  new_args = apply_scope(*args)
  super(*new_args)
end

#find(*args) ⇒ Object

Add scoping to Rfm::Base class methods for querying fmp records. Usage:

class MyModel < Rfm::Base
  SCOPE = proc { |optional-scope-args| <single-or-array-of-fmp-request-hashes> }
end

Optionally pass :scope=>fmp-request-hash-or-array or :scope_args=>anything in the FMP request options hash, to be used at scoping time, instead of above scope constant.



19
20
21
22
# File 'lib/rfm/utilities/scope.rb', line 19

def find(*args)
  new_args = apply_scope(*args)
  super(*new_args)
end