pagy 5.5.1 → 5.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e23922e820d30e39dee31d0b1931e79da25f9fd6fa4ee4c5f75a25f383ca99bc
4
- data.tar.gz: 863360e18514c2250c2746d7fdfac4b11e65e0b04488150bee078466877a8b33
3
+ metadata.gz: 4a62cb4c6decfdc1013da6d14b38220aa361246637301a8d92b2d781440397bc
4
+ data.tar.gz: 2555195198def80d6ec002928da8cef07419a76da9a196d8971d50e7fb5095e2
5
5
  SHA512:
6
- metadata.gz: 27819f1864544b94b0c916cca3d4d43a7bfd051c41a7a202b30818e9b5730fd754c2034247c912d48bb046d90ce46f95f4a18a636b2b75e8ed6b06203f1ddc81
7
- data.tar.gz: '031559fa58448415d4555613c76cb16f3e5f1b1262400d6ea928c0817dd45a6902a28014052860aa1ac74f6da59d2313d142226635fd6d351578f12477e34439'
6
+ metadata.gz: f665f63cb281c25d6fcf430c12888def46ea46911e45a566fd52cb4943763efe775b094015d3b658b8322ade23a9a43a5888c60f10471bd567f000d5a67352c7
7
+ data.tar.gz: 8c22ab2ad5efc0bd91b648d89217d1b71d77fbf669e1153b015954b79c2506c8f5ffe29d80e7d5988bea76b4bed35e7a76bb9c3657f60de0db3cde84b38e62e2
data/lib/config/pagy.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Pagy initializer file (5.5.1)
3
+ # Pagy initializer file (5.6.0)
4
4
  # Customize only what you really need and notice that Pagy works also without any of the following lines.
5
5
  # Should you just cherry pick part of this file, please maintain the require-order of the extras
6
6
 
@@ -5,7 +5,7 @@
5
5
  // Container of the whole pagy stuff
6
6
  function Pagy(){}
7
7
 
8
- Pagy.version = '5.5.1'
8
+ Pagy.version = '5.6.0'
9
9
 
10
10
  // Used by the waitForMe function
11
11
  Pagy.delay = 100
@@ -1,44 +1,16 @@
1
1
  # See Pagy::Countless API documentation: https://ddnexus.github.io/pagy/api/calendar
2
2
  # frozen_string_literal: true
3
3
 
4
+ require_relative 'month_mixin'
5
+
4
6
  class Pagy # :nodoc:
5
7
  class Calendar # :nodoc:
6
8
  # Calendar month subclass
7
9
  class Month < Calendar
8
10
  DEFAULT = { order: :asc, # rubocop:disable Style/MutableConstant
9
11
  format: '%Y-%m' }
10
-
11
- protected
12
-
13
- # Setup the calendar variables
14
- def setup_unit_vars
15
- super
16
- @initial = new_time(@starting.year, @starting.month)
17
- @final = bump_month(@ending)
18
- @pages = @last = months(@final) - months(@initial)
19
- @from = start_for(@page)
20
- @to = bump_month(@from)
21
- end
22
-
23
- # Time for the page
24
- def start_for(page)
25
- bump_month(@initial, snap(page))
26
- end
27
-
28
- private
29
-
30
- # Months in local time
31
- def months(time)
32
- (time.year * 12) + time.month
33
- end
34
-
35
- # Add 1 or more months to local time
36
- def bump_month(time, months = 1)
37
- months += months(time)
38
- year = months / 12
39
- month = months % 12
40
- month.zero? ? new_time(year - 1, 12) : new_time(year, month)
41
- end
12
+ MONTHS = 1 # number of months in the unit
13
+ include MonthMixin
42
14
  end
43
15
  end
44
16
  end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Pagy
