Class: Liquor::Drop::Scope

Inherits:
Object
  • Object
show all
Includes:
External
Defined in:
lib/liquor/drop/drop_scope.rb

Direct Known Subclasses

Pagination::Scope

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from External

included, #liquor_send

Constructor Details

#initialize(source) ⇒ Scope

Returns a new instance of Scope



7
8
9
10
11
12
13
# File 'lib/liquor/drop/drop_scope.rb', line 7

def initialize(source)
  unless source.respond_to? :each
    source = source.where(nil)
  end

  @source = source
end

Instance Attribute Details

#sourceObject (readonly)

Returns the value of attribute source



5
6
7
# File 'lib/liquor/drop/drop_scope.rb', line 5

def source
  @source
end

Instance Method Details

#[](index) ⇒ Object



71
72
73
# File 'lib/liquor/drop/drop_scope.rb', line 71

def [](index)
  DropDelegation.wrap_element @source[index]
end

#countObject Also known as: size



97
98
99
# File 'lib/liquor/drop/drop_scope.rb', line 97

def count
  @source.count
end

#eachObject

Not exported. No block support in Liquor.



76
77
78
79
80
# File 'lib/liquor/drop/drop_scope.rb', line 76

def each
  @source.each do |elem|
    yield DropDelegation.wrap_element(elem)
  end
end

#entityObject



23
24
25
# File 'lib/liquor/drop/drop_scope.rb', line 23

def entity
  @source.model_name.to_s
end

#except(record) ⇒ Object



44
45
46
47
48
49
# File 'lib/liquor/drop/drop_scope.rb', line 44

def except(record)
  record, = Drop.unwrap_scope_arguments([ record ])

  result = @source.where(@source.arel_table[:id].in(record).not)
  DropDelegation.wrap_scope(result)
end

#find_all_by(_, fields = {}) ⇒ Object



35
36
37
38
39
40
# File 'lib/liquor/drop/drop_scope.rb', line 35

def find_all_by(_, fields={})
  fields, = Drop.unwrap_scope_arguments([ fields ])

  result = @source.where(fields)
  DropDelegation.wrap_scope(result)
end

#find_by(_, fields = {}) ⇒ Object



28
29
30
31
32
33
# File 'lib/liquor/drop/drop_scope.rb', line 28

def find_by(_, fields={})
  fields, = Drop.unwrap_scope_arguments([ fields ])

  result = @source.where(fields).first
  DropDelegation.wrap_element result if result
end

#find_except_by(_, fields = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/liquor/drop/drop_scope.rb', line 51

def find_except_by(_, fields={})
  fields, = Drop.unwrap_scope_arguments([ fields ])

  result = @source.
    where(fields.map do |key, value|
      @source.arel_table[key].eq(value)
    end.reduce(&:and).not)
  DropDelegation.wrap_scope(result)
end

#firstObject



63
64
65
# File 'lib/liquor/drop/drop_scope.rb', line 63

def first
  DropDelegation.wrap_element @source.first
end

#include?(elem) ⇒ Boolean

Not exported. No ? in names in Liquor.

Returns:

  • (Boolean)


83
84
85
86
87
88
89
# File 'lib/liquor/drop/drop_scope.rb', line 83

def include?(elem)
  if elem.is_a? Liquor::Drop
    @source.include? elem.source
  else
    false
  end
end

#lastObject



67
68
69
# File 'lib/liquor/drop/drop_scope.rb', line 67

def last
  DropDelegation.wrap_element @source.last
end

#limit(count) ⇒ Object



104
105
106
# File 'lib/liquor/drop/drop_scope.rb', line 104

def limit(count)
  DropDelegation.wrap_scope @source.limit(count)
end

#offset(count) ⇒ Object



108
109
110
# File 'lib/liquor/drop/drop_scope.rb', line 108

def offset(count)
  DropDelegation.wrap_scope @source.offset(count)
end

#pluck(attribute) ⇒ Object



91
92
93
94
95
# File 'lib/liquor/drop/drop_scope.rb', line 91

def pluck(attribute)
  @source.map do |elem|
    DropDelegation.wrap_element(elem).liquor_send(attribute.to_s, [])
  end
end

#reverseObject



112
113
114
# File 'lib/liquor/drop/drop_scope.rb', line 112

def reverse
  DropDelegation.wrap_scope @source.reverse_order
end

#to_aObject Also known as: to_ary

Not exported.



16
17
18
19
20
# File 'lib/liquor/drop/drop_scope.rb', line 16

def to_a
  @source.map do |elem|
    DropDelegation.wrap_element(elem)
  end
end