Class: Terrain::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/terrain/page.rb

Defined Under Namespace

Classes: RangeError

Constant Summary collapse

RANGE_REGEX =
/^(?<from>[0-9]*)-(?<to>[0-9]*)$/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope, range = nil) ⇒ Page

Returns a new instance of Page.



9
10
11
12
# File 'lib/terrain/page.rb', line 9

def initialize(scope, range = nil)
  @scope = scope
  @range = range
end

Instance Attribute Details

#rangeObject (readonly)

Returns the value of attribute range.



7
8
9
# File 'lib/terrain/page.rb', line 7

def range
  @range
end

#scopeObject (readonly)

Returns the value of attribute scope.



7
8
9
# File 'lib/terrain/page.rb', line 7

def scope
  @scope
end

Instance Method Details

#boundsObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/terrain/page.rb', line 14

def bounds
  @bounds ||= begin
    if range.present?
      if match
        raise RangeError if from > to
        [from, to]
      else
        raise RangeError
      end
    else
      [0, count - 1]
    end
  end
end

#content_rangeObject



39
40
41
42
43
44
45
46
47
# File 'lib/terrain/page.rb', line 39

def content_range
  if count > 0
    from, to = bounds
    to = [to, from + records.count - 1].min
    "#{from}-#{to}/#{count}"
  else
    '*/0'
  end
end

#countObject



29
30
31
# File 'lib/terrain/page.rb', line 29

def count
  @count ||= scope.count
end

#recordsObject



33
34
35
36
37
# File 'lib/terrain/page.rb', line 33

def records
  from, to = bounds
  limit = [to - from + 1, Terrain.config.max_records].min
  @records ||= scope.offset(from).limit(limit)
end