4
+ class Calendar
5
+ # Mixin for month based unit periods
6
+ # It is used for month and quarter, but you could use it to implement less common unit of 6, 4, 2 months
7
+ # (see the https://ddnexus.github.io/pagy/api/calendar#custom-units sections for details).
8
+ # The class that includes it needs to set the MONTH duration for the unit and the usual DEFAULT.
9
+ module MonthMixin
10
+ protected
11
+
12
+ # Setup the calendar variables
13
+ def setup_unit_vars
14
+ super
15
+ @months = self.class::MONTHS # number of months in the unit
16
+ @initial = new_time(@starting.year, beginning_month(@starting.month))
17
+ @final = add_to(new_time(@ending.year, beginning_month(@ending.month)), @months)
18
+ @pages = @last = (months_in(@final) - months_in(@initial)) / @months
19
+ @from = start_for(@page)
20
+ @to = add_to(@from, @months)
21
+ end
22
+
23
+ # Time for the page
24
+ def start_for(page)
25
+ add_to(@initial, snap(page) * @months)
26
+ end
27
+
28
+ # Return the beginning month for the unit (e.g. quarter) that includes the month argument
29
+ def beginning_month(month)
30
+ (@months * ((month - 1) / @months)) + 1
31
+ end
32
+
33
+ private
34
+
35
+ # Months in time
36
+ def months_in(time)
37
+ (time.year * 12) + time.month
38
+ end
39
+
40
+ # Add months to time
41
+ def add_to(time, months)
42
+ months += months_in(time)
43
+ year = months / 12
44
+ month = months % 12
45
+ month.zero? ? new_time(year - 1, 12) : new_time(year, month)
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,16 @@
1
+ # See Pagy::Countless API documentation: https://ddnexus.github.io/pagy/api/calendar
2
+ # frozen_string_literal: true
3
+
4
+ require_relative 'month_mixin'
5
+
6
+ class Pagy # :nodoc:
7
+ class Calendar # :nodoc:
8
+ # Calendar quarter subclass
9
+ class Quarter < Calendar
10
+ DEFAULT = { order: :asc, # rubocop:disable Style/MutableConstant
11
+ format: '%Y-Q%q' }
12
+ MONTHS = 3 # number of months of the unit
13
+ include MonthMixin
14
+ end
15
+ end
16
+ end
@@ -29,7 +29,7 @@ class Pagy # :nodoc:
29
29
 
30
30
  private
31
31
 
32
- # Return the start of the week for local time
32
+ # Return the start of the week for time
33
33
  def week_start(time)
34
34
  start = time - (((time.wday - @offset) * DAY) % WEEK)
35
35
  new_time(start.year, start.month, start.day)
data/lib/pagy/calendar.rb CHANGED
@@ -4,10 +4,12 @@
4
4
  require 'pagy'
5
5
 
6
6
  class Pagy # :nodoc:
7
- # Base class for time units subclasses (Year, Month, Week, Day)
7
+ # Base class for time units subclasses (Year, Quarter, Month, Week, Day)
8
8
  class Calendar < Pagy
9
- DAY = 60 * 60 * 24
10
- WEEK = DAY * 7
9
+ # List of units in desc order of duration. It can be used for custom units.
10
+ UNITS = %i[year quarter month week day] # rubocop:disable Style/MutableConstant
11
+ DAY = 60 * 60 * 24 # One day in seconds
12
+ WEEK = DAY * 7 # One week in seconds
11
13
 
12
14
  attr_reader :order
13
15
 
@@ -15,8 +17,8 @@ class Pagy # :nodoc:
15
17
  def initialize(vars) # rubocop:disable Lint/MissingSuper
16
18
  raise InternalError, 'Pagy::Calendar is a base class; use one of its subclasses' if instance_of?(Pagy::Calendar)
17
19
 
18
- normalize_vars(vars)
19
- @vars = self.class::DEFAULT.merge(@vars)
20
+ vars = self.class::DEFAULT.merge(vars) # subclass specific default
21
+ normalize_vars(vars) # general default
20
22
  setup_vars(page: 1)
21
23
  setup_unit_vars
22
24
  setup_params_var
@@ -34,12 +36,14 @@ class Pagy # :nodoc:
34
36
  # The label for any page (it can pass along the I18n gem opts when it's used with the i18n extra)
35
37
  def label_for(page, **opts)
36
38
  opts[:format] ||= @vars[:format]
37
- localize(start_for(page.to_i), **opts)
39
+ start = start_for(page.to_i)
40
+ opts[:format] = opts[:format].gsub('%q') { (start.month / 4) + 1 }
41
+ localize(start, **opts)
38
42
  end
39
43
 
40
44
  # Period of the active page (used for nested units)
41
45
  def active_period
42
- [[@starting, @from].max, [@to - DAY, @ending].min] # include only last unit day
46
+ [[@starting, @from].max, [@to - 1, @ending].min] # -1 sec: include only last unit day
43
47
  end
44
48
 
45
49
  protected
@@ -61,7 +65,7 @@ class Pagy # :nodoc:
61
65
  time.strftime(opts[:format])
62
66
  end
63
67
 
64
- # Simple trick to snap the page to its ordered start, without actually reordering anything in the internal structure.
68
+ # Simple trick to snap the page to its ordered start, without actually reordering anything in the internal structure
65
69
  def snap(page)
