mmb-seasonal 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -7,10 +7,10 @@ require 'seasonal'
7
7
 
8
8
  holidays = Seasonal::Calendar.new
9
9
 
10
- holidays.push(Seasonal::Event.new(
11
- 'Merry Christmas', 'America/New_York', 'dec 25'))
12
- holidays.push(Seasonal::Event.new(
13
- "April Fool's Day", 'America/New_York', 'apr 1'))
10
+ holidays.push(Seasonal::Event.new('Merry Christmas', 'America/New_York',
11
+ :start => 'dec 25 00:00', :end => 'dec 25 23:59:59'))
12
+ holidays.push(Seasonal::Event.new("April Fool's Day", 'America/New_York',
13
+ :start => 'apr 1 00:00', :end => 'apr 1 23:59:59'))
14
14
 
15
15
  holidays.going_on { |e| puts e.payload }
16
16
 
@@ -20,14 +20,25 @@ require 'seasonal'
20
20
 
21
21
  colors = Seasonal::Calendar.new
22
22
 
23
- colors.push(Seasonal::Event.new(
24
- '#ff0000', 'America/New_York', nil, '9:00am', nil, '10:00am'))
23
+ colors.push(Seasonal::Event.new('#ff0000', 'America/New_York',
24
+ :start => '9:00am', :end => '10:00am'))
25
25
 
26
26
  color = '#000000'
27
27
  colors.going_on { |e| color = e.payload }
28
28
 
29
29
  puts "<p style=\"background-color : #{color}\">test</p>"
30
30
 
31
+ Example (sale ends Jul 10):
32
+
33
+ require 'seasonal'
34
+
35
+ sales = Seasonal::Calendar.new
36
+
37
+ sales.push(Seasonal::Event.new('Everything on sale', 'America/New_York',
38
+ :end => 'jul 10 2009'))
39
+
40
+ sales.going_on { |e| puts e.payload }
41
+
31
42
  Each event has an attached payload which can be any object.
32
43
 
33
44
  Uses the flexible time parsing of Time.parse to be easy to use and the
data/lib/seasonal.rb CHANGED
@@ -8,50 +8,46 @@ module Seasonal
8
8
  class Event
9
9
  attr_accessor :payload
10
10
  attr_accessor :zone
11
- attr_accessor :start_date
12
- attr_accessor :start_time
13
- attr_accessor :end_date
14
- attr_accessor :end_time
15
-
16
- def initialize(payload, zone, start_date, start_time=nil, end_date=nil,
17
- end_time=nil)
18
- if end_time.nil?
19
- if start_time.nil?
20
- end_time = '23:59:59'
21
- else
22
- if end_date.nil?
23
- end_time = start_time
24
- else
25
- end_time = '23:59:59'
26
- end
27
- end
28
- end
29
-
30
- start_time = '00:00:00' if start_time.nil?
31
-
32
- end_date = start_date if end_date.nil?
11
+ attr_accessor :start
12
+ attr_accessor :ennd
33
13
 
14
+ def initialize(payload, zone, options={})
34
15
  @payload = payload
35
16
  @zone = zone
36
- @start_date = start_date
37
- @end_date = end_date
38
- @start_time = start_time
39
- @end_time = end_time
17
+ @start = options[:start]
18
+ @ennd = options[:end]
40
19
  end
41
20
 
42
- def start
43
- tz = TZInfo::Timezone.get(zone)
44
- tz.local_to_utc(Time.parse("#{start_date} #{start_time}"))
21
+ def start_utc
22
+ unless start.nil?
23
+ tz = TZInfo::Timezone.get(zone)
24
+ tz.local_to_utc(Time.parse(start))
25
+ end
45
26
  end
46
27
 
47
- def ennd
48
- tz = TZInfo::Timezone.get(zone)
49
- tz.local_to_utc(Time.parse("#{end_date} #{end_time}"))
28
+ def end_utc
29
+ unless ennd.nil?
30
+ tz = TZInfo::Timezone.get(zone)
31
+ tz.local_to_utc(Time.parse(ennd))
32
+ end
50
33
  end
