schedule_atts 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +51 -0
- data/README.markdown +67 -0
- data/Rakefile +3 -0
- data/lib/schedule_atts.rb +108 -0
- data/lib/schedule_atts/version.rb +3 -0
- data/schedule_atts.gemspec +22 -0
- metadata +85 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
schedule_atts (0.0.1)
|
5
|
+
activesupport
|
6
|
+
ice_cube (>= 0.6.4)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: http://rubygems.org/
|
10
|
+
specs:
|
11
|
+
ZenTest (4.4.2)
|
12
|
+
activesupport (3.0.3)
|
13
|
+
archive-tar-minitar (0.5.2)
|
14
|
+
autotest (4.4.6)
|
15
|
+
ZenTest (>= 4.4.1)
|
16
|
+
columnize (0.3.2)
|
17
|
+
diff-lcs (1.1.2)
|
18
|
+
facets (2.9.0)
|
19
|
+
ice_cube (0.6.4)
|
20
|
+
linecache19 (0.5.11)
|
21
|
+
ruby_core_source (>= 0.1.4)
|
22
|
+
rspec (2.3.0)
|
23
|
+
rspec-core (~> 2.3.0)
|
24
|
+
rspec-expectations (~> 2.3.0)
|
25
|
+
rspec-mocks (~> 2.3.0)
|
26
|
+
rspec-core (2.3.1)
|
27
|
+
rspec-expectations (2.3.0)
|
28
|
+
diff-lcs (~> 1.1.2)
|
29
|
+
rspec-mocks (2.3.0)
|
30
|
+
ruby-debug-base19 (0.11.24)
|
31
|
+
columnize (>= 0.3.1)
|
32
|
+
linecache19 (>= 0.5.11)
|
33
|
+
ruby_core_source (>= 0.1.4)
|
34
|
+
ruby-debug19 (0.11.6)
|
35
|
+
columnize (>= 0.3.1)
|
36
|
+
linecache19 (>= 0.5.11)
|
37
|
+
ruby-debug-base19 (>= 0.11.19)
|
38
|
+
ruby_core_source (0.1.4)
|
39
|
+
archive-tar-minitar (>= 0.5.2)
|
40
|
+
|
41
|
+
PLATFORMS
|
42
|
+
ruby
|
43
|
+
|
44
|
+
DEPENDENCIES
|
45
|
+
activesupport
|
46
|
+
autotest
|
47
|
+
facets
|
48
|
+
ice_cube (>= 0.6.4)
|
49
|
+
rspec (>= 2.3)
|
50
|
+
ruby-debug19
|
51
|
+
schedule_atts!
|
data/README.markdown
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
# Schedule Attributes
|
2
|
+
|
3
|
+
Schedule Attributes allows models (ORM agnostic) to accept recurring schedule form parameters and translate them into an [IceCube](https://github.com/seejohnrun/ice_cube/) `Schedule` object. Schedule Attributes adds `#schedule_attributes` and `#schedule_attributes=` methods that let your model automatically populate Rails forms and receive HTML form parameters. Additionally, it provides access to the `IceCube::Schedule` object itself.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
To use, include the `ScheduleAttributes` module in your model class.
|
8
|
+
|
9
|
+
class SomeModel
|
10
|
+
include ScheduleAttributes
|
11
|
+
end
|
12
|
+
|
13
|
+
Your model must respond to `:schedule_yaml` and `:schedule_yaml=`, because ScheduleAttributes will serialize and deserialize the schedule in YAML using this column. If you are using ActiveRecord, make a string column called `schedule_yaml`. If you're using Mongoid, make a string field like so: `field :schedule_yaml`.
|
14
|
+
|
15
|
+
You model will gain the following methods:
|
16
|
+
|
17
|
+
## `schedule_attributes=`
|
18
|
+
|
19
|
+
Accepts form a parameter hash that represent a schedule, and creates an `IceCube::Schedule` object, and serializes it into your `schedule_yaml` field.
|
20
|
+
|
21
|
+
E.x.
|
22
|
+
|
23
|
+
@event.schedule_attributes = params[:event][:schedule_attributes]
|
24
|
+
|
25
|
+
### Parameters Accepted
|
26
|
+
|
27
|
+
Because they are coming from a form, all parameters are expected to be in string format.
|
28
|
+
|
29
|
+
##### `:repeat`
|
30
|
+
|
31
|
+
Can be `0` or `1`. The parameter should respond to `:to_i`. `0` indicates that the event does not repeat. Anything else indicates that the event repeats.
|
32
|
+
|
33
|
+
#### Parameters for non-repeating events
|
34
|
+
|
35
|
+
The following parameters will only be used if the `repeat` parameter is `0`.
|
36
|
+
|
37
|
+
##### `:date`
|
38
|
+
|
39
|
+
The date that this (non-repeating) event is scheduled for. Should be parseable by `Time.parse`. This parameter is only used if `:repeat` is `0`.
|
40
|
+
|
41
|
+
#### Parameters for repeating events
|
42
|
+
|
43
|
+
The following parameters will only be used if the `repeat` parameter is `1`.
|
44
|
+
|
45
|
+
##### `:start_date`
|
46
|
+
|
47
|
+
The date at which the event starts repeating. Must be parseable by `Time.parse`.
|
48
|
+
|
49
|
+
#### `:interval_unit`
|
50
|
+
|
51
|
+
The interval unit by which the event repeats. Can be `"day"` or `"week"`. For example, if the even repeats every day, this would be `"day"`. If it repeats weekly, this would be `"week"`.
|
52
|
+
|
53
|
+
#### `:interval`
|
54
|
+
|
55
|
+
The interval by which the event repeats. Should be an integer (in string format). For example, if the event repeats every other day, this parameter should be `2`, and `:interval_unit` should be `"day"`. If it repeats every other week, it should be `2` and interval unit should be `"week"`.
|
56
|
+
|
57
|
+
#### `:sunday`, `:monday`, `:tuesday`, `:wednesday`, `:thursday`, `:friday`, `:saturday`
|
58
|
+
|
59
|
+
Indicates with a `0` or `1` whether the event repeats on this day. For example, if the event repeats every other week on Tues and Thursday, then the parameters would include these: `:interval => "2", :interval_unit => "week", :tuesday => "1", :thursday => "1"`. These parameters are only used if `:repeat` is `1` and `:interval_unit` is `"week"`.
|
60
|
+
|
61
|
+
## `schedule_attributes`
|
62
|
+
|
63
|
+
Provides an OStruct with methods that correspond to the attributes accepted by `#schedule_attributes=`. ActiveRecord can use this object to populate a form using [`FormHelper#fields_for`](http://apidock.com/rails/ActionView/Helpers/FormHelper/fields_for).
|
64
|
+
|
65
|
+
##`schedule`
|
66
|
+
|
67
|
+
Returns a the `IceCube::Schedule` object that is serialized to the model's `schedule_yaml` field. If there is none, it returns a schedule with `start_date` of the current date, recurring daily, ending never.
|
data/Rakefile
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
require 'ice_cube'
|
2
|
+
require 'active_support'
|
3
|
+
require 'active_support/time_with_zone'
|
4
|
+
require 'ostruct'
|
5
|
+
|
6
|
+
module ScheduleAtts
|
7
|
+
# Your code goes here...
|
8
|
+
DAY_NAMES = Date::DAYNAMES.map(&:downcase).map(&:to_sym)
|
9
|
+
def schedule
|
10
|
+
@schedule ||= begin
|
11
|
+
if schedule_yaml.blank?
|
12
|
+
IceCube::Schedule.new(Date.today.to_time).tap{|sched| sched.add_recurrence_rule(IceCube::Rule.daily) }
|
13
|
+
else
|
14
|
+
IceCube::Schedule.from_yaml(schedule_yaml)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def schedule_attributes=(options)
|
20
|
+
options = options.dup
|
21
|
+
options[:interval] = options[:interval].to_i
|
22
|
+
options[:start_date] &&= ScheduleAttributes.parse_in_timezone(options[:start_date])
|
23
|
+
options[:date] &&= ScheduleAttributes.parse_in_timezone(options[:date])
|
24
|
+
options[:until_date] &&= ScheduleAttributes.parse_in_timezone(options[:until_date])
|
25
|
+
|
26
|
+
if options[:repeat].to_i == 0
|
27
|
+
@schedule = IceCube::Schedule.new(options[:date])
|
28
|
+
@schedule.add_recurrence_date(options[:date])
|
29
|
+
else
|
30
|
+
@schedule = IceCube::Schedule.new(options[:start_date])
|
31
|
+
|
32
|
+
rule = case options[:interval_unit]
|
33
|
+
when 'day'
|
34
|
+
IceCube::Rule.daily options[:interval]
|
35
|
+
when 'week'
|
36
|
+
IceCube::Rule.weekly(options[:interval]).day( *IceCube::DAYS.keys.select{|day| options[day].to_i == 1 } )
|
37
|
+
end
|
38
|
+
|
39
|
+
rule.until(options[:until_date]) if options[:ends] == 'eventually'
|
40
|
+
|
41
|
+
@schedule.add_recurrence_rule(rule)
|
42
|
+
end
|
43
|
+
|
44
|
+
self.schedule_yaml = @schedule.to_yaml
|
45
|
+
end
|
46
|
+
|
47
|
+
def schedule_attributes
|
48
|
+
atts = {}
|
49
|
+
|
50
|
+
if rule = schedule.rrules.first
|
51
|
+
atts[:repeat] = 1
|
52
|
+
atts[:start_date] = schedule.start_date.to_date
|
53
|
+
atts[:date] = Date.today # for populating the other part of the form
|
54
|
+
|
55
|
+
rule_hash = rule.to_hash
|
56
|
+
atts[:interval] = rule_hash[:interval]
|
57
|
+
|
58
|
+
case rule
|
59
|
+
when IceCube::DailyRule
|
60
|
+
atts[:interval_unit] = 'day'
|
61
|
+
when IceCube::WeeklyRule
|
62
|
+
atts[:interval_unit] = 'week'
|
63
|
+
rule_hash[:validations][:day].each do |day_idx|
|
64
|
+
atts[ DAY_NAMES[day_idx] ] = 1
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
if rule.until_date
|
69
|
+
atts[:until_date] = rule.until_date.to_date
|
70
|
+
atts[:ends] = 'eventually'
|
71
|
+
else
|
72
|
+
atts[:ends] = 'never'
|
73
|
+
end
|
74
|
+
else
|
75
|
+
atts[:repeat] = 0
|
76
|
+
atts[:date] = schedule.start_date.to_date
|
77
|
+
atts[:start_date] = Date.today # for populating the other part of the form
|
78
|
+
end
|
79
|
+
|
80
|
+
OpenStruct.new(atts)
|
81
|
+
end
|
82
|
+
|
83
|
+
# TODO: test this
|
84
|
+
def self.parse_in_timezone(str)
|
85
|
+
if Time.respond_to? :zone
|
86
|
+
Time.zone.parse(str)
|
87
|
+
else
|
88
|
+
Time.parse(str)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
# TODO: we shouldn't need this
|
94
|
+
ScheduleAttributes = ScheduleAtts
|
95
|
+
|
96
|
+
#TODO: this should be merged into ice_cube, or at least, make a pull request or something.
|
97
|
+
class IceCube::Rule
|
98
|
+
def ==(other)
|
99
|
+
to_hash == other.to_hash
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
class IceCube::Schedule
|
104
|
+
def ==(other)
|
105
|
+
to_hash == other.to_hash
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "schedule_atts/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "schedule_atts"
|
7
|
+
s.version = ScheduleAtts::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Gunner Technology"]
|
10
|
+
s.email = ["developers@gunnertech.com"]
|
11
|
+
s.homepage = "http://gunnertech.com"
|
12
|
+
s.summary = %q{Provides form attributes setting a recurring schedule.}
|
13
|
+
s.description = %q{Provides form attributes setting a recurring schedule.}
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.add_dependency 'ice_cube', '>= 0.6.4'
|
21
|
+
s.add_dependency 'activesupport'
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: schedule_atts
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.2
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Gunner Technology
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-04-10 00:00:00 -07:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: ice_cube
|
18
|
+
prerelease: false
|
19
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
|
+
none: false
|
21
|
+
requirements:
|
22
|
+
- - ">="
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: 0.6.4
|
25
|
+
type: :runtime
|
26
|
+
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
prerelease: false
|
30
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
31
|
+
none: false
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: "0"
|
36
|
+
type: :runtime
|
37
|
+
version_requirements: *id002
|
38
|
+
description: Provides form attributes setting a recurring schedule.
|
39
|
+
email:
|
40
|
+
- developers@gunnertech.com
|
41
|
+
executables: []
|
42
|
+
|
43
|
+
extensions: []
|
44
|
+
|
45
|
+
extra_rdoc_files: []
|
46
|
+
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- Gemfile
|
50
|
+
- Gemfile.lock
|
51
|
+
- README.markdown
|
52
|
+
- Rakefile
|
53
|
+
- lib/schedule_atts.rb
|
54
|
+
- lib/schedule_atts/version.rb
|
55
|
+
- schedule_atts.gemspec
|
56
|
+
has_rdoc: true
|
57
|
+
homepage: http://gunnertech.com
|
58
|
+
licenses: []
|
59
|
+
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: "0"
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: "0"
|
77
|
+
requirements: []
|
78
|
+
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 1.6.0
|
81
|
+
signing_key:
|
82
|
+
specification_version: 3
|
83
|
+
summary: Provides form attributes setting a recurring schedule.
|
84
|
+
test_files: []
|
85
|
+
|