tick_tack 0.1.0 → 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/README.md +1 -0
- data/lib/tick_tack.rb +6 -8
- data/lib/tick_tack/calendar.rb +43 -0
- data/lib/tick_tack/day.rb +44 -15
- data/lib/tick_tack/month.rb +50 -14
- data/lib/tick_tack/scope/day.rb +17 -0
- data/lib/tick_tack/scope/month.rb +17 -0
- data/lib/tick_tack/scope/year.rb +11 -0
- data/lib/tick_tack/version.rb +1 -1
- data/lib/tick_tack/year.rb +43 -9
- data/tick_tack.gemspec +3 -0
- metadata +8 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 754c58f89be99bf3d6205cdc6a1e6de8b3fb7241
|
4
|
+
data.tar.gz: 23722a172c09b1678410806560c208a1a7235eb0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8da0da836bb99b1024a5bc76adda2b2add3a0973be1aed88f69b9ad069b44f906dc60a16d57d9b3db09e5d04ace78ecdbfabcd688c459599ace2d755e44b8014
|
7
|
+
data.tar.gz: 390ed8a0c11dcc465bc49d9dc679aa706310b4ec0cda350c7818e5697735540d28b036f3885e270e22caacfa5f493e5c55a7c99de876b0336b375e097323883c
|
data/README.md
CHANGED
data/lib/tick_tack.rb
CHANGED
@@ -1,18 +1,16 @@
|
|
1
1
|
require 'date'
|
2
2
|
|
3
3
|
require 'tick_tack/version'
|
4
|
+
|
5
|
+
require 'tick_tack/scope/year'
|
6
|
+
require 'tick_tack/scope/month'
|
7
|
+
require 'tick_tack/scope/day'
|
8
|
+
|
9
|
+
require 'tick_tack/calendar'
|
4
10
|
require 'tick_tack/year'
|
5
11
|
require 'tick_tack/month'
|
6
12
|
require 'tick_tack/day'
|
7
13
|
|
8
14
|
module TickTack
|
9
|
-
@now = nil
|
10
|
-
|
11
|
-
def self.now=(now)
|
12
|
-
@now = now
|
13
|
-
end
|
14
15
|
|
15
|
-
def self.now
|
16
|
-
@now.nil? ? Date.new : @now
|
17
|
-
end
|
18
16
|
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module TickTack
|
2
|
+
class Calendar
|
3
|
+
|
4
|
+
attr_reader :conf
|
5
|
+
|
6
|
+
def initialize(now = nil, conf = {})
|
7
|
+
self.now = now
|
8
|
+
@conf = {
|
9
|
+
dow: 0 # day of week - 0 = sunday, 1 = monday
|
10
|
+
}.merge!(conf)
|
11
|
+
end
|
12
|
+
|
13
|
+
def now=(now)
|
14
|
+
@now = now
|
15
|
+
end
|
16
|
+
|
17
|
+
def now
|
18
|
+
@now.nil? ? DateTime.now.to_date : @now
|
19
|
+
end
|
20
|
+
|
21
|
+
def year(*args)
|
22
|
+
self.class.factory(:Year).new(self, *args)
|
23
|
+
end
|
24
|
+
|
25
|
+
def month(*args)
|
26
|
+
self.class.factory(:Month).new(self, *args)
|
27
|
+
end
|
28
|
+
|
29
|
+
def day(*args)
|
30
|
+
self.class.factory(:Day).new(self, *args)
|
31
|
+
end
|
32
|
+
|
33
|
+
protected
|
34
|
+
|
35
|
+
def self.factory class_id
|
36
|
+
{
|
37
|
+
:Year => Year,
|
38
|
+
:Month => Month,
|
39
|
+
:Day => Day
|
40
|
+
}[class_id]
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/lib/tick_tack/day.rb
CHANGED
@@ -1,24 +1,53 @@
|
|
1
1
|
module TickTack
|
2
2
|
class Day
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
@
|
10
|
-
|
3
|
+
include Comparable
|
4
|
+
include TickTack::Scope::Day
|
5
|
+
|
6
|
+
attr_reader :date
|
7
|
+
|
8
|
+
def initialize(calendar, year_i = nil, month_i = nil, day_i = nil)
|
9
|
+
@calendar = calendar
|
10
|
+
init_year(year_i)
|
11
|
+
init_month(month_i)
|
12
|
+
init_day(day_i)
|
13
|
+
|
14
|
+
@date = Date.new(self.year_i, self.month_i, self.day_i)
|
15
|
+
end
|
16
|
+
|
17
|
+
def week_start
|
18
|
+
week_start = date - (date.wday - @calendar.conf[:dow]) % 7
|
19
|
+
@calendar.day(week_start.year, week_start.month, week_start.day)
|
20
|
+
end
|
21
|
+
|
22
|
+
def week_end
|
23
|
+
week_end = week_start.date + 6
|
24
|
+
@calendar.day(week_end.year, week_end.month, week_end.day)
|
11
25
|
end
|
12
26
|
|
13
|
-
def
|
14
|
-
|
27
|
+
def weekdays
|
28
|
+
(week_start..week_end).to_a
|
15
29
|
end
|
16
30
|
|
17
|
-
def
|
18
|
-
|
19
|
-
|
20
|
-
o.month == month &&
|
21
|
-
o.day == day
|
31
|
+
def next
|
32
|
+
next_date = @date.next_day
|
33
|
+
@calendar.day(next_date.year, next_date.month, next_date.day)
|
22
34
|
end
|
35
|
+
alias_method :succ, :next
|
36
|
+
|
37
|
+
def previous
|
38
|
+
prev_date = @date.prev_day
|
39
|
+
@calendar.day(prev_date.year, prev_date.month, prev_date.day)
|
40
|
+
end
|
41
|
+
alias_method :prev, :previous
|
42
|
+
|
43
|
+
def <=>(other)
|
44
|
+
self.date <=> other.date
|
45
|
+
end
|
46
|
+
|
47
|
+
# def inspect
|
48
|
+
# "#{year_i}-#{month_i}-#{day_i}"
|
49
|
+
# end
|
50
|
+
|
51
|
+
private
|
23
52
|
end
|
24
53
|
end
|
data/lib/tick_tack/month.rb
CHANGED
@@ -1,30 +1,66 @@
|
|
1
1
|
module TickTack
|
2
2
|
class Month
|
3
|
-
|
4
|
-
|
3
|
+
include Comparable
|
4
|
+
include TickTack::Scope::Month
|
5
5
|
|
6
|
-
def initialize(
|
7
|
-
@
|
8
|
-
|
6
|
+
def initialize(calendar, year_i = nil, month_i = nil)
|
7
|
+
@calendar = calendar
|
8
|
+
|
9
|
+
init_year(year_i)
|
10
|
+
init_month(month_i)
|
11
|
+
|
12
|
+
@first_day_date = Date.new(self.year_i, self.month_i, 1)
|
9
13
|
end
|
10
14
|
|
11
15
|
def first_day
|
12
|
-
|
16
|
+
@calendar.day(year_i, month_i, 1)
|
13
17
|
end
|
14
18
|
|
15
19
|
def last_day
|
16
|
-
date = (
|
17
|
-
|
20
|
+
date = (@first_day_date >> 1) - 1
|
21
|
+
@calendar.day(year_i, month_i, date.day)
|
22
|
+
end
|
23
|
+
|
24
|
+
def day(day_i)
|
25
|
+
@calendar.day(year_i, month_i, day_i)
|
26
|
+
end
|
27
|
+
|
28
|
+
def weekdays
|
29
|
+
first = first_day.week_start
|
30
|
+
last = last_day.week_end
|
31
|
+
weeks = []
|
32
|
+
|
33
|
+
while first < last
|
34
|
+
weeks << first.weekdays
|
35
|
+
first = first.week_end.next
|
36
|
+
end
|
37
|
+
|
38
|
+
weeks
|
39
|
+
end
|
40
|
+
|
41
|
+
def contains?(other)
|
42
|
+
range = (first_day.date.jd..last_day.date.jd)
|
43
|
+
if other.is_a? Day
|
44
|
+
range === other.date.jd
|
45
|
+
else
|
46
|
+
false
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def next
|
51
|
+
next_date = @first_day_date.next_month
|
52
|
+
@calendar.month(next_date.year, next_date.month)
|
18
53
|
end
|
54
|
+
alias_method :succ, :next
|
19
55
|
|
20
|
-
def
|
21
|
-
|
56
|
+
def previous
|
57
|
+
prev_date = @first_day_date.prev_month
|
58
|
+
@calendar.month(prev_date.year, prev_date.month)
|
22
59
|
end
|
60
|
+
alias_method :prev, :previous
|
23
61
|
|
24
|
-
def
|
25
|
-
|
26
|
-
o.year == year &&
|
27
|
-
o.month == month
|
62
|
+
def <=>(other)
|
63
|
+
self.first_day.date <=> other.first_day.date
|
28
64
|
end
|
29
65
|
end
|
30
66
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module TickTack
|
2
|
+
module Scope
|
3
|
+
module Day
|
4
|
+
include TickTack::Scope::Month
|
5
|
+
|
6
|
+
attr_accessor :day_i
|
7
|
+
|
8
|
+
def init_day(day_i = nil)
|
9
|
+
@day_i = day_i || @calendar.now.day
|
10
|
+
end
|
11
|
+
|
12
|
+
def month
|
13
|
+
@calendar.month(year_i, month_i)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module TickTack
|
2
|
+
module Scope
|
3
|
+
module Month
|
4
|
+
include TickTack::Scope::Year
|
5
|
+
|
6
|
+
attr_accessor :month_i
|
7
|
+
|
8
|
+
def init_month(month_i = nil)
|
9
|
+
@month_i = month_i || @calendar.now.month
|
10
|
+
end
|
11
|
+
|
12
|
+
def year
|
13
|
+
@calendar.year(year_i)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/tick_tack/version.rb
CHANGED
data/lib/tick_tack/year.rb
CHANGED
@@ -1,25 +1,59 @@
|
|
1
1
|
module TickTack
|
2
2
|
class Year
|
3
|
-
|
3
|
+
include Comparable
|
4
|
+
include TickTack::Scope::Year
|
4
5
|
|
5
|
-
def initialize(
|
6
|
-
@
|
6
|
+
def initialize(calendar, year_i = nil)
|
7
|
+
@calendar = calendar
|
8
|
+
init_year(year_i)
|
9
|
+
|
10
|
+
@first_day_date = Date.new(self.year_i, 1, 1)
|
7
11
|
end
|
8
12
|
|
9
13
|
def first_day
|
10
|
-
|
14
|
+
@calendar.day(year_i, 1, 1)
|
11
15
|
end
|
12
16
|
|
13
17
|
def last_day
|
14
|
-
|
18
|
+
date = (@first_day_date >> 12) - 1
|
19
|
+
@calendar.day(year_i, date.month, date.day)
|
20
|
+
end
|
21
|
+
|
22
|
+
def month(month_i)
|
23
|
+
@calendar.month(year_i, month_i)
|
24
|
+
end
|
25
|
+
|
26
|
+
def months
|
27
|
+
(1..12).map do |i|
|
28
|
+
month(i)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def contains?(other)
|
33
|
+
range = (first_day.date.jd..last_day.date.jd)
|
34
|
+
if other.is_a? Month
|
35
|
+
range === other.first_day.date.jd
|
36
|
+
elsif other.is_a? Day
|
37
|
+
range === other.date.jd
|
38
|
+
else
|
39
|
+
false
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def next
|
44
|
+
next_date = @first_day_date.next_year
|
45
|
+
@calendar.year(next_date.year)
|
15
46
|
end
|
47
|
+
alias_method :succ, :next
|
16
48
|
|
17
|
-
def
|
18
|
-
|
49
|
+
def previous
|
50
|
+
prev_date = @first_day_date.prev_year
|
51
|
+
@calendar.year(prev_date.year)
|
19
52
|
end
|
53
|
+
alias_method :prev, :previous
|
20
54
|
|
21
|
-
def
|
22
|
-
|
55
|
+
def <=>(other)
|
56
|
+
self.first_day.date <=> other.first_day.date
|
23
57
|
end
|
24
58
|
end
|
25
59
|
end
|
data/tick_tack.gemspec
CHANGED
@@ -14,6 +14,9 @@ Gem::Specification.new do |spec|
|
|
14
14
|
spec.homepage = "https://github.com/tomraithel/tick_tack"
|
15
15
|
spec.license = "MIT"
|
16
16
|
|
17
|
+
spec.required_ruby_version = '>= 2.2.2'
|
18
|
+
spec.required_rubygems_version = '>= 2.0.0'
|
19
|
+
|
17
20
|
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
18
21
|
# delete this section to allow pushing this gem to any host.
|
19
22
|
if spec.respond_to?(:metadata)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tick_tack
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tom Raithel
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-01-
|
11
|
+
date: 2016-01-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -70,8 +70,12 @@ files:
|
|
70
70
|
- bin/console
|
71
71
|
- bin/setup
|
72
72
|
- lib/tick_tack.rb
|
73
|
+
- lib/tick_tack/calendar.rb
|
73
74
|
- lib/tick_tack/day.rb
|
74
75
|
- lib/tick_tack/month.rb
|
76
|
+
- lib/tick_tack/scope/day.rb
|
77
|
+
- lib/tick_tack/scope/month.rb
|
78
|
+
- lib/tick_tack/scope/year.rb
|
75
79
|
- lib/tick_tack/version.rb
|
76
80
|
- lib/tick_tack/year.rb
|
77
81
|
- tick_tack.gemspec
|
@@ -88,12 +92,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
88
92
|
requirements:
|
89
93
|
- - ">="
|
90
94
|
- !ruby/object:Gem::Version
|
91
|
-
version:
|
95
|
+
version: 2.2.2
|
92
96
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
97
|
requirements:
|
94
98
|
- - ">="
|
95
99
|
- !ruby/object:Gem::Version
|
96
|
-
version:
|
100
|
+
version: 2.0.0
|
97
101
|
requirements: []
|
98
102
|
rubyforge_project:
|
99
103
|
rubygems_version: 2.5.1
|