51
34
 
52
35
  def going_on?(test_time=Time.now)
53
- # puts "#{start} - #{ennd}"
54
- test_time >= start and test_time <= ennd
36
+ # puts "#{start_utc} - #{end_utc}"
37
+ if start_utc.nil?
38
+ if end_utc.nil?
39
+ result = false
40
+ else
41
+ result = test_time <= end_utc
42
+ end
43
+ else
44
+ if end_utc.nil?
45
+ result = test_time >= start_utc
46
+ else
47
+ result = (test_time >= start_utc and test_time <= end_utc)
48
+ end
49
+ end
50
+ result
55
51
  end
56
52
 
57
53
  end
data/seasonal.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "seasonal"
3
- s.version = "0.0.2"
4
- s.date = "2008-11-07"
3
+ s.version = "0.0.3"
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"
7
7
  s.homepage = "http://github.com/mmb/seasonal"
@@ -6,101 +6,53 @@ require 'test/unit'
6
6
 
7
7
  class SeasonalTest < Test::Unit::TestCase
8
8
 
9
- def test_start_date
10
- e = Seasonal::Event.new(nil, 'America/New_York', 'jan 1')
9
+ def test_none
10
+ e = Seasonal::Event.new(nil, 'America/New_York')
11
11
 
12
- assert_equal(false,
13
- e.going_on?(Time.utc(Time.now.utc.year - 1, 12, 30, 23, 59, 59)))
14
-
15
- assert(e.going_on?(Time.utc(Time.now.utc.year, 1, 1, 17, 0, 0)))
16
-
17
- assert_equal(false, e.going_on?(Time.utc(Time.now.utc.year, 1, 2, 5, 0, 0)))
18
- end
19
-
20
- def test_start_date_end_time
21
- e = Seasonal::Event.new(nil,
22
- 'America/New_York', 'feb 22', nil, nil, '5:00pm')
23
-
24
- assert_equal(false,
25
- e.going_on?(Time.utc(Time.now.utc.year, 2, 22, 4, 59, 59)))
26
-
27
- assert(e.going_on?(Time.utc(Time.now.utc.year, 2, 22, 13, 0, 0)))
28
-
29
- assert_equal(false,
30
- e.going_on?(Time.utc(Time.now.utc.year, 2, 22, 22, 0, 1)))
12
+ assert_equal(false, e.going_on?)
31
13
  end
32
14
 
33
- def test_start_date_end_date
34
- e = Seasonal::Event.new(nil,
35
- 'America/New_York', 'mar 3 2003', nil, 'mar 5 2003')
36
-
37
- assert_equal(false, e.going_on?(Time.utc(2003, 3, 3, 4, 59, 59)))
15
+ def test_end
16
+ es = 'mar 17'
17
+ ennd = Time.parse(es).utc
38
18
 
39
- assert(e.going_on?(Time.utc(2003, 3, 4, 0, 0, 0)))
19
+ e = Seasonal::Event.new(nil, 'America/New_York', :end => es)
40
20
 
41
- assert_equal(false, e.going_on?(Time.utc(2003, 3, 6, 5, 0, 0)))
21
+ assert(e.going_on?(ennd - 1))
22
+ assert(e.going_on?(ennd))
23
+ assert_equal(false, e.going_on?(ennd + 1))
42
24
  end
43
25
 
44
- def test_start_date_end_date_end_time
45
- e = Seasonal::Event.new(nil,
46
- 'America/New_York', 'apr 20', nil, 'may 1', '00:00')
47
-
48
- assert_equal(false,
49
- e.going_on?(Time.utc(Time.now.utc.year, 4, 20, 3, 59, 59)))
26
+ def test_start
27
+ ss = 'jan 2'
28
+ start = Time.parse(ss).utc
50
29
 