66
70
  @order == :asc ? page - 1 : @pages - page
67
71
  end
@@ -70,20 +74,18 @@ class Pagy # :nodoc:
70
74
  def new_time(year, month = 1, day = 1)
71
75
  Time.new(year, month, day, 0, 0, 0, @utc_offset)
72
76
  end
73
- end
74
- require 'pagy/calendar/year'
75
- require 'pagy/calendar/month'
76
- require 'pagy/calendar/week'
77
- require 'pagy/calendar/day'
78
-
79
- class Calendar # :nodoc:
80
- UNITS = { year: Year, month: Month, week: Week, day: Day }.freeze
81
77
 
82
- # Create a subclass instance by unit name (internal use)
83
- def self.create(unit, vars)
84
- raise InternalError, "unit must be in #{UNITS.keys.inspect}; got #{unit}" unless UNITS.key?(unit)
78
+ class << self
79
+ # Create a subclass instance by unit name (internal use)
80
+ def create(unit, vars)
81
+ raise InternalError, "unit must be in #{UNITS.inspect}; got #{unit}" unless UNITS.include?(unit)
85
82
 
86
- UNITS[unit].new(vars)
83
+ name = unit.to_s
84
+ name[0] = name[0].capitalize
85
+ Object.const_get("Pagy::Calendar::#{name}").new(vars)
86
+ end
87
87
  end
88
88
  end
89
+
90
+ Calendar::UNITS.each { |unit| require "pagy/calendar/#{unit}" }
89
91
  end
@@ -8,11 +8,11 @@ class Pagy # :nodoc:
8
8
  module CalendarExtra
9
9
  # Additions for the Backend module
10
10
  module Backend
11
- CONF_KEYS = %i[year month week day pagy active].freeze
11
+ CONF_KEYS = (Calendar::UNITS + %i[pagy active]).freeze
12
12
 
13
13
  private
14
14
 
15
- # Take a collection and a conf Hash with keys in [:year, :month: week, :day, :pagy: :active];
15
+ # Take a collection and a conf Hash with keys in [:year, :quarter, :month: week, :day, :pagy: :active];
16
16
  # The calendar is active by default, but it can be explicitly inactivated with `active: false`
17
17
  # Return a hash with 3 items:
18
18
  # 0. Array of pagy calendar unit objects
@@ -31,7 +31,7 @@ class Pagy # :nodoc:
31
31
 
32
32
  # Setup and return the calendar objects and the filtered collection
33
33
  def pagy_setup_calendar(collection, conf)
34
- units = Calendar::UNITS.keys & conf.keys
34
+ units = Calendar::UNITS & conf.keys # get the units in time length desc order
35
35
  page_param = conf[:pagy][:page_param] || DEFAULT[:page_param]
36
36
  units.each do |unit| # set all the :page_param vars for later deletion
37
37
  unit_page_param = :"#{unit}_#{page_param}"
@@ -47,7 +47,7 @@ class Pagy # :nodoc:
47
47
  params
48
48
  end
49
49
  conf[unit][:period] = period
50
- calendar[unit] = Calendar.create(unit, conf[unit])
50
+ calendar[unit] = Calendar.send(:create, unit, conf[unit])
51
51
  period = calendar[unit].active_period # set the period for the next unit
52
52
  end
53
53
  [calendar, pagy_calendar_filter(collection, calendar[units.last].from, calendar[units.last].to)]
data/lib/pagy.rb CHANGED
@@ -5,7 +5,7 @@ require 'pathname'
5
5
 
6
6
  # Core class
7
7
  class Pagy
8
- VERSION = '5.5.1'
8
+ VERSION = '5.6.0'
9
9
 
10
10
  # Root pathname to get the path of Pagy files like templates or dictionaries
11
11
  def self.root
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pagy
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.5.1
4
+ version: 5.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Domizio Demichelis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-11-23 00:00:00.000000000 Z
11
+ date: 2021-11-26 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Agnostic pagination in plain ruby. It does it all. Better.
14
14
  email:
@@ -56,6 +56,8 @@ files:
56
56
  - lib/pagy/calendar.rb
57
57
  - lib/pagy/calendar/day.rb
58
58
  - lib/pagy/calendar/month.rb
59
+ - lib/pagy/calendar/month_mixin.rb
60
+ - lib/pagy/calendar/quarter.rb
59
61
  - lib/pagy/calendar/week.rb
60
62
  - lib/pagy/calendar/year.rb
61
63
  - lib/pagy/console.rb