forked from nehresma/ice_cube
-
Notifications
You must be signed in to change notification settings - Fork 0
Implement BYSETPOS #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
04290ec
Support BYSETPOS for MONTHLY AND YEARLY freq
51ea4a1
Modernize BYSETPOS commit
nehresma dede276
Remove .gitignore change unrelated to this branch
davidstosik 7d5bc51
Be more specific when including ActiveSupport core extensions
davidstosik 0d32139
Refactor BYSETPOS validation to apply to all frequencies
davidstosik 39bc6fe
Refactor BYSETPOS module to be more generic and more readable
davidstosik 638732e
Remove warning about unused variable
davidstosik 41dce1f
Refactor spec for readability
davidstosik e506037
Add extra spec (currently fails)
davidstosik 007eeea
Try to fix Travis for old Ruby versions
davidstosik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
sudo: false | ||
language: ruby | ||
before_install: | ||
- gem install bundler | ||
- if [[ $TRAVIS_RUBY_VERSION =~ ^(1|2\.[012]|jruby-1)\. ]]; then gem install rubygems-update --version '~> 2.7' --no-document && update_rubygems; elif [[ ! $TRAVIS_RUBY_VERSION =~ ^truffleruby- ]]; then gem update --system; fi | ||
- gem --version | ||
- if [[ $TRAVIS_RUBY_VERSION =~ ^(1|2\.[012]|jruby-1)\. ]]; then gem install bundler --version '~> 1.17'; else gem install bundler; fi | ||
- bundle --version | ||
notifications: | ||
email: | ||
- [email protected] | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
require "active_support/core_ext/date/calculations" | ||
require "active_support/core_ext/date_time/calculations" | ||
require "active_support/core_ext/time/calculations" | ||
|
||
require "active_support/core_ext/hash/except" | ||
|
||
module IceCube | ||
|
||
module Validations::BySetPos | ||
|
||
def by_set_pos(*bysplist) | ||
bysplist.flatten.each do |set_pos_day| | ||
unless set_pos_day.is_a?(Integer) && (-366..366).include?(set_pos_day) && set_pos_day != 0 | ||
raise ArgumentError, "expecting Integer value in [-366, -1] or [1, 366] for setposday, got #{set_pos_day} (#{bysplist})" | ||
end | ||
|
||
validations_for(:by_set_pos) << Validation.new(set_pos_day, self) | ||
end | ||
|
||
self | ||
end | ||
|
||
class Validation | ||
|
||
attr_reader :source_rule, :set_pos_day | ||
|
||
def initialize(set_pos_day, source_rule) | ||
@set_pos_day = set_pos_day | ||
@source_rule = source_rule | ||
end | ||
|
||
def type | ||
:day | ||
end | ||
|
||
def dst_adjust? | ||
true | ||
end | ||
|
||
def validate(step_time, _start_time) | ||
@step_time = step_time | ||
|
||
if step_time == occurrences_this_period[zero_indexed_position] | ||
0 | ||
else | ||
1 | ||
end | ||
end | ||
|
||
def build_s(builder) | ||
builder.piece(:by_set_pos) << set_pos_day | ||
end | ||
|
||
def build_hash(builder) | ||
builder.validations_array(:by_set_pos) << set_pos_day | ||
end | ||
|
||
def build_ical(builder) | ||
builder['BYSETPOS'] << set_pos_day | ||
end | ||
|
||
private | ||
|
||
attr_reader :step_time | ||
|
||
def zero_indexed_position | ||
if set_pos_day > 0 | ||
set_pos_day - 1 | ||
else | ||
set_pos_day | ||
end | ||
end | ||
|
||
def occurrences_this_period | ||
schedule_for_rule.occurrences_between( | ||
beginning_of_period, | ||
end_of_period | ||
) | ||
end | ||
|
||
def interval_type | ||
if source_rule.interval_type == 'daily' | ||
'day' | ||
else | ||
source_rule.interval_type.sub(/ly$/, '') | ||
end | ||
end | ||
|
||
def beginning_of_period | ||
if interval_type == 'second' | ||
fail 'boo' | ||
else | ||
step_time.public_send("beginning_of_#{interval_type}") | ||
end | ||
end | ||
|
||
def end_of_period | ||
if interval_type == 'second' | ||
fail 'boo' | ||
else | ||
step_time.public_send("end_of_#{interval_type}") | ||
end | ||
end | ||
|
||
def last_period | ||
case interval_type | ||
when 'second' | ||
fail 'boo' | ||
when 'minute' | ||
step_time - ONE_MINUTE | ||
when 'hour' | ||
step_time - ONE_HOUR | ||
when 'day' | ||
step_time.yesterday | ||
when 'week', 'month', 'year' | ||
step_time.public_send("last_#{interval_type}") | ||
end | ||
end | ||
|
||
def schedule_for_rule | ||
IceCube::Schedule.new(last_period) do |s| | ||
s.add_recurrence_rule Rule.from_hash(rule_hash_for_all_occurrences) | ||
end | ||
end | ||
|
||
def rule_hash_for_all_occurrences | ||
source_rule.to_hash.except(:count, :until).tap do |hash| | ||
if hash[:validations] | ||
hash[:validations].delete(:by_set_pos) | ||
end | ||
end | ||
end | ||
|
||
end | ||
|
||
end | ||
|
||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
require File.dirname(__FILE__) + '/../spec_helper' | ||
|
||
module IceCube | ||
|
||
describe MonthlyRule, 'BYSETPOS' do | ||
it 'should behave correctly' do | ||
schedule = IceCube::Schedule.from_ical "RRULE:FREQ=MONTHLY;COUNT=4;BYDAY=WE;BYSETPOS=4" | ||
schedule.start_time = Time.new(2015, 5, 28, 12, 0, 0) | ||
expectations = [ | ||
Time.new(2015, 6, 24, 12, 0, 0), | ||
Time.new(2015, 7, 22, 12, 0, 0), | ||
Time.new(2015, 8, 26, 12, 0, 0), | ||
Time.new(2015, 9, 23, 12, 0, 0) | ||
] | ||
expect(schedule.occurrences(Time.new(2017, 01, 01))).to eq(expectations) | ||
end | ||
|
||
end | ||
|
||
describe YearlyRule, 'BYSETPOS' do | ||
it 'should behave correctly' do | ||
schedule = IceCube::Schedule.from_ical "RRULE:FREQ=YEARLY;BYMONTH=7;BYDAY=SU,MO,TU,WE,TH,FR,SA;BYSETPOS=-1" | ||
schedule.start_time = Time.new(1966, 7, 5) | ||
expectations = [ | ||
Time.new(2015, 7, 31), | ||
Time.new(2016, 7, 31) | ||
] | ||
expect(schedule.occurrences_between(Time.new(2015, 01, 01), Time.new(2017, 01, 01))).to eq(expectations) | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note to self: this could be
hash[:validations]&.delete(:by_set_post)
. Not sure about the Ruby version requirements though... 🤔