Class: Paraphrase::Repository

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

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.



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

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

Instance Attribute Details

#mappingObject (readonly)

Returns the value of attribute mapping.



3
4
5
# File 'lib/paraphrase/repository.rb', line 3

def mapping
  @mapping
end

#paramsObject (readonly)

Returns the value of attribute params.



3
4
5
# File 'lib/paraphrase/repository.rb', line 3

def params
  @params
end

#relationObject (readonly)

Returns the value of attribute relation.



3
4
5
# File 'lib/paraphrase/repository.rb', line 3

def relation
  @relation
end

Class Method Details

.chain(relation, mapping, params) ⇒ Object



5
6
7
# File 'lib/paraphrase/repository.rb', line 5

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

Instance Method Details

#chainObject



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/paraphrase/repository.rb', line 13

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



27
28
29
30
31
32
33
34
# File 'lib/paraphrase/repository.rb', line 27

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