Module: RPCMapper::Scopes::ClassMethods

Defined in:
lib/rpc_mapper/scopes.rb

Instance Method Summary collapse

Instance Method Details

#scope(name, scope_options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rpc_mapper/scopes.rb', line 15

def scope(name, scope_options={})
  name = name.to_sym
  
  # TRP: Define the scope and merge onto the relation
  scopes[name] = lambda do |*args|
    options = scope_options.is_a?(Proc) ? scope_options.call(*args) : scope_options
    if options.is_a?(Hash)
      scoped.apply_finder_options(options)
    else
      scoped.merge(options)
    end
  end
  
  # TRP: Bind the above block to a method for easy access
  singleton_class.send(:define_method, name, &scopes[name])
end

#scopedObject



11
12
13
# File 'lib/rpc_mapper/scopes.rb', line 11

def scoped
  current_scope ? relation.merge(current_scope) : relation.clone
end

#scopesObject



7
8
9
# File 'lib/rpc_mapper/scopes.rb', line 7

def scopes
  read_inheritable_attribute(:scopes) || write_inheritable_attribute(:scopes, {})
end