Skip to content

Commit

Permalink
Move locale and timezone arguments to options (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
alpinweis authored May 26, 2020
1 parent 7ebe4dc commit 77dd209
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 40 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### [0.10.0] - 2020-05-26
* Move locale and timezone arguments to options

### [0.9.2] - 2020-04-24
* Add italian locale

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ Or install it yourself as:

#### Localization

Cronex::ExpressionDescriptor.new('30 2 * 2 1-5', {}, 'fr').description
Cronex::ExpressionDescriptor.new('30 2 * 2 1-5', locale: 'fr').description
=> À 2:30 AM, lundi à vendredi, seulement en février

#### Timezones

Cronex::ExpressionDescriptor.new('0-10 11 * * *', {}, 'en', 'America/Los_Angeles').description
Cronex::ExpressionDescriptor.new('0-10 11 * * *', timezone: 'America/Los_Angeles').description
=> Every minute between 4:00 AM and 4:10 AM # PDT or
=> Every minute between 3:00 AM and 3:10 AM # PST

Expand Down
10 changes: 6 additions & 4 deletions lib/cronex/exp_descriptor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@ module Cronex
verbose: false,
zero_based_dow: true,
use_24_hour_time_format: false,
throw_exception_on_parse_error: true
throw_exception_on_parse_error: true,
locale: nil,
timezone: nil
}

class ExpressionDescriptor
attr_accessor :expression, :expression_parts, :options, :parsed, :resources, :timezone

def initialize(expression, options = {}, locale = nil, timezone = 'UTC')
def initialize(expression, options = {})
@expression = expression
@options = CRONEX_OPTS.merge(options)
@expression_parts = []
@parsed = false
@resources = Cronex::Resource.new(locale)
@timezone = timezone
@resources = Cronex::Resource.new(@options[:locale])
@timezone = @options[:timezone] || 'UTC'
end

def to_hash
Expand Down
2 changes: 1 addition & 1 deletion lib/cronex/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Cronex
VERSION = '0.9.2'
VERSION = '0.10.0'
end
5 changes: 3 additions & 2 deletions spec/exp_descriptor_de_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
module Cronex
describe ExpressionDescriptor do

def desc(expression, opts = {}, timezone = 'UTC')
Cronex::ExpressionDescriptor.new(expression, opts, 'de', timezone).description
def desc(expression, opts = {})
opts[:locale] = 'de'
Cronex::ExpressionDescriptor.new(expression, opts).description
end

let(:opts) { { zero_based_dow: false } }
Expand Down
11 changes: 6 additions & 5 deletions spec/exp_descriptor_en_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
module Cronex
describe ExpressionDescriptor do

def desc(expression, opts = {}, timezone = 'UTC')
Cronex::ExpressionDescriptor.new(expression, opts, 'en', timezone).description
def desc(expression, opts = {})
opts[:locale] = 'en'
Cronex::ExpressionDescriptor.new(expression, opts).description
end

let(:opts) { { zero_based_dow: false } }
Expand Down Expand Up @@ -383,19 +384,19 @@ def desc(expression, opts = {}, timezone = 'UTC')
it 'minute span' do
tz = TZInfo::Timezone.get('America/Los_Angeles')
hour = tz.period_for_local(Time.now).zone_identifier.to_s == 'PDT' ? 4 : 3
expect(desc('0-10 11 * * *', {}, 'America/Los_Angeles')).to eq("Every minute between #{hour}:00 AM and #{hour}:10 AM")
expect(desc('0-10 11 * * *', timezone: 'America/Los_Angeles')).to eq("Every minute between #{hour}:00 AM and #{hour}:10 AM")
end

it 'twice a day' do
tz = TZInfo::Timezone.get('America/Los_Angeles')
hour = tz.period_for_local(Time.now).zone_identifier.to_s == 'PDT' ? 5 : 4
expect(desc('0 0,12 * * *', {}, 'America/Los_Angeles')).to eq("At #{hour}:00 PM and #{hour}:00 AM")
expect(desc('0 0,12 * * *', timezone: 'America/Los_Angeles')).to eq("At #{hour}:00 PM and #{hour}:00 AM")
end

