Class: Leaflet::Collection

Inherits:
Array
  • Object
show all
Defined in:
lib/leaflet/collection.rb,
lib/leaflet/decoration.rb

Constant Summary collapse

PartialCollectionCannotBePaginated =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Collection

––––––––––––––Initialization ––––––––––––––



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/leaflet/collection.rb', line 15

def initialize(*args)
  @success = true

  if args.size == 1
    # An Array was passed in. "Convert" it to a Collection.
    replace args.shift

  elsif args.size == 2
    # Someone is building a custom Paginator, let's fetch the data.
    options = args.pop
    records = Array(args.shift)
    replace records
    @current_page  = options[:page]
    @per_page      = options[:per_page]
    @total_entries = options[:total] || self.size

  else
    raise ArgumentError
  end
end

Instance Attribute Details

#successObject

Returns the value of attribute success.



9
10
11
# File 'lib/leaflet/collection.rb', line 9

def success
  @success
end

Instance Method Details

#as_json(options = {}) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/leaflet/collection.rb', line 106

def as_json(options = {})
  {
    total_entries: total_entries,
    total_count: total_count,
    per_page: per_page,
    limit_value: limit_value,
    current_page: current_page,
    offset: offset,
    offset_value: offset_value,
    total_pages: total_pages,
    records: self.to_a
  }
end

#current_pageObject



62
63
64
65
66
# File 'lib/leaflet/collection.rb', line 62

def current_page
  Positify.it(max: total_pages) {
    @current_page ||= 1
  }
end

#decorateObject

Basic support for decorating collections just like draper does. Could be improved with more options using something like this: github.com/drapergem/draper/blob/4b933ef39d252ecfe93c573a072633be545c49fb/lib/draper/collection_decorator.rb Also a #decorated? would be nice. But the following works in 99% of the cases as is.



9
10
11
12
13
# File 'lib/leaflet/decoration.rb', line 9

def decorate
  result = dup
  result.map!(&:decorate)
  result
end

#first_page?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/leaflet/collection.rb', line 98

def first_page?
  current_page == 1
end

#inspectObject



83
84
85
86
87
88
# File 'lib/leaflet/collection.rb', line 83

def inspect
  result = ['#<Collection with']
  result << (complete? ? "#{self.size} records" : "#{self.size}/#{total_entries} records")
  result << "on page #{current_page}/#{total_pages} (#{per_page} per page): #{self.to_a}>"
  result.join(' ')
end

#last_page?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/leaflet/collection.rb', line 102

def last_page?
  current_page == total_pages
end

#next_pageObject



94
95
96
# File 'lib/leaflet/collection.rb', line 94

def next_page
  current_page < total_pages ? (current_page + 1) : nil
end

#offsetObject Also known as: offset_value

––––––––––––––––––––Public Class Getters ––––––––––––––––––––



72
73
74
# File 'lib/leaflet/collection.rb', line 72

def offset
  (current_page - 1) * per_page
end

#paginate(options = {}) ⇒ Object



36
37
38
39
40
# File 'lib/leaflet/collection.rb', line 36

def paginate(options = {})
  raise PartialCollectionCannotBePaginated unless complete?
  preliminary = self.class.new [], options.merge!(total: self.size)
  result = self.class.new self[preliminary.offset, preliminary.per_page].to_a, options
end

#per_pageObject Also known as: limit_value



55
56
57
58
59
# File 'lib/leaflet/collection.rb', line 55

def per_page
  Positify.it(max: Leaflet.config.max_per_page) {
    @per_page ||= Leaflet.config.default_per_page
  }
end

#previous_pageObject



90
91
92
# File 'lib/leaflet/collection.rb', line 90

def previous_page
  current_page > 1 ? (current_page - 1) : nil
end

#success?Boolean

–––––––––––––––––––––––Public Instance Getters –––––––––––––––––––––––

Returns:

  • (Boolean)


46
47
48
# File 'lib/leaflet/collection.rb', line 46

def success?
  !!success
end

#total_entriesObject Also known as: total_count



50
51
52
# File 'lib/leaflet/collection.rb', line 50

def total_entries
  (@total_entries ||= self.size).to_i.abs
end

#total_pagesObject



77
78
79
80
81
# File 'lib/leaflet/collection.rb', line 77

def total_pages
  Positify.it {
    total_entries.zero? ? 1 : (total_entries / per_page.to_f).ceil
  }
end