mmb-seasonal 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/{README → README.textile} +17 -7
- data/lib/seasonal.rb +27 -4
- data/seasonal.gemspec +2 -2
- data/test/seasonal_test.rb +98 -20
- metadata +2 -2
data/{README → README.textile}
RENAMED
@@ -3,19 +3,25 @@ events are currently happening.
|
|
3
3
|
|
4
4
|
Example (print holiday greetings):
|
5
5
|
|
6
|
+
<pre>
|
7
|
+
<code>
|
6
8
|
require 'seasonal'
|
7
9
|
|
8
10
|
holidays = Seasonal::Calendar.new
|
9
11
|
|
10
12
|
holidays.push(Seasonal::Event.new('Merry Christmas', 'America/New_York',
|
11
|
-
:
|
13
|
+
:on => 'dec 25'))
|
12
14
|
holidays.push(Seasonal::Event.new("April Fool's Day", 'America/New_York',
|
13
|
-
:
|
15
|
+
:on => 'apr 1'))
|
14
16
|
|
15
|
-
holidays.going_on { |e| puts e
|
17
|
+
holidays.going_on { |e| puts e }
|
18
|
+
</code>
|
19
|
+
</pre>
|
16
20
|
|
17
21
|
Example (make text red between 9:00 and 10:00 am):
|
18
22
|
|
23
|
+
<pre>
|
24
|
+
<code>
|
19
25
|
require 'seasonal'
|
20
26
|
|
21
27
|
colors = Seasonal::Calendar.new
|
@@ -23,13 +29,15 @@ colors = Seasonal::Calendar.new
|
|
23
29
|
colors.push(Seasonal::Event.new('#ff0000', 'America/New_York',
|
24
30
|
:start => '9:00am', :end => '10:00am'))
|
25
31
|
|
26
|
-
color = '#000000'
|
27
|
-
colors.going_on { |e| color = e.payload }
|
28
|
-
|
32
|
+
color = colors.going_on(:or_if_none => '#000000')
|
29
33
|
puts "<p style=\"background-color : #{color}\">test</p>"
|
34
|
+
</code>
|
35
|
+
</pre>
|
30
36
|
|
31
37
|
Example (sale ends Jul 10):
|
32
38
|
|
39
|
+
<pre>
|
40
|
+
<code>
|
33
41
|
require 'seasonal'
|
34
42
|
|
35
43
|
sales = Seasonal::Calendar.new
|
@@ -37,7 +45,9 @@ sales = Seasonal::Calendar.new
|
|
37
45
|
sales.push(Seasonal::Event.new('Everything on sale', 'America/New_York',
|
38
46
|
:end => 'jul 10 2009'))
|
39
47
|
|
40
|
-
sales.going_on { |e| puts e
|
48
|
+
sales.going_on { |e| puts e }
|
49
|
+
</code>
|
50
|
+
</pre>
|
41
51
|
|
42
52
|
Each event has an attached payload which can be any object.
|
43
53
|
|
data/lib/seasonal.rb
CHANGED
@@ -14,8 +14,13 @@ module Seasonal
|
|
14
14
|
def initialize(payload, zone, options={})
|
15
15
|
@payload = payload
|
16
16
|
@zone = zone
|
17
|
-
|
18
|
-
|
17
|
+
if options[:on]
|
18
|
+
@start = "#{options[:on]} 00:00:00"
|
19
|
+
@ennd = "#{options[:on]} 23:59:59"
|
20
|
+
else
|
21
|
+
@start = options[:start]
|
22
|
+
@ennd = options[:end]
|
23
|
+
end
|
19
24
|
end
|
20
25
|
|
21
26
|
def start_utc
|
@@ -54,8 +59,26 @@ module Seasonal
|
|
54
59
|
|
55
60
|
class Calendar < Array
|
56
61
|
|
57
|
-
def going_on(
|
58
|
-
|
62
|
+
def going_on(options={})
|
63
|
+
options = { :at => Time.now, :payloads => true }.merge(options)
|
64
|
+
if block_given?
|
65
|
+
if options[:payloads]
|
66
|
+
each { |event| yield event.payload if event.going_on?(options[:at]) }
|
67
|
+
else
|
68
|
+
each { |event| yield event if event.going_on?(options[:at]) }
|
69
|
+
end
|
70
|
+
else
|
71
|
+
result = reject { |event| !event.going_on?(options[:at]) }
|
72
|
+
if options[:payloads]
|
73
|
+
if result.empty? and !options[:or_if_none].nil?
|
74
|
+
result.push(options[:or_if_none])
|
75
|
+
else
|
76
|
+
result.collect { |e| e.payload }
|
77
|
+
end
|
78
|
+
else
|
79
|
+
result
|
80
|
+
end
|
81
|
+
end
|
59
82
|
end
|
60
83
|
|
61
84
|
end
|
data/seasonal.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "seasonal"
|
3
|
-
s.version = "0.0.
|
3
|
+
s.version = "0.0.4"
|
4
4
|
s.date = "2008-11-10"
|
5
5
|
s.summary = "create logic based on defined date and time events"
|
6
6
|
s.email = "matthewm@boedicker.org"
|
@@ -10,7 +10,7 @@ Gem::Specification.new do |s|
|
|
10
10
|
s.authors = ["Matthew M. Boedicker"]
|
11
11
|
s.files = [
|
12
12
|
"lib/seasonal.rb",
|
13
|
-
"README",
|
13
|
+
"README.textile",
|
14
14
|
"seasonal.gemspec",
|
15
15
|
]
|
16
16
|
s.test_files = [
|
data/test/seasonal_test.rb
CHANGED
@@ -6,13 +6,24 @@ require 'test/unit'
|
|
6
6
|
|
7
7
|
class SeasonalTest < Test::Unit::TestCase
|
8
8
|
|
9
|
-
def
|
9
|
+
def test_event_none
|
10
10
|
e = Seasonal::Event.new(nil, 'America/New_York')
|
11
11
|
|
12
12
|
assert_equal(false, e.going_on?)
|
13
13
|
end
|
14
14
|
|
15
|
-
def
|
15
|
+
def test_event_start
|
16
|
+
ss = 'jan 2'
|
17
|
+
start = Time.parse(ss).utc
|
18
|
+
|
19
|
+
e = Seasonal::Event.new(nil, 'America/New_York', :start => ss)
|
20
|
+
|
21
|
+
assert_equal(false, e.going_on?(start - 1))
|
22
|
+
assert(e.going_on?(start))
|
23
|
+
assert(e.going_on?(start + 1))
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_event_end
|
16
27
|
es = 'mar 17'
|
17
28
|
ennd = Time.parse(es).utc
|
18
29
|
|
@@ -23,36 +34,103 @@ class SeasonalTest < Test::Unit::TestCase
|
|
23
34
|
assert_equal(false, e.going_on?(ennd + 1))
|
24
35
|
end
|
25
36
|
|
26
|
-
def
|
27
|
-
ss = '
|
37
|
+
def test_event_start_end
|
38
|
+
ss = 'aug 29'
|
39
|
+
es = 'sep 1'
|
28
40
|
start = Time.parse(ss).utc
|
41
|
+
ennd = Time.parse(es).utc
|
29
42
|
|
30
|
-
e = Seasonal::Event.new(nil, 'America/New_York', :start => ss)
|
43
|
+
e = Seasonal::Event.new(nil, 'America/New_York', :start => ss, :end => es)
|
31
44
|
|
32
45
|
assert_equal(false, e.going_on?(start - 1))
|
33
46
|
assert(e.going_on?(start))
|
34
|
-
assert(e.going_on?(
|
47
|
+
assert(e.going_on?(ennd))
|
48
|
+
assert_equal(false, e.going_on?(ennd + 1))
|
35
49
|
end
|
36
50
|
|
37
|
-
def
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
51
|
+
def test_calendar_going_on
|
52
|
+
calendar = Seasonal::Calendar.new
|
53
|
+
calendar.push(Seasonal::Event.new(nil, 'America/New_York',
|
54
|
+
:start => 'June 22, 1978 10:00pm', :end => 'June 28, 1978 10:00am'))
|
55
|
+
assert_equal(1, calendar.going_on(
|
56
|
+
:at => Time.utc(1978, 6, 23, 2, 0, 0)).size)
|
57
|
+
end
|
42
58
|
|
43
|
-
|
59
|
+
def test_calendar_going_on_or_if_none_some
|
60
|
+
calendar = Seasonal::Calendar.new
|
61
|
+
calendar.push(Seasonal::Event.new(nil, 'America/New_York',
|
62
|
+
:start => 'Sep 14, 1999 1:10pm', :end => 'Sep 15, 1999 12:00am'))
|
63
|
+
assert_equal(1, calendar.going_on(
|
64
|
+
:at => Time.utc(1999, 9, 14, 18, 11, 1)).size)
|
65
|
+
end
|
44
66
|
|
45
|
-
|
67
|
+
def test_calendar_going_on_or_if_none_none
|
68
|
+
calendar = Seasonal::Calendar.new
|
69
|
+
calendar.push(Seasonal::Event.new(nil, 'America/New_York',
|
70
|
+
:start => '05/01/2007', :end => '05/03/2007'))
|
71
|
+
or_if_none = 'default'
|
72
|
+
assert_equal(1, calendar.going_on(:at => Time.utc(2007, 5, 5, 0, 0, 0),
|
73
|
+
:or_if_none => or_if_none).size)
|
74
|
+
assert_equal(or_if_none, calendar.going_on(
|
75
|
+
:at => Time.utc(2007, 5, 5, 0, 0, 0), :or_if_none => or_if_none).first)
|
76
|
+
end
|
46
77
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
78
|
+
def test_calendar_going_on_block
|
79
|
+
calendar = Seasonal::Calendar.new
|
80
|
+
payload = 'test'
|
81
|
+
calendar.push(Seasonal::Event.new(payload, 'America/New_York',
|
82
|
+
:start => 'apr 11', :end => 'may 5'))
|
83
|
+
result = false
|
84
|
+
calendar.going_on(:at => Time.utc(Time.now.utc.year, 4, 16, 1, 2, 3)) do |e|
|
85
|
+
assert(payload, e)
|
86
|
+
result = true
|
53
87
|
end
|
88
|
+
assert(result)
|
89
|
+
end
|
54
90
|
|
55
|
-
|
91
|
+
def test_calendar_going_on_block_whole_event
|
92
|
+
calendar = Seasonal::Calendar.new
|
93
|
+
payload = 'test'
|
94
|
+
calendar.push(Seasonal::Event.new(payload, 'America/New_York',
|
95
|
+
:start => 'apr 11', :end => 'may 5'))
|
96
|
+
result = false
|
97
|
+
calendar.going_on(:at => Time.utc(Time.now.utc.year, 4, 16, 1, 2, 3),
|
98
|
+
:payloads => false) do |e|
|
99
|
+
assert(payload, e.payload)
|
100
|
+
result = true
|
101
|
+
end
|
102
|
+
assert(result)
|
103
|
+
end
|
104
|
+
|
105
|
+
def test_calendar_on
|
106
|
+
calendar = Seasonal::Calendar.new
|
107
|
+
on = Time.now
|
108
|
+
calendar.push(Seasonal::Event.new(nil, 'America/New_York',
|
109
|
+
:on => on.strftime('%b %d')))
|
110
|
+
assert_equal(1, calendar.going_on(:at => on).size)
|
111
|
+
end
|
112
|
+
|
113
|
+
def test_calendar_payload
|
114
|
+
calendar = Seasonal::Calendar.new
|
115
|
+
payload = 'test'
|
116
|
+
on = Time.now
|
117
|
+
calendar.push(Seasonal::Event.new(payload, 'America/New_York',
|
118
|
+
:on => on.strftime('%b %d')))
|
119
|
+
assert_equal(1, calendar.going_on(:at => on).size)
|
120
|
+
assert_equal(payload, calendar.going_on(:at => on).first)
|
121
|
+
end
|
122
|
+
|
123
|
+
def test_calendar_whole_event
|
124
|
+
calendar = Seasonal::Calendar.new
|
125
|
+
payload = 'test'
|
126
|
+
on = Time.now
|
127
|
+
calendar.push(Seasonal::Event.new(payload, 'America/New_York',
|
128
|
+
:on => on.strftime('%b %d')))
|
129
|
+
assert_equal(1, calendar.going_on(:at => on, :payloads => false).size)
|
130
|
+
assert_kind_of(Seasonal::Event, calendar.going_on(:at => on,
|
131
|
+
:payloads => false).first)
|
132
|
+
assert_equal(payload, calendar.going_on(:at => on,
|
133
|
+
:payloads => false).first.payload)
|
56
134
|
end
|
57
135
|
|
58
136
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mmb-seasonal
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthew M. Boedicker
|
@@ -31,7 +31,7 @@ extra_rdoc_files: []
|
|
31
31
|
|
32
32
|
files:
|
33
33
|
- lib/seasonal.rb
|
34
|
-
- README
|
34
|
+
- README.textile
|
35
35
|
- seasonal.gemspec
|
36
36
|
has_rdoc: false
|
37
37
|
homepage: http://github.com/mmb/seasonal
|