it 'ahead of GMT' do
tz = TZInfo::Timezone.get('Europe/Vienna')
hour = tz.period_for_local(Time.now).zone_identifier.to_s == 'CEST' ? 1 : 12
expect(desc('0-10 11 * * *', {}, 'Europe/Vienna')).to eq("Every minute between #{hour}:00 PM and #{hour}:10 PM")
expect(desc('0-10 11 * * *', timezone: 'Europe/Vienna')).to eq("Every minute between #{hour}:00 PM and #{hour}:10 PM")
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion spec/exp_descriptor_fr_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ module Cronex
describe ExpressionDescriptor do

def desc(expression, opts = {})
Cronex::ExpressionDescriptor.new(expression, opts, 'fr').description
opts[:locale] = 'fr'
Cronex::ExpressionDescriptor.new(expression, opts).description
end

let(:opts) { { zero_based_dow: false } }
Expand Down
25 changes: 3 additions & 22 deletions spec/exp_descriptor_it_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
module Cronex
describe ExpressionDescriptor do

def desc(expression, opts = {}, timezone = 'UTC')
Cronex::ExpressionDescriptor.new(expression, opts, 'it', timezone).description
def desc(expression, opts = {})
opts[:locale] = 'it'
Cronex::ExpressionDescriptor.new(expression, opts).description
end

let(:opts) { { zero_based_dow: false } }
Expand Down Expand Up @@ -378,25 +379,5 @@ def desc(expression, opts = {}, timezone = 'UTC')
expect(desc('0 0 0 1 MAR * 2010/5')).to eq('Alle(ai) 12:00 AM, il giorno 1 del mese, solo a(nel) marzo, ogni 5 anni, a partire da 2010')
end
end

context 'timezone' do
it 'minute span' do
tz = TZInfo::Timezone.get('America/Los_Angeles')
hour = tz.period_for_local(Time.now).zone_identifier.to_s == 'PDT' ? 4 : 3
expect(desc('0-10 11 * * *', {}, 'America/Los_Angeles')).to eq("Ogni minuto tra le #{hour}:00 AM e le #{hour}:10 AM")
end

it 'twice a day' do
tz = TZInfo::Timezone.get('America/Los_Angeles')
hour = tz.period_for_local(Time.now).zone_identifier.to_s == 'PDT' ? 5 : 4
expect(desc('0 0,12 * * *', {}, 'America/Los_Angeles')).to eq("Alle(ai) #{hour}:00 PM e #{hour}:00 AM")
end

it 'ahead of GMT' do
tz = TZInfo::Timezone.get('Europe/Vienna')
hour = tz.period_for_local(Time.now).zone_identifier.to_s == 'CEST' ? 1 : 12
expect(desc('0-10 11 * * *', {}, 'Europe/Vienna')).to eq("Ogni minuto tra le #{hour}:00 PM e le #{hour}:10 PM")
end
end
end
end
3 changes: 2 additions & 1 deletion spec/exp_descriptor_pt_BR_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ module Cronex
describe ExpressionDescriptor do

def desc(expression, opts = {})
Cronex::ExpressionDescriptor.new(expression, opts, 'pt_BR').description
opts[:locale] = 'pt_BR'
Cronex::ExpressionDescriptor.new(expression, opts).description
end

let(:opts) { { zero_based_dow: false } }
Expand Down
3 changes: 2 additions & 1 deletion spec/exp_descriptor_ro_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ module Cronex
describe ExpressionDescriptor do

def desc(expression, opts = {})
Cronex::ExpressionDescriptor.new(expression, opts, 'ro').description
opts[:locale] = 'ro'
Cronex::ExpressionDescriptor.new(expression, opts).description
end

let(:opts) { { zero_based_dow: false } }
Expand Down
3 changes: 2 additions & 1 deletion spec/exp_descriptor_ru_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ module Cronex
describe ExpressionDescriptor do

def desc(expression, opts = {})
Cronex::ExpressionDescriptor.new(expression, opts, 'ru').description
opts[:locale] = 'ru'
Cronex::ExpressionDescriptor.new(expression, opts).description
end

let(:opts) { { zero_based_dow: false } }
Expand Down

0 comments on commit 77dd209

Please sign in to comment.