51
- assert(e.going_on?(Time.utc(Time.now.utc.year, 5, 1, 0, 0, 0)))
30
+ e = Seasonal::Event.new(nil, 'America/New_York', :start => ss)
52
31
 
53
- assert_equal(false, e.going_on?(Time.utc(Time.now.utc.year, 5, 2, 0, 0, 0)))
32
+ assert_equal(false, e.going_on?(start - 1))
33
+ assert(e.going_on?(start))
34
+ assert(e.going_on?(start + 1))
54
35
  end
55
36
 
56
- def test_start_date_start_time
57
- e = Seasonal::Event.new(nil, 'America/New_York', 'may 10', '10:00am')
58
-
59
- assert_equal(false,
60
- e.going_on?(Time.utc(Time.now.utc.year, 5, 10, 13, 59, 59)))
61
-
62
- assert(e.going_on?(Time.utc(Time.now.utc.year, 5, 10, 14, 0, 0)))
63
-
64
- assert_equal(false,
65
- e.going_on?(Time.utc(Time.now.utc.year, 5, 10, 14, 0, 1)))
66
- end
67
-
68
- def test_start_date_start_time_end_time
69
- e = Seasonal::Event.new(nil,
70
- 'America/New_York', 'june 22', '10:00am', nil, '10:30am')
71
-
72
- assert_equal(false,
73
- e.going_on?(Time.utc(Time.now.utc.year, 6, 22, 13, 59, 59)))
74
-
75
- assert(e.going_on?(Time.utc(Time.now.utc.year, 6, 22, 14, 15, 0)))
76
-
77
- assert_equal(false,
78
- e.going_on?(Time.utc(Time.now.utc.year, 6, 22, 14, 30, 1)))
79
- end
80
-
81
- def test_start_date_start_time_end_date
82
- e = Seasonal::Event.new(nil,
83
- 'America/New_York', 'july 4', '11:00pm', 'jul 5')
84
-
85
- assert_equal(false,
86
- e.going_on?(Time.utc(Time.now.utc.year, 7, 5, 2, 59, 59)))
87
-
88
- assert(e.going_on?(Time.utc(Time.now.utc.year, 7, 5, 15, 0, 0)))
89
-
90
- assert_equal(false, e.going_on?(Time.utc(Time.now.utc.year, 7, 6, 4, 0, 0)))
91
- end
37
+ def test_start_end
38
+ ss = 'aug 29'
39
+ es = 'sep 1'
40
+ start = Time.parse(ss).utc
41
+ ennd = Time.parse(es).utc
92
42
 
93
- def test_start_date_start_time_end_date_end_time
94
- e = Seasonal::Event.new(nil,
95
- 'America/New_York', 'aug 19', '1:00pm', 'aug 21', '2:00pm')
43
+ e = Seasonal::Event.new(nil, 'America/New_York', :start => ss, :end => es)
96
44
 
97
- assert_equal(false,
98
- e.going_on?(Time.utc(Time.now.utc.year, 8, 19, 16, 59, 59)))
45
+ assert_equal(false, e.going_on?(start - 1))
99
46
 
100
- assert(e.going_on?(Time.utc(Time.now.utc.year, 8, 20, 15, 0, 0)))
47
+ if start <= ennd
48
+ assert(e.going_on?(start))
49
+ assert(e.going_on?(ennd))
50
+ else
51
+ assert_equal(false, e.going_on?(start))
52
+ assert_equal(false, e.going_on?(ennd))
53
+ end
101
54
 
102
- assert_equal(false,
103
- e.going_on?(Time.utc(Time.now.utc.year, 8, 21, 18, 0, 1)))
55
+ assert_equal(false, e.going_on?(ennd + 1))
104
56
  end
105
57
 
106
58
  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.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew M. Boedicker
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-11-07 00:00:00 -08:00
12
+ date: 2008-11-10 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency