Class: Paraphrase::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/paraphrase/repository.rb

Overview

Repository is were query-specific scopes are defined. They can be defined by re-opening the class inside the Query class definition or by using the scope class method on Query. Both methods are equivalent.

Inside scopes defined on a Repository, the method has access to Query#params as params.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(relation, mapping, params) ⇒ Repository

Returns a new instance of Repository.



15
16
17
# File 'lib/paraphrase/repository.rb', line 15

def initialize(relation, mapping, params)
  @relation, @mapping, @params = relation, mapping, params
end

Instance Attribute Details

#mappingObject (readonly)

Returns the value of attribute mapping.



9
10
11
# File 'lib/paraphrase/repository.rb', line 9

def mapping
  @mapping
end

#paramsObject (readonly)

Returns the value of attribute params.



9
10
11
# File 'lib/paraphrase/repository.rb', line 9

def params
  @params
end

#relationObject (readonly)

Returns the value of attribute relation.



9
10
11
# File 'lib/paraphrase/repository.rb', line 9

def relation
  @relation
end

Class Method Details

.chain(relation, mapping, params) ⇒ Object



11
12
13
# File 'lib/paraphrase/repository.rb', line 11

def self.chain(relation, mapping, params)
  new(relation, mapping, params).chain
end

Instance Method Details

#chainObject



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/paraphrase/repository.rb', line 19

def chain
  if mapping.satisfied?(params)

    if scope.arity.zero?
      relation.scoping { scope.call }
    else
      values = mapping.values(params)
      relation.scoping { scope.call(*values) }
    end
  else
    relation
  end
end

#scopeObject



33
34
35
36
37
38
39
40
# File 'lib/paraphrase/repository.rb', line 33

def scope
  @scope ||=
    if respond_to?(mapping.name)
      method(mapping.name)
    else
      relation.klass.method(mapping.name)
    end
end