cron-spec 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 2.1.0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.5.2"
12
+ gem "rcov", ">= 0"
13
+ end
@@ -0,0 +1,28 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.2)
5
+ git (1.2.5)
6
+ jeweler (1.5.2)
7
+ bundler (~> 1.0.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rake (0.8.7)
11
+ rcov (0.9.8)
12
+ rspec (2.1.0)
13
+ rspec-core (~> 2.1.0)
14
+ rspec-expectations (~> 2.1.0)
15
+ rspec-mocks (~> 2.1.0)
16
+ rspec-core (2.1.0)
17
+ rspec-expectations (2.1.0)
18
+ diff-lcs (~> 1.1.2)
19
+ rspec-mocks (2.1.0)
20
+
21
+ PLATFORMS
22
+ ruby
23
+
24
+ DEPENDENCIES
25
+ bundler (~> 1.0.0)
26
+ jeweler (~> 1.5.2)
27
+ rcov
28
+ rspec (~> 2.1.0)
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Dave Sieh
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,19 @@
1
+ = cron-spec
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to cron-spec
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 Dave Sieh. See LICENSE.txt for
18
+ further details.
19
+
@@ -0,0 +1,50 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "cron-spec"
16
+ gem.homepage = "http://github.com/j0hnds/cron-spec"
17
+ gem.license = "MIT"
18
+ gem.summary = "Cron specification implementation"
19
+ gem.description = "Cron specification implementation"
20
+ gem.email = "dave.sieh@nursinghomequality.com"
21
+ gem.authors = ["Dave Sieh"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rspec/core'
30
+ require 'rspec/core/rake_task'
31
+ RSpec::Core::RakeTask.new(:spec) do |spec|
32
+ spec.pattern = FileList['spec/**/*_spec.rb']
33
+ end
34
+
35
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
36
+ spec.pattern = 'spec/**/*_spec.rb'
37
+ spec.rcov = true
38
+ end
39
+
40
+ task :default => :spec
41
+
42
+ require 'rake/rdoctask'
43
+ Rake::RDocTask.new do |rdoc|
44
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
45
+
46
+ rdoc.rdoc_dir = 'rdoc'
47
+ rdoc.title = "cron-spec #{version}"
48
+ rdoc.rdoc_files.include('README*')
49
+ rdoc.rdoc_files.include('lib/**/*.rb')
50
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,94 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{cron-spec}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Dave Sieh"]
12
+ s.date = %q{2011-02-27}
13
+ s.description = %q{Cron specification implementation}
14
+ s.email = %q{dave.sieh@nursinghomequality.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".idea/.name",
22
+ ".idea/.rakeTasks",
23
+ ".idea/cron-spec.iml",
24
+ ".idea/encodings.xml",
25
+ ".idea/misc.xml",
26
+ ".idea/modules.xml",
27
+ ".idea/vcs.xml",
28
+ ".idea/workspace.xml",
29
+ ".rspec",
30
+ ".rvmrc",
31
+ "Gemfile",
32
+ "Gemfile.lock",
33
+ "LICENSE.txt",
34
+ "README.rdoc",
35
+ "Rakefile",
36
+ "VERSION",
37
+ "cron-spec.gemspec",
38
+ "lib/cron-spec.rb",
39
+ "lib/cron-spec/cron_specification.rb",
40
+ "lib/cron-spec/cron_specification_factory.rb",
41
+ "lib/cron-spec/cron_value_base.rb",
42
+ "lib/cron-spec/day_factory.rb",
43
+ "lib/cron-spec/dow_factory.rb",
44
+ "lib/cron-spec/hour_factory.rb",
45
+ "lib/cron-spec/minute_factory.rb",
46
+ "lib/cron-spec/month_factory.rb",
47
+ "lib/cron-spec/range_cron_value.rb",
48
+ "lib/cron-spec/single_value_cron_value.rb",
49
+ "lib/cron-spec/step_cron_value.rb",
50
+ "lib/cron-spec/wildcard_cron_value.rb",
51
+ "spec/cron_specification_spec.rb",
52
+ "spec/range_cron_value_spec.rb",
53
+ "spec/single_value_cron_value_spec.rb",
54
+ "spec/spec_helper.rb",
55
+ "spec/step_cron_value_spec.rb",
56
+ "spec/wildcard_cron_value_spec.rb"
57
+ ]
58
+ s.homepage = %q{http://github.com/j0hnds/cron-spec}
59
+ s.licenses = ["MIT"]
60
+ s.require_paths = ["lib"]
61
+ s.rubygems_version = %q{1.3.7}
62
+ s.summary = %q{Cron specification implementation}
63
+ s.test_files = [
64
+ "spec/cron_specification_spec.rb",
65
+ "spec/range_cron_value_spec.rb",
66
+ "spec/single_value_cron_value_spec.rb",
67
+ "spec/spec_helper.rb",
68
+ "spec/step_cron_value_spec.rb",
69
+ "spec/wildcard_cron_value_spec.rb"
70
+ ]
71
+
72
+ if s.respond_to? :specification_version then
73
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
74
+ s.specification_version = 3
75
+
76
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
77
+ s.add_development_dependency(%q<rspec>, ["~> 2.1.0"])
78
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
79
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
80
+ s.add_development_dependency(%q<rcov>, [">= 0"])
81
+ else
82
+ s.add_dependency(%q<rspec>, ["~> 2.1.0"])
83
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
84
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
85
+ s.add_dependency(%q<rcov>, [">= 0"])
86
+ end
87
+ else
88
+ s.add_dependency(%q<rspec>, ["~> 2.1.0"])
89
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
90
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
91
+ s.add_dependency(%q<rcov>, [">= 0"])
92
+ end
93
+ end
94
+
@@ -0,0 +1,12 @@
1
+ require 'cron-spec/cron_value_base'
2
+ require 'cron-spec/range_cron_value'
3
+ require 'cron-spec/single_value_cron_value'
4
+ require 'cron-spec/step_cron_value'
5
+ require 'cron-spec/wildcard_cron_value'
6
+ require 'cron-spec/cron_specification'
7
+ require 'cron-spec/cron_specification_factory'
8
+ require 'cron-spec/day_factory'
9
+ require 'cron-spec/dow_factory'
10
+ require 'cron-spec/hour_factory'
11
+ require 'cron-spec/minute_factory'
12
+ require 'cron-spec/month_factory'
@@ -0,0 +1,70 @@
1
+ module CronSpec
2
+ class CronSpecification
3
+
4
+ TimeMethods = [ :min, :hour, :day, :month, :wday ]
5
+
6
+ MINUTE = 0
7
+ HOUR = 1
8
+ DAY = 2
9
+ MONTH = 3
10
+ DOW = 4
11
+
12
+ # Shortcut patterns
13
+ YEARLY = 'yearly'
14
+ ANNUALLY = 'annually'
15
+ MONTHLY = 'monthly'
16
+ WEEKLY = 'weekly'
17
+ DAILY = 'daily'
18
+ MIDNIGHT = 'midnight'
19
+ HOURLY = 'hourly'
20
+
21
+ SHORTCUT_CRONS = {
22
+ YEARLY => '0 0 1 1 *',
23
+ ANNUALLY => '0 0 1 1 *',
24
+ MONTHLY => '0 0 1 * *',
25
+ WEEKLY => '0 0 * * 0',
26
+ DAILY => '0 0 * * *',
27
+ MIDNIGHT => '0 0 * * *',
28
+ HOURLY => '0 * * * *'}
29
+
30
+ SHORTCUT_PATTERN = /\A@(#{SHORTCUT_CRONS.keys.join('|')})\z/
31
+
32
+ attr_reader :raw_specification
33
+
34
+ def initialize(raw_specification)
35
+ raise "Must specify a cron specification" if raw_specification.nil?
36
+
37
+ @raw_specification = (raw_specification =~ SHORTCUT_PATTERN) ? SHORTCUT_CRONS[$1] : raw_specification
38
+
39
+ specification = @raw_specification.split(' ')
40
+ raise "Invalid cron specification" if specification.size != 5
41
+
42
+ @cron_values = []
43
+ @cron_values << parse_specification(specification[MINUTE], MinuteFactory.new)
44
+ @cron_values << parse_specification(specification[HOUR], HourFactory.new)
45
+ @cron_values << parse_specification(specification[DAY], DayFactory.new)
46
+ @cron_values << parse_specification(specification[MONTH], MonthFactory.new)
47
+ @cron_values << parse_specification(specification[DOW], DowFactory.new)
48
+ end
49
+
50
+ def is_specification_in_effect?(time=Time.now.in_time_zone(DISPLAY_TIME_ZONE))
51
+ idx = 0
52
+ test_results = @cron_values.collect do | cvalues |
53
+ time_value = time.send(TimeMethods[idx])
54
+ idx += 1
55
+ !cvalues.detect { | cv | cv.is_effective?(time_value) }.nil?
56
+ end.all?
57
+
58
+ end
59
+
60
+ private
61
+
62
+ def parse_specification(specification, factory)
63
+ specification.split(',').collect do |spec_component|
64
+ factory.parse(spec_component)
65
+ end
66
+ end
67
+
68
+ end
69
+
70
+ end
@@ -0,0 +1,39 @@
1
+ module CronSpec
2
+
3
+ class CronSpecificationFactory
4
+
5
+ WildcardPattern = /\A\*\z/
6
+ SingleValuePattern = /\A(\d+)\z/
7
+ RangePattern = /\A(\d+)-(\d+)\z/
8
+ StepPattern = /\A\*\/(\d+)\z/
9
+
10
+ def initialize
11
+ @single_value_pattern = SingleValuePattern
12
+ @range_pattern = RangePattern
13
+ @step_pattern = StepPattern
14
+ end
15
+
16
+ def parse(value_spec)
17
+ case value_spec
18
+ when WildcardPattern
19
+ WildcardCronValue.new(@lower_limit, @upper_limit)
20
+ when @single_value_pattern
21
+ SingleValueCronValue.new(@lower_limit, @upper_limit, convert_value($1))
22
+ when @range_pattern
23
+ RangeCronValue.new(@lower_limit, @upper_limit, convert_value($1), convert_value($2))
24
+ when @step_pattern
25
+ StepCronValue.new(@lower_limit, @upper_limit, $1.to_i)
26
+ else
27
+ raise "Unrecognized cron specification pattern."
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def convert_value(value)
34
+ value.to_i
35
+ end
36
+
37
+ end
38
+
39
+ end
@@ -0,0 +1,19 @@
1
+ module CronSpec
2
+
3
+ class CronValueBase
4
+
5
+ attr_reader :lower_limit, :upper_limit
6
+
7
+ def initialize(lower_limit, upper_limit)
8
+ @lower_limit = lower_limit
9
+ @upper_limit = upper_limit
10
+
11
+ raise "Lower limit must be less than or equal to upper limit" if @lower_limit > @upper_limit
12
+ end
13
+
14
+ def is_value_within_limits?(value)
15
+ value >= @lower_limit && value <= upper_limit
16
+ end
17
+ end
18
+
19
+ end
@@ -0,0 +1,12 @@
1
+ module CronSpec
2
+ class DayFactory < CronSpecificationFactory
3
+ DayLowerLimit = 1
4
+ DayUpperLimit = 31
5
+
6
+ def initialize
7
+ super
8
+ @lower_limit = DayLowerLimit
9
+ @upper_limit = DayUpperLimit
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,33 @@
1
+ module CronSpec
2
+ class DowFactory < CronSpecificationFactory
3
+
4
+ DayOfWeekLowerLimit = 0
5
+ DayOfWeekUpperLimit = 6
6
+
7
+ DaysOfWeek = %w{ sun mon tue wed thu fri sat }
8
+
9
+ DayOfWeekExpression = DaysOfWeek.join('|')
10
+
11
+ DayOfWeekSingleValuePattern = /\A(#{DayOfWeekExpression}|\d)\z/
12
+
13
+ DayOfWeekRangePattern = /\A(#{DayOfWeekExpression}|\d)-(#{DayOfWeekExpression}|\d)\z/
14
+
15
+ NamedDayOfWeekPattern = /\A(#{DayOfWeekExpression})\z/
16
+
17
+ def initialize
18
+ super
19
+ @lower_limit = DayOfWeekLowerLimit
20
+ @upper_limit = DayOfWeekUpperLimit
21
+ @single_value_pattern = DayOfWeekSingleValuePattern
22
+ @range_pattern = DayOfWeekRangePattern
23
+ end
24
+
25
+ private
26
+
27
+ def convert_value(value)
28
+ dow = (value =~ NamedDayOfWeekPattern) ? DaysOfWeek.index(value) : value.to_i
29
+ # Sunday can be specified as index 7
30
+ (dow == 7) ? 0 : dow
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,16 @@
1
+ module CronSpec
2
+
3
+ class HourFactory < CronSpecificationFactory
4
+
5
+ HourLowerLimit = 0
6
+ HourUpperLimit = 23
7
+
8
+ def initialize
9
+ super
10
+ @lower_limit = HourLowerLimit
11
+ @upper_limit = HourUpperLimit
12
+ end
13
+
14
+ end
15
+
16
+ end