Skip to content

Commit

Permalink
Rubocop fixes and test
Browse files Browse the repository at this point in the history
  • Loading branch information
mizinsky committed Dec 18, 2023
1 parent 10244b7 commit 946d0d6
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
6 changes: 5 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
AllCops:
TargetRubyVersion: 3.2
TargetRubyVersion: 2.6
NewCops: enable

Metrics/BlockLength:
Exclude:
- spec/cron_calc_spec.rb
1 change: 1 addition & 0 deletions cron_calc.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Gem::Specification.new do |spec|
spec.metadata['homepage_uri'] = spec.homepage
spec.metadata['source_code_uri'] = 'https://github.com/mizinsky/cron_calc'
spec.metadata['changelog_uri'] = 'https://github.com/mizinsky/cron_calc/blob/main/CHANGELOG.md'
spec.metadata['rubygems_mfa_required'] = 'true'

# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
Expand Down
8 changes: 8 additions & 0 deletions lib/cron_calc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
require_relative 'cron_calc/version'
require 'time'

# The CronCalc is gem-wrapper module for the Parser class
module CronCalc
class Error < StandardError; end

def self.new(cron_string, period)
Parser.new(cron_string, period)
end

# The Parser class provides functionality to parse and calculate occurrences
# of cron jobs within a given time period. It interprets cron strings and
# calculates when cron jobs will occur
class Parser
attr_reader :cron_string, :cron_parts, :period

Expand Down Expand Up @@ -54,6 +58,7 @@ def parse_cron_expression
%i[minutes hours days months].map { |unit| parse_cron_part(unit) } << (period.min.year..period.max.year).to_a
end

# rubocop:disable Metrics
def parse_cron_part(time_unit)
range = RANGE[time_unit]
part = cron_parts[time_unit]
Expand All @@ -71,9 +76,12 @@ def parse_cron_part(time_unit)
[part.to_i]
end
end
# rubocop:enable Metrics

def cron_string_valid?
# rubocop:disable Layout/LineLength
regex = %r{\A(\*|([0-5]?\d)(,([0-5]?\d))*|(\*/\d+)|(\d+-\d+)) (\*|([01]?\d|2[0-3])(,([01]?\d|2[0-3]))*|(\*/\d+)|(\d+-\d+)) (\*|([12]?\d|3[01])(,([12]?\d|3[01]))*|(\*/\d+)|(\d+-\d+)) (\*|([1-9]|1[0-2])(,([1-9]|1[0-2]))*|(\*/\d+)|(\d+-\d+)) \*\z}
# rubocop:enable Layout/LineLength
cron_string.match?(regex)
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/cron_calc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
let(:period) { Time.new(2023, 1, 1)..Time.new(2023, 1, 2) }

it do
expect { subject }.to raise_error(RuntimeError, 'Cron expression is not supported or invalid')
expect { subject }.to raise_error(RuntimeError, 'Cron expression is not supported or invalid test')
end
end

Expand Down

0 comments on commit 946d0d6

Please sign in to comment.