calendario 0.2.0 → 0.3.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 +4 -4
- data/CHANGELOG.md +6 -2
- data/README.md +43 -0
- data/lib/calendario/interval.rb +32 -0
- data/lib/calendario/month.rb +23 -0
- data/lib/calendario/rendered_interval.rb +38 -0
- data/lib/calendario/renderers/interval_renderer.rb +71 -0
- data/lib/calendario/renderers/year_renderer.rb +2 -40
- data/lib/calendario/version.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dc383b592c482fbab67e016507b402e7ee9c0a66553e8e306ca4ce54b53b22dd
|
4
|
+
data.tar.gz: '08888fb132751ed44cda84da8c1f3f24b3230c4c7e89216bc153f1a6d77c09d9'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d27a34fe68a5839f9729466e337e494322be699a436995aa7fd86a22079bb39bd752f12f9c67f1efb0b137afc448301b561f08d589ac0932398a831ce60b59be
|
7
|
+
data.tar.gz: ee1d9868cf5cf3a62d8f580fb240bc18c75a0fab205dd2508fdd8d1edb2c65f01c683250a794ef45e202943fda6e7d17fef46f634a1594c3139124f7bcedc7da
|
data/CHANGELOG.md
CHANGED
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
|
|
4
4
|
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
|
5
5
|
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
|
6
6
|
|
7
|
+
## [0.3.0] - 2020-06-30
|
8
|
+
### Added
|
9
|
+
- Added the concept of `Interval`. Intervals are groups of one or more months and can span multiple years
|
10
|
+
|
7
11
|
## [0.2.0] - 2020-06-29
|
8
12
|
### Changed
|
9
13
|
- Split the year rendering into 4 classes: `YearRenderer`, `RenderedYear`, `MonthRenderer` and `RenderedMonth`
|
@@ -18,5 +22,5 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|
18
22
|
- Initial core functionality
|
19
23
|
- Codebase maintenance tools
|
20
24
|
|
21
|
-
[0.2.0]: https://github.com/wilsonsilva/
|
22
|
-
[0.1.0]: https://github.com/wilsonsilva/
|
25
|
+
[0.2.0]: https://github.com/wilsonsilva/calendario/compare/v0.1.0...v0.2.0
|
26
|
+
[0.1.0]: https://github.com/wilsonsilva/calendario/compare/15adab8...v0.1.0
|
data/README.md
CHANGED
@@ -65,6 +65,9 @@ Or install it yourself as:
|
|
65
65
|
|
66
66
|
## Usage
|
67
67
|
|
68
|
+
__WARNING:__ The API is experimental until the gem reaches the version 1.0.
|
69
|
+
|
70
|
+
### Rendering a year
|
68
71
|
Rendering the current year:
|
69
72
|
```ruby
|
70
73
|
calendar = Calendario::Calendar.new
|
@@ -85,6 +88,46 @@ end
|
|
85
88
|
puts rendered_calendar
|
86
89
|
```
|
87
90
|
|
91
|
+
### Rendering a custom time interval
|
92
|
+
|
93
|
+
You can render a custom interval in any given number of columns. In the example below, we're rendering 5 months in
|
94
|
+
2 columns and as many rows as necessary:
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
interval = Calendario::Interval.new(2020, 2, 2020, 6)
|
98
|
+
renderer = Calendario::Renderers::IntervalRenderer.new
|
99
|
+
|
100
|
+
rendered_year = renderer.render(interval, columns: 2)
|
101
|
+
puts rendered_year
|
102
|
+
|
103
|
+
# February March
|
104
|
+
# Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
|
105
|
+
# 1 1 2 3 4 5 6 7
|
106
|
+
# 2 3 4 5 6 7 8 8 9 10 11 12 13 14
|
107
|
+
# 9 10 11 12 13 14 15 15 16 17 18 19 20 21
|
108
|
+
# 16 17 18 19 20 21 22 22 23 24 25 26 27 28
|
109
|
+
# 23 24 25 26 27 28 29 29 30 31
|
110
|
+
#
|
111
|
+
#
|
112
|
+
# April May
|
113
|
+
# Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
|
114
|
+
# 1 2 3 4 1 2
|
115
|
+
# 5 6 7 8 9 10 11 3 4 5 6 7 8 9
|
116
|
+
# 12 13 14 15 16 17 18 10 11 12 13 14 15 16
|
117
|
+
# 19 20 21 22 23 24 25 17 18 19 20 21 22 23
|
118
|
+
# 26 27 28 29 30 24 25 26 27 28 29 30
|
119
|
+
# 31
|
120
|
+
#
|
121
|
+
# June
|
122
|
+
# Su Mo Tu We Th Fr Sa
|
123
|
+
# 1 2 3 4 5 6
|
124
|
+
# 7 8 9 10 11 12 13
|
125
|
+
# 14 15 16 17 18 19 20
|
126
|
+
# 21 22 23 24 25 26 27
|
127
|
+
# 28 29 30
|
128
|
+
#
|
129
|
+
```
|
130
|
+
|
88
131
|
## Development
|
89
132
|
|
90
133
|
After checking out the repo, run `bin/setup` to install dependencies, configure git hooks and create support files.
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'calendario/month'
|
2
|
+
|
3
|
+
module Calendario
|
4
|
+
# A list of one or more months
|
5
|
+
class Interval
|
6
|
+
include Comparable
|
7
|
+
|
8
|
+
# Array of all months in the period
|
9
|
+
#
|
10
|
+
# @api private
|
11
|
+
# @return [Array<Month>]
|
12
|
+
#
|
13
|
+
attr_reader :months
|
14
|
+
|
15
|
+
# Initialize a time interval
|
16
|
+
#
|
17
|
+
# @api private
|
18
|
+
#
|
19
|
+
# @param [Integer] first_year The primitive representation of the first year
|
20
|
+
# @param [Integer] first_month The primitive representation of the first month
|
21
|
+
# @param [Integer] last_year The primitive representation of the last year
|
22
|
+
# @param [Integer] last_month The primitive representation of the last month
|
23
|
+
#
|
24
|
+
def initialize(first_year, first_month, last_year, last_month)
|
25
|
+
@first_year = first_year
|
26
|
+
@first_month = first_month
|
27
|
+
@last_year = last_year
|
28
|
+
@last_month = last_month
|
29
|
+
@months = (Month.new(first_year, first_month)..Month.new(last_year, last_month)).to_a
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/lib/calendario/month.rb
CHANGED
@@ -79,5 +79,28 @@ module Calendario
|
|
79
79
|
def name
|
80
80
|
MONTH_NAMES[month_number]
|
81
81
|
end
|
82
|
+
|
83
|
+
# The following month
|
84
|
+
#
|
85
|
+
# @api private
|
86
|
+
# @return [Month]
|
87
|
+
#
|
88
|
+
def succ
|
89
|
+
if month_number == 12
|
90
|
+
self.class.new(year_number + 1, 1)
|
91
|
+
else
|
92
|
+
self.class.new(year_number, month_number + 1)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
# Operator to sorts months in chronological order
|
97
|
+
#
|
98
|
+
# @api private
|
99
|
+
# @param [Month] other
|
100
|
+
# @return [Integer]
|
101
|
+
#
|
102
|
+
def <=>(other)
|
103
|
+
(year_number <=> other.year_number).nonzero? || month_number <=> other.month_number
|
104
|
+
end
|
82
105
|
end
|
83
106
|
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Calendario
|
2
|
+
# A cal-like representation of an interval:
|
3
|
+
#
|
4
|
+
# January February March
|
5
|
+
# Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
|
6
|
+
# 1 2 3 4 1 1 2 3 4 5 6 7
|
7
|
+
# 5 6 7 8 9 10 11 2 3 4 5 6 7 8 8 9 10 11 12 13 14
|
8
|
+
# 12 13 14 15 16 17 18 9 10 11 12 13 14 15 15 16 17 18 19 20 21
|
9
|
+
# 19 20 21 22 23 24 25 16 17 18 19 20 21 22 22 23 24 25 26 27 28
|
10
|
+
# 26 27 28 29 30 31 23 24 25 26 27 28 29 29 30 31
|
11
|
+
#
|
12
|
+
class RenderedInterval
|
13
|
+
# A list of lines representing an interval
|
14
|
+
#
|
15
|
+
# @api private
|
16
|
+
# @return [Array<String>]
|
17
|
+
#
|
18
|
+
attr_reader :lines
|
19
|
+
|
20
|
+
# Initializes a rendered interval
|
21
|
+
#
|
22
|
+
# @api private
|
23
|
+
# @param [Array<String>] lines List of lines representing an interval
|
24
|
+
#
|
25
|
+
def initialize(lines)
|
26
|
+
@lines = lines
|
27
|
+
end
|
28
|
+
|
29
|
+
# Returns the textual representation of an interval
|
30
|
+
#
|
31
|
+
# @api private
|
32
|
+
# @return [String]
|
33
|
+
#
|
34
|
+
def to_s
|
35
|
+
lines.join("\n")
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'calendario/interval'
|
2
|
+
require 'calendario/rendered_interval'
|
3
|
+
require 'calendario/renderers/month_renderer'
|
4
|
+
|
5
|
+
module Calendario
|
6
|
+
module Renderers
|
7
|
+
# Renders a time interval, line by line, in a table
|
8
|
+
class IntervalRenderer
|
9
|
+
# The space of an empty day
|
10
|
+
# @return [String]
|
11
|
+
EMPTY_DAY_SPACES = ' '.freeze
|
12
|
+
|
13
|
+
# Initializes a interval renderer
|
14
|
+
#
|
15
|
+
# @api private
|
16
|
+
# @param [MonthRenderer] month_renderer A service to render a month line by line
|
17
|
+
#
|
18
|
+
def initialize(month_renderer = MonthRenderer.new)
|
19
|
+
@month_renderer = month_renderer
|
20
|
+
end
|
21
|
+
|
22
|
+
# Formats a time interval, line by line, in a table
|
23
|
+
#
|
24
|
+
# @api private
|
25
|
+
# @param [Interval] interval The time interval to be rendered.
|
26
|
+
# @return [RenderedInterval]
|
27
|
+
#
|
28
|
+
def render(interval, columns:, &block)
|
29
|
+
month_renderer.filter = block if block_given?
|
30
|
+
|
31
|
+
lines = []
|
32
|
+
rendered_months = render_months(interval.months)
|
33
|
+
|
34
|
+
rendered_months.each_slice(columns) do |months|
|
35
|
+
0.upto(7) { |month_line| lines << take_row(month_line, months) }
|
36
|
+
lines.push('') # Separate rows of months
|
37
|
+
end
|
38
|
+
|
39
|
+
RenderedInterval.new(lines)
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
# A service to render a month line by line
|
45
|
+
#
|
46
|
+
# @api private
|
47
|
+
# @return [Calendario::Renderers::MonthRenderer]
|
48
|
+
#
|
49
|
+
attr_accessor :month_renderer
|
50
|
+
|
51
|
+
# Extracts a row from a group of months
|
52
|
+
#
|
53
|
+
# @api private
|
54
|
+
# @return [Array<String>]
|
55
|
+
#
|
56
|
+
def take_row(month_line, months)
|
57
|
+
months.map { |month| month[month_line] }.join(EMPTY_DAY_SPACES)
|
58
|
+
end
|
59
|
+
|
60
|
+
# Renders a list of months
|
61
|
+
#
|
62
|
+
# @api private
|
63
|
+
# @param [Array<Month>] months The list of months to be rendered
|
64
|
+
# @return [Array<RenderedMonth>]
|
65
|
+
#
|
66
|
+
def render_months(months)
|
67
|
+
months.map { |month| month_renderer.render(month) }
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -1,27 +1,15 @@
|
|
1
1
|
require 'calendario/rendered_year'
|
2
2
|
require 'calendario/renderers/month_renderer'
|
3
|
+
require 'calendario/renderers/interval_renderer'
|
3
4
|
|
4
5
|
module Calendario
|
5
6
|
module Renderers
|
6
7
|
# Renders a year, line by line, in a table of 3 columns by 4 rows
|
7
|
-
class YearRenderer
|
8
|
-
# The space of an empty day
|
9
|
-
# @return [String]
|
10
|
-
EMPTY_DAY_SPACES = ' '.freeze
|
11
|
-
|
8
|
+
class YearRenderer < IntervalRenderer
|
12
9
|
# Number of month columns to display
|
13
10
|
# @return [Integer]
|
14
11
|
NUMBER_OF_MONTH_COLUMNS = 3
|
15
12
|
|
16
|
-
# Initializes a year renderer
|
17
|
-
#
|
18
|
-
# @api private
|
19
|
-
# @param [MonthRenderer] month_renderer A service to render a month line by line
|
20
|
-
#
|
21
|
-
def initialize(month_renderer = MonthRenderer.new)
|
22
|
-
@month_renderer = month_renderer
|
23
|
-
end
|
24
|
-
|
25
13
|
# Formats a year, line by line, in a table of 3 columns by 4 rows
|
26
14
|
#
|
27
15
|
# @api private
|
@@ -44,22 +32,6 @@ module Calendario
|
|
44
32
|
|
45
33
|
private
|
46
34
|
|
47
|
-
# A service to render a month line by line
|
48
|
-
#
|
49
|
-
# @api private
|
50
|
-
# @return [Calendario::Renderers::MonthRenderer]
|
51
|
-
#
|
52
|
-
attr_accessor :month_renderer
|
53
|
-
|
54
|
-
# Extracts a row from a group of months
|
55
|
-
#
|
56
|
-
# @api private
|
57
|
-
# @return [Array<String>]
|
58
|
-
#
|
59
|
-
def take_row(month_line, months)
|
60
|
-
months.map { |month| month[month_line] }.join(EMPTY_DAY_SPACES)
|
61
|
-
end
|
62
|
-
|
63
35
|
# Centers the year in the middle of the calendar
|
64
36
|
#
|
65
37
|
# @api private
|
@@ -69,16 +41,6 @@ module Calendario
|
|
69
41
|
def center_year_number(year)
|
70
42
|
year.year_number.to_s.center(61)
|
71
43
|
end
|
72
|
-
|
73
|
-
# Renders a list of months
|
74
|
-
#
|
75
|
-
# @api private
|
76
|
-
# @param [Array<Month>] months The list of months to be rendered
|
77
|
-
# @return [Array<RenderedMonth>]
|
78
|
-
#
|
79
|
-
def render_months(months)
|
80
|
-
months.map { |month| month_renderer.render(month) }
|
81
|
-
end
|
82
44
|
end
|
83
45
|
end
|
84
46
|
end
|
data/lib/calendario/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: calendario
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Wilson Silva
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-06-
|
11
|
+
date: 2020-06-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler-audit
|
@@ -290,9 +290,12 @@ files:
|
|
290
290
|
- calendario.gemspec
|
291
291
|
- lib/calendario.rb
|
292
292
|
- lib/calendario/calendar.rb
|
293
|
+
- lib/calendario/interval.rb
|
293
294
|
- lib/calendario/month.rb
|
295
|
+
- lib/calendario/rendered_interval.rb
|
294
296
|
- lib/calendario/rendered_month.rb
|
295
297
|
- lib/calendario/rendered_year.rb
|
298
|
+
- lib/calendario/renderers/interval_renderer.rb
|
296
299
|
- lib/calendario/renderers/month_renderer.rb
|
297
300
|
- lib/calendario/renderers/year_renderer.rb
|
298
301
|
- lib/calendario/version.rb
|