calendar_walker 0.0.3 → 1.0.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/.travis.yml +6 -3
- data/README.md +57 -3
- data/lib/calendar_walker/{walker.rb → month_grid.rb} +8 -8
- data/lib/calendar_walker/month_walker/row_element.rb +16 -0
- data/lib/calendar_walker/month_walker.rb +65 -0
- data/lib/calendar_walker/version.rb +1 -1
- data/lib/calendar_walker.rb +25 -1
- data/spec/calendar_walker/month_grid_spec.rb +62 -0
- data/spec/calendar_walker/month_walker/row_element_spec.rb +19 -0
- data/spec/calendar_walker/month_walker_spec.rb +170 -0
- data/spec/calendar_walker_spec.rb +31 -0
- data/spec/spec_helper.rb +7 -0
- data/spec/support/grid_support.rb +5 -0
- metadata +17 -5
- data/spec/calendar_walker/walker_spec.rb +0 -57
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9c40117316ae496c869335bdbf0dfcd7d92ed62d
|
4
|
+
data.tar.gz: e264d50a4d896a600be0a7789fb7e87accba5c49
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d87c7a4aaa81d7f0ef0a18ce47b7023faa286bdc6da9138b08beeb3321eee123553dae0c12ee529eb86ad1ffb4d2ca8a6b7e4693ef33f51cc1bddd51578c51ff
|
7
|
+
data.tar.gz: 64c4b38c281cd0ace514ef8d9df9db8826ccafa31ea3a8241af867b93cdc7d685780e4ba452772fb1f699bb1f6e362b6cb1f407f07071625f1cc64bd64b08745
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -4,7 +4,14 @@
|
|
4
4
|
|
5
5
|
<https://github.com/violarium/calendar_walker>
|
6
6
|
|
7
|
-
A simple gem to
|
7
|
+
A simple gem to work with month dates in correct order and objects attached to them.
|
8
|
+
|
9
|
+
It supports:
|
10
|
+
|
11
|
+
1. Walking through dates of month (2d array) in correct order.
|
12
|
+
2. Choosing the firsdt day of week: Sunday, Monday or whatever.
|
13
|
+
3. Adding objects collections which will be automatically attached to appropriate dates.
|
14
|
+
|
8
15
|
|
9
16
|
## Installation
|
10
17
|
|
@@ -20,15 +27,62 @@ Or install it yourself as:
|
|
20
27
|
|
21
28
|
$ gem install calendar_walker
|
22
29
|
|
30
|
+
|
31
|
+
|
23
32
|
## Usage
|
24
33
|
|
25
|
-
|
34
|
+
### Basic usage of CalendarWalker
|
26
35
|
|
36
|
+
require 'calendar_walker'
|
37
|
+
|
38
|
+
events = [event1, event2, event3]
|
39
|
+
dates = [date1, date2, date3, date4]
|
40
|
+
|
41
|
+
walker = CalendarWalker.month_walker_for(
|
42
|
+
Date.new(2014, 1), # Date object to find a current month. Could be also Time or DateTime object
|
43
|
+
1, # first day if week - Monday
|
44
|
+
{ events: {collection: events, date_method: :event_date }, # collection "event", :event_date - method in objects to get dates
|
45
|
+
dates: { collection: dates } } # collection "dates", by default, date_method is #date
|
46
|
+
)
|
47
|
+
|
48
|
+
Next code will go through all dates in received month. Each row is week. Row contains elements. Element contains date and objects.
|
49
|
+
|
50
|
+
walker.each do |row|
|
51
|
+
row.each do |row_element|
|
52
|
+
row_element.date # current date
|
53
|
+
row_element.objects[:events] # list of events at this date
|
54
|
+
row_element.objects[:dates] # list of dates at this date
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
|
59
|
+
### Going deeper
|
60
|
+
|
61
|
+
#### CalendarWalker::MonthGrid
|
62
|
+
|
63
|
+
When **CalendarWalker#month_walker_for** is called, objects of this class are used by default to manage grid of dates in month. You can use it itself for your own aims.
|
64
|
+
|
27
65
|
CalendarWalker::Walker.new(Date.new(2014, 1, 1)).wday_list # => [0, 1, 2, 3, 4, 5, 6]
|
28
|
-
|
66
|
+
|
29
67
|
walker = CalendarWalker::Walker.new(Date.new(2014, 5, 5), 6)
|
30
68
|
walker.wday_list # => [6, 0, 1, 2, 3, 4, 5]
|
31
69
|
walker.days_matrix # => [[Date.new(2014, 4, 28), Date.new(2014, 4, 29), ...], ..., [..., Date.new(2014, 6, 1)]]
|
70
|
+
|
71
|
+
|
72
|
+
#### CalendarWalker::MonthWalker
|
73
|
+
|
74
|
+
Method **CalendarWalker#month_walker_for** returns an object of class CalendarWalker::MonthWalker and encapsulates a proccess of configuring it. You can do it manually:
|
75
|
+
|
76
|
+
walker = CalendarWalker::MonthWalker.new(month_grid)
|
77
|
+
walker.add_objects(:events, events, :event_date)
|
78
|
+
walker.add_objects(:dates, dates)
|
79
|
+
|
80
|
+
|
81
|
+
## Roadmap
|
82
|
+
|
83
|
+
1. Walking through the month in vertical way
|
84
|
+
2. Simplier interface to manage single collection of objects
|
85
|
+
|
32
86
|
|
33
87
|
## Contributing
|
34
88
|
|
@@ -1,19 +1,19 @@
|
|
1
|
-
require 'date'
|
2
|
-
|
3
1
|
module CalendarWalker
|
4
|
-
# Class to
|
5
|
-
class
|
2
|
+
# Class to work with grid of month days
|
3
|
+
class MonthGrid
|
6
4
|
attr_reader :current_date, :first_wday
|
7
5
|
|
8
|
-
# Create a new
|
6
|
+
# Create a new month grid
|
9
7
|
# Arguments:
|
10
8
|
# current date (date, datetime or time)
|
11
9
|
# number of start week day between 0 and 6. 0 - Sunday, 6 - Monday.
|
12
10
|
# Examples:
|
13
|
-
# CalendarWalker::
|
14
|
-
# CalendarWalker::
|
11
|
+
# CalendarWalker::MonthGrid.new(Date.new(2014, 1, 1))
|
12
|
+
# CalendarWalker::MonthGrid.new(Time.now)
|
15
13
|
def initialize(date, first_wday = 0)
|
16
|
-
|
14
|
+
unless first_wday.is_a? Fixnum and 0 <= first_wday and first_wday <= 6
|
15
|
+
raise IncorrectWDay, 'Weekday should have Fixnum value between 0 and 6'
|
16
|
+
end
|
17
17
|
|
18
18
|
@current_date = date
|
19
19
|
@first_wday = first_wday
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module CalendarWalker
|
2
|
+
class MonthWalker
|
3
|
+
# Special class which represent element in month walker.
|
4
|
+
# It contains date and hash of appropriate objects
|
5
|
+
class RowElement
|
6
|
+
attr_reader :date
|
7
|
+
attr_reader :objects
|
8
|
+
|
9
|
+
# Create an row element for month walker
|
10
|
+
def initialize(date, objects)
|
11
|
+
@date = date
|
12
|
+
@objects = objects
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module CalendarWalker
|
2
|
+
# Class to walk through dates in months with attached to them appropriate objects
|
3
|
+
class MonthWalker
|
4
|
+
attr_reader :month_grid
|
5
|
+
|
6
|
+
# Create a new walker with month grid
|
7
|
+
# Arguments:
|
8
|
+
# month_grid - instance of the class CalendarWalker::MonthGrid or something with same functionality
|
9
|
+
# Examples:
|
10
|
+
# month_grid = CalendarWalker::MonthGrid.new(Date.new(2014, 1, 1))
|
11
|
+
# month_walker = CalendarWalker::MonthWalker.new(month_grid)
|
12
|
+
def initialize(month_grid)
|
13
|
+
@month_grid = month_grid
|
14
|
+
@objects = {}
|
15
|
+
end
|
16
|
+
|
17
|
+
# Add collection of objects with dates
|
18
|
+
# Arguments:
|
19
|
+
# key - key to identify each collection
|
20
|
+
# objects - collection of objects
|
21
|
+
# date_method - method to get date from each object in collection
|
22
|
+
# Examples:
|
23
|
+
# month_walker.add_objects[:dates, [..., ..., ...]]
|
24
|
+
# month_walker.add_objects[:events, [..., ..., ...], :event_time]
|
25
|
+
def add_objects(key, objects, date_method = :date)
|
26
|
+
@objects[key] = {}
|
27
|
+
objects.each do |object|
|
28
|
+
date_key = object.send(date_method).to_date
|
29
|
+
@objects[key][date_key] ||= []
|
30
|
+
@objects[key][date_key].push object
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# Remove object collection by key
|
35
|
+
# Arguments:
|
36
|
+
# key - key to identify existing object collection
|
37
|
+
# Examples:
|
38
|
+
# month_walker.remove_objects(:events)
|
39
|
+
def remove_objects(key)
|
40
|
+
@objects.delete(key)
|
41
|
+
end
|
42
|
+
|
43
|
+
# Walk through all date matrix with attached objects from added collections
|
44
|
+
# Examples:
|
45
|
+
# month_walker.each do |row|
|
46
|
+
# row.each do |row_element|
|
47
|
+
# row_element.date # => Date.new(2014, 5, 8)
|
48
|
+
# row_element.objects # => { events: [..., ...], dates: [] }
|
49
|
+
# end
|
50
|
+
# end
|
51
|
+
def each(&block)
|
52
|
+
@month_grid.days_matrix.each do |day_row|
|
53
|
+
row = []
|
54
|
+
day_row.each do |date|
|
55
|
+
row_elements_object = {}
|
56
|
+
@objects.each do |object_key, objects_list|
|
57
|
+
row_elements_object[object_key] = objects_list[date].nil? ? [] : objects_list[date]
|
58
|
+
end
|
59
|
+
row.push RowElement.new(date, row_elements_object)
|
60
|
+
end
|
61
|
+
block.call(row)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
data/lib/calendar_walker.rb
CHANGED
@@ -1,5 +1,29 @@
|
|
1
1
|
require 'calendar_walker/version'
|
2
|
-
require '
|
2
|
+
require 'date'
|
3
|
+
require 'calendar_walker/month_grid'
|
4
|
+
require 'calendar_walker/month_walker'
|
5
|
+
require 'calendar_walker/month_walker/row_element'
|
3
6
|
|
4
7
|
module CalendarWalker
|
8
|
+
# Create month walker with objects
|
9
|
+
# Arguments:
|
10
|
+
# date - date to get the month
|
11
|
+
# first_wday - first day of week: from 0 to 6
|
12
|
+
# objects - object collections with dates
|
13
|
+
# Examples:
|
14
|
+
# CalendarWalker.month_walker_for(
|
15
|
+
# Date.new(2014, 1, 1),
|
16
|
+
# 1,
|
17
|
+
# { events: { collection: events, date_method: :event_date },
|
18
|
+
# dates: { collection: dates } } # date_method is :date by default
|
19
|
+
# )
|
20
|
+
def self.month_walker_for(date, first_wday, objects = {})
|
21
|
+
walker = CalendarWalker::MonthWalker.new(CalendarWalker::MonthGrid.new(date, first_wday))
|
22
|
+
objects.each do |key, obj_container|
|
23
|
+
arguments = [key, obj_container[:collection]]
|
24
|
+
arguments.push obj_container[:date_method] unless obj_container[:date_method].nil?
|
25
|
+
walker.add_objects(*arguments)
|
26
|
+
end
|
27
|
+
walker
|
28
|
+
end
|
5
29
|
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe CalendarWalker::MonthGrid do
|
4
|
+
let(:current_date) { ::DateTime.new(2014, 5, 5) }
|
5
|
+
|
6
|
+
describe '#current_date' do
|
7
|
+
it 'should return same current date' do
|
8
|
+
grid = CalendarWalker::MonthGrid.new(current_date)
|
9
|
+
grid.current_date.should eq current_date
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '#first_wday' do
|
14
|
+
it 'should return received wday number' do
|
15
|
+
CalendarWalker::MonthGrid.new(current_date, 4).first_wday.should eq 4
|
16
|
+
end
|
17
|
+
|
18
|
+
describe 'incorrect values' do
|
19
|
+
[-1, 7, 'hello', nil].each do |incorrect_value|
|
20
|
+
output_value = incorrect_value.nil? ? '`nil`' : incorrect_value.to_s
|
21
|
+
it output_value + ' as a first week day should throw an error' do
|
22
|
+
expect do
|
23
|
+
CalendarWalker::MonthGrid.new(current_date, incorrect_value)
|
24
|
+
end.to raise_error CalendarWalker::IncorrectWDay
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe '#days_matrix' do
|
31
|
+
{
|
32
|
+
0 => { from: Date.new(2014, 4, 27), to: Date.new(2014, 5, 31) },
|
33
|
+
1 => { from: Date.new(2014, 4, 28), to: Date.new(2014, 6, 1) },
|
34
|
+
2 => { from: Date.new(2014, 4, 29), to: Date.new(2014, 6, 2) },
|
35
|
+
3 => { from: Date.new(2014, 4, 30), to: Date.new(2014, 6, 3) },
|
36
|
+
4 => { from: Date.new(2014, 5, 1), to: Date.new(2014, 6, 4) },
|
37
|
+
5 => { from: Date.new(2014, 4, 25), to: Date.new(2014, 6, 5) },
|
38
|
+
6 => { from: Date.new(2014, 4, 26), to: Date.new(2014, 6, 6) },
|
39
|
+
}.each do |first_wday, date_range|
|
40
|
+
it 'should return correct matrix when first week day is ' + Date::DAYNAMES[first_wday] do
|
41
|
+
grid = CalendarWalker::MonthGrid.new(current_date, first_wday)
|
42
|
+
grid.days_matrix.should eq week_grid(date_range[:from], date_range[:to])
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe '#wday_list' do
|
48
|
+
[
|
49
|
+
(0..6).to_a,
|
50
|
+
(1..6).to_a + [0],
|
51
|
+
(2..6).to_a + (0..1).to_a,
|
52
|
+
(3..6).to_a + (0..2).to_a,
|
53
|
+
(4..6).to_a + (0..3).to_a,
|
54
|
+
(5..6).to_a + (0..4).to_a,
|
55
|
+
[6] + (0..5).to_a
|
56
|
+
].each_with_index do |expect, first_wday|
|
57
|
+
it 'should print week days in proper order when first week day is ' + Date::DAYNAMES[first_wday] do
|
58
|
+
CalendarWalker::MonthGrid.new(current_date, first_wday).wday_list.should eq expect
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe CalendarWalker::MonthWalker::RowElement do
|
4
|
+
before do
|
5
|
+
@row_element = CalendarWalker::MonthWalker::RowElement.new('row_date', {a: [1, 2]})
|
6
|
+
end
|
7
|
+
|
8
|
+
describe '#date' do
|
9
|
+
it 'should return correct date' do
|
10
|
+
@row_element.date.should eq 'row_date'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe '#objects' do
|
15
|
+
it 'should return correct objects' do
|
16
|
+
@row_element.objects.should eq ({a: [1, 2]})
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,170 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe CalendarWalker::MonthWalker do
|
4
|
+
|
5
|
+
describe 'functional' do
|
6
|
+
|
7
|
+
let(:month_grid) { CalendarWalker::MonthGrid.new(Date.new(2014, 5, 9)) }
|
8
|
+
let(:calendar_walker) { CalendarWalker::MonthWalker.new(month_grid) }
|
9
|
+
|
10
|
+
describe '#month_grid' do
|
11
|
+
it 'should return received later month grid' do
|
12
|
+
calendar_walker.month_grid.should eq month_grid
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe '#each' do
|
17
|
+
|
18
|
+
it 'should walk through all dates in month grid' do
|
19
|
+
walk_result = []
|
20
|
+
calendar_walker.each do |row|
|
21
|
+
row_result = []
|
22
|
+
row.each do |row_element|
|
23
|
+
row_result.push row_element.date
|
24
|
+
end
|
25
|
+
walk_result.push row_result
|
26
|
+
end
|
27
|
+
|
28
|
+
walk_result.should eq month_grid.days_matrix
|
29
|
+
end
|
30
|
+
|
31
|
+
describe 'managing objects' do
|
32
|
+
|
33
|
+
let(:a_objects) do
|
34
|
+
[
|
35
|
+
double('a1', date: Date.new(2014, 5, 8)),
|
36
|
+
double('a2', date: Date.new(2014, 5, 8)),
|
37
|
+
double('a3', date: Date.new(2014, 4, 30)),
|
38
|
+
double('a4', date: Date.new(2014, 1, 1)),
|
39
|
+
]
|
40
|
+
end
|
41
|
+
|
42
|
+
let(:b_objects) do
|
43
|
+
[
|
44
|
+
double('b1', date_time: ::DateTime.new(2014, 5, 1, 12, 1, 1)),
|
45
|
+
double('b1', date_time: ::DateTime.new(2014, 5, 2, 23, 2, 2)),
|
46
|
+
]
|
47
|
+
end
|
48
|
+
|
49
|
+
describe 'with added objects' do
|
50
|
+
before do
|
51
|
+
calendar_walker.add_objects(:a, a_objects)
|
52
|
+
calendar_walker.add_objects(:b, b_objects, :date_time)
|
53
|
+
|
54
|
+
@result = {}
|
55
|
+
calendar_walker.each do |row|
|
56
|
+
row.each do |row_element|
|
57
|
+
@result[row_element.date] = row_element.objects
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'should not go through dates which a not in range' do
|
63
|
+
expect(@result[Date.new(2014, 1, 1)]).to be_nil
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'should get objects in lists, attached to appropriate dates' do
|
67
|
+
expect(@result[Date.new(2014, 5, 8)][:a]).to include(a_objects[0], a_objects[1])
|
68
|
+
expect(@result[Date.new(2014, 4, 30)][:a]).to include(a_objects[2])
|
69
|
+
|
70
|
+
expect(@result[Date.new(2014, 5, 1)][:b]).to include(b_objects[0])
|
71
|
+
expect(@result[Date.new(2014, 5, 2)][:b]).to include(b_objects[1])
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'should get empty list when there is no appropriate objects' do
|
75
|
+
expect(@result[Date.new(2014, 5, 12)][:a]).to eq []
|
76
|
+
expect(@result[Date.new(2014, 5, 12)][:b]).to eq []
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'should not get another objects (which were not added)' do
|
80
|
+
expect(@result[Date.new(2014, 5, 1)][:c]).to be_nil
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
|
85
|
+
describe 'with added and deleted objects' do
|
86
|
+
before do
|
87
|
+
calendar_walker.add_objects(:a, a_objects)
|
88
|
+
calendar_walker.add_objects(:b, b_objects, :date_time)
|
89
|
+
calendar_walker.remove_objects(:a)
|
90
|
+
|
91
|
+
@result = {}
|
92
|
+
calendar_walker.each do |row|
|
93
|
+
row.each do |row_element|
|
94
|
+
@result[row_element.date] = row_element.objects
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
it 'should not contain deleted objects' do
|
100
|
+
expect(@result[Date.new(2014, 5, 8)][:a]).to be_nil
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
|
108
|
+
describe 'unit' do
|
109
|
+
let(:month_grid) { double('month_grid', days_matrix: [[1, 2], [3, 4]]) }
|
110
|
+
let(:month_walker) { CalendarWalker::MonthWalker.new(month_grid) }
|
111
|
+
|
112
|
+
describe '#each' do
|
113
|
+
it 'should create a special object for each date in day matrix with empty object hash' do
|
114
|
+
[1, 2, 3, 4].each do |e|
|
115
|
+
CalendarWalker::MonthWalker::RowElement.should_receive(:new).with(e, {}).once
|
116
|
+
end
|
117
|
+
month_walker.each { }
|
118
|
+
end
|
119
|
+
|
120
|
+
it 'should pass to block arrays of special objects for each row of day matrix' do
|
121
|
+
row_elements = []
|
122
|
+
[1, 2, 3, 4].each do |e|
|
123
|
+
row_element = "row_#{e}"
|
124
|
+
CalendarWalker::MonthWalker::RowElement.should_receive(:new).with(e, {}).and_return(row_element)
|
125
|
+
row_elements.push row_element
|
126
|
+
end
|
127
|
+
|
128
|
+
row_array = []
|
129
|
+
month_walker.each do |row|
|
130
|
+
row_array.push row
|
131
|
+
end
|
132
|
+
|
133
|
+
expect(row_array[0]).to eq [row_elements[0], row_elements[1]]
|
134
|
+
expect(row_array[1]).to eq [row_elements[2], row_elements[3]]
|
135
|
+
end
|
136
|
+
|
137
|
+
|
138
|
+
describe 'with added objects' do
|
139
|
+
let(:a_objects) do
|
140
|
+
[
|
141
|
+
double('a1', m: double('a1_date', to_date: 1)),
|
142
|
+
double('a2', m: double('a2_date', to_date: 1)),
|
143
|
+
double('a3', m: double('a3_date', to_date: 3)),
|
144
|
+
double('a4', m: double('a4_date', to_date: 10))
|
145
|
+
]
|
146
|
+
end
|
147
|
+
|
148
|
+
let(:b_objects) { [double('b1', x: double('b1_date', to_date: 1))] }
|
149
|
+
|
150
|
+
before do
|
151
|
+
month_walker.add_objects(:a, a_objects, :m)
|
152
|
+
month_walker.add_objects(:b, b_objects, :x)
|
153
|
+
end
|
154
|
+
|
155
|
+
it 'should create a special object for each date in day matrix with appropriate object hash' do
|
156
|
+
CalendarWalker::MonthWalker::RowElement.should_receive(:new).with(1, {a: [a_objects[0], a_objects[1]], b: [b_objects[0]]}).once
|
157
|
+
CalendarWalker::MonthWalker::RowElement.should_receive(:new).with(2, {a: [], b: []}).once
|
158
|
+
CalendarWalker::MonthWalker::RowElement.should_receive(:new).with(3, {a: [a_objects[2]], b: []}).once
|
159
|
+
CalendarWalker::MonthWalker::RowElement.should_receive(:new).with(4, {a: [], b: []}).once
|
160
|
+
month_walker.each { }
|
161
|
+
end
|
162
|
+
|
163
|
+
it 'should not create special objects which are out of date range' do
|
164
|
+
CalendarWalker::MonthWalker::RowElement.should_receive(:new).with(10, anything).never
|
165
|
+
month_walker.each { }
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe CalendarWalker do
|
4
|
+
|
5
|
+
describe '#month_walker_for' do
|
6
|
+
it 'should create and return CalendarWalker::MonthWalker with correct month grid' do
|
7
|
+
month_grid = double('month_grid')
|
8
|
+
CalendarWalker::MonthGrid.should_receive(:new).with(Date.new(2014, 1, 1), 2).and_return(month_grid)
|
9
|
+
|
10
|
+
walker = double('month_walker')
|
11
|
+
CalendarWalker::MonthWalker.should_receive(:new).with(month_grid).and_return(walker)
|
12
|
+
|
13
|
+
CalendarWalker.month_walker_for(Date.new(2014, 1, 1), 2).should eq walker
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
it 'should fill CalendarWalker::MonthWalker object with received collections' do
|
18
|
+
walker = double('month_walker')
|
19
|
+
CalendarWalker::MonthWalker.should_receive(:new).and_return(walker)
|
20
|
+
|
21
|
+
walker.should_receive(:add_objects).with(:events, [1, 2, 3], :event_date).once
|
22
|
+
walker.should_receive(:add_objects).with(:dates, [6, 7])
|
23
|
+
|
24
|
+
CalendarWalker.month_walker_for(
|
25
|
+
Date.new(2014, 1, 1),
|
26
|
+
2,
|
27
|
+
{ events: {collection: [1, 2, 3], date_method: :event_date}, dates: { collection: [6, 7] } }
|
28
|
+
)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: calendar_walker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pavel Makarov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-05-
|
11
|
+
date: 2014-05-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -69,9 +69,16 @@ files:
|
|
69
69
|
- Rakefile
|
70
70
|
- calendar_walker.gemspec
|
71
71
|
- lib/calendar_walker.rb
|
72
|
+
- lib/calendar_walker/month_grid.rb
|
73
|
+
- lib/calendar_walker/month_walker.rb
|
74
|
+
- lib/calendar_walker/month_walker/row_element.rb
|
72
75
|
- lib/calendar_walker/version.rb
|
73
|
-
-
|
74
|
-
- spec/calendar_walker/
|
76
|
+
- spec/calendar_walker/month_grid_spec.rb
|
77
|
+
- spec/calendar_walker/month_walker/row_element_spec.rb
|
78
|
+
- spec/calendar_walker/month_walker_spec.rb
|
79
|
+
- spec/calendar_walker_spec.rb
|
80
|
+
- spec/spec_helper.rb
|
81
|
+
- spec/support/grid_support.rb
|
75
82
|
homepage: https://github.com/violarium/calendar_walker
|
76
83
|
licenses:
|
77
84
|
- MIT
|
@@ -97,4 +104,9 @@ signing_key:
|
|
97
104
|
specification_version: 4
|
98
105
|
summary: Simple gem to walk through calendar months.
|
99
106
|
test_files:
|
100
|
-
- spec/calendar_walker/
|
107
|
+
- spec/calendar_walker/month_grid_spec.rb
|
108
|
+
- spec/calendar_walker/month_walker/row_element_spec.rb
|
109
|
+
- spec/calendar_walker/month_walker_spec.rb
|
110
|
+
- spec/calendar_walker_spec.rb
|
111
|
+
- spec/spec_helper.rb
|
112
|
+
- spec/support/grid_support.rb
|
@@ -1,57 +0,0 @@
|
|
1
|
-
require 'calendar_walker'
|
2
|
-
|
3
|
-
describe CalendarWalker::Walker do
|
4
|
-
let(:current_date) { DateTime.new(2014, 5, 5) }
|
5
|
-
|
6
|
-
describe '#current_date' do
|
7
|
-
it 'should return same current date' do
|
8
|
-
walker = CalendarWalker::Walker.new(current_date)
|
9
|
-
walker.current_date.should eq current_date
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
describe '#first_wday' do
|
14
|
-
it 'should be Sunday by default' do
|
15
|
-
CalendarWalker::Walker.new(current_date).first_wday.should eq 0
|
16
|
-
end
|
17
|
-
it 'should return received wday number' do
|
18
|
-
CalendarWalker::Walker.new(current_date, 4).first_wday.should eq 4
|
19
|
-
end
|
20
|
-
it 'should throw an error when week day is incorrect' do
|
21
|
-
expect do
|
22
|
-
CalendarWalker::Walker.new(current_date, -1)
|
23
|
-
end.to raise_error CalendarWalker::IncorrectWDay
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
describe '#days_matrix' do
|
28
|
-
context 'first wday is Sunday' do
|
29
|
-
let(:walker) { CalendarWalker::Walker.new(current_date) }
|
30
|
-
it 'should print days in proper order' do
|
31
|
-
walker.days_matrix.should eq (Date.new(2014, 4, 27)..Date.new(2014, 5, 31)).to_a.each_slice(7).to_a
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
context 'first day is Monday' do
|
36
|
-
let(:walker) { CalendarWalker::Walker.new(current_date, 1) }
|
37
|
-
it 'should print days in proper order' do
|
38
|
-
walker.days_matrix.should eq (Date.new(2014, 4, 28)..Date.new(2014, 6, 1)).to_a.each_slice(7).to_a
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
|
44
|
-
describe '#wday_list' do
|
45
|
-
context 'first wday is Sunday' do
|
46
|
-
it 'should print week days in proper order' do
|
47
|
-
CalendarWalker::Walker.new(current_date).wday_list.should eq [0, 1, 2, 3, 4, 5, 6]
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
context 'first day is Monday' do
|
52
|
-
it 'should print week days in proper order' do
|
53
|
-
CalendarWalker::Walker.new(current_date, 1).wday_list.should eq [1, 2, 3, 4, 5, 6, 0]
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|