couch_scheduler 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,107 +1,23 @@
1
- Given /^an instance of a model that includes CouchScheduler$/ do
2
- class Model < CouchRest::Model::Base
3
- include CouchScheduler
4
- end
5
- @instance = Model.new
1
+ Given /^an.*:$/ do |code|
2
+ eval code
6
3
  end
7
4
 
8
- When /^I set "([^"]*)" to now$/ do |property|
9
- @instance.send "#{property}=", Time.now
5
+ When /^I (?:set|wait) .*:$/ do |code|
6
+ eval code
10
7
  end
11
8
 
12
- When /^I set "([^"]*)" to a day from now$/ do |property|
13
- @instance.send "#{property}=", 1.day.from_now
9
+ Then /^.*should.*:$/ do |code|
10
+ eval code
14
11
  end
15
12
 
16
- Then /^I should be able to save my document$/ do
17
- @instance.save
18
- @instance.errors.empty?.should be(true)
13
+ Given /^there are.*:$/ do |code|
14
+ eval code
19
15
  end
20
16
 
21
- When /^I set "([^"]*)" to yesterday$/ do |property|
22
- @instance.send "#{property}=", 1.day.ago
17
+ Given /^a model that includes CouchScheduler:$/ do |code|
18
+ eval code
23
19
  end
24
20
 
25
- Then /^I should not be able to save my document$/ do
26
- @instance.save
27
- @instance.errors.empty?.should be(false)
21
+ Given /^the date is .*:$/ do |code|
22
+ eval code
28
23
  end
29
-
30
- Then /^I should get an error message that the end "([^"]*)"$/ do |end_message|
31
- @instance.errors[:end].should == [end_message]
32
- end
33
-
34
- Then /^"([^"]*)" should return true on my instance$/ do |method|
35
- @instance.send(method).should be(true)
36
- end
37
-
38
- When /^I set "([^"]*)" to a week from now$/ do |property|
39
- @instance.send "#{property}=", 1.week.from_now
40
- end
41
-
42
- Then /^"([^"]*)" should return false on my instance$/ do |method|
43
- @instance.send(method).should be(false)
44
- end
45
-
46
- When /^I wait two days$/ do
47
- tomorrow = 2.days.from_now.beginning_of_day
48
- Timecop.freeze tomorrow
49
- end
50
-
51
- Then /^"([^"]*)" should return the documents currently within schedule$/ do |method|
52
- @result = Document.send(method).collect &:id
53
- @currently_scheduled.collect(&:id).sort.should == @result.sort
54
- end
55
-
56
- Then /^"([^"]*)" should not return the documents scheduled in the future$/ do |method|
57
- @result = Document.send(method)
58
- (@future_scheduled.collect(&:id) - @result.collect(&:id)).should == @future_scheduled.collect(&:id)
59
- end
60
-
61
- Given /^there are several documents currently scheduled$/ do
62
- unless defined? Document
63
- class Document < CouchRest::Model::Base
64
- include CouchScheduler
65
- end
66
- end
67
-
68
- @currently_scheduled = []
69
- 4.times { @currently_scheduled << Document.create(:start => Time.now, :end => 4.days.from_now) }
70
- end
71
-
72
- Given /^there are several documents scheduled in the future$/ do
73
- unless defined? Document
74
- class Document < CouchRest::Model::Base
75
- include CouchScheduler
76
- end
77
- end
78
-
79
- @future_scheduled = []
80
- 10.times { @future_scheduled << Document.create(:start => 1.day.from_now, :end => 4.days.from_now) }
81
- end
82
-
83
- Given /^there are (\d+) documents scheduled between now and tomorrow$/ do |num|
84
- unless defined? Document
85
- class Document < CouchRest::Model::Base
86
- include CouchScheduler
87
- end
88
- end
89
- num.to_i.times { Document.create :start => Time.now, :end => 1.day.from_now }
90
- end
91
-
92
- Given /^ther are (\d+) documents scheduled between tomorrow and two days from now$/ do |num|
93
- num.to_i.times { Document.create :start => 1.day.from_now, :end => 2.days.from_now }
94
- end
95
-
96
- Then /^"([^"]*)" should return (\d+)$/ do |code, num|
97
- eval("Document.#{code}").should == num.to_i
98
- end
99
-
100
- When /^I wait a day$/ do
101
- Timecop.freeze 1.day.from_now
102
- end
103
-
104
- When /^I wait another day$/ do
105
- Timecop.freeze 1.day.from_now
106
- end
107
-
@@ -15,4 +15,10 @@ end
15
15
  Before do
16
16
  CouchRest::Model::Config.default_database.recreate!
17
17
  Timecop.return
18
+
19
+ Object.class_eval do
20
+ if defined? Article
21
+ remove_const "Article"
22
+ end
23
+ end
18
24
  end
@@ -1,7 +1,7 @@
1
- require 'couchrest_model'
2
- require 'couch_scheduler/options'
1
+ require 'couch_view'
3
2
  require 'couch_scheduler/map'
4
3
  require 'couch_scheduler/couch_visible_integration'
5
4
  require 'couch_scheduler/couch_publish_integration'
6
5
  require 'couch_scheduler/couch_publish_and_couch_visible_integration'
7
6
  require 'couch_scheduler/couch_scheduler'
7
+ require 'couch_scheduler/conditions'
@@ -0,0 +1,23 @@
1
+ module Published
2
+ def conditions
3
+ "#{super} && doc.milestone_memories.length > 0"
4
+ end
5
+ end
6
+
7
+ module Unpublished
8
+ def conditions
9
+ "#{super} && doc.milestone_memories.length == 0"
10
+ end
11
+ end
12
+
13
+ module Shown
14
+ def conditions
15
+ "#{super} && doc.couch_visible == true"
16
+ end
17
+ end
18
+
19
+ module Hidden
20
+ def conditions
21
+ "#{super} && doc.couch_visible == false"
22
+ end
23
+ end
@@ -1,24 +1,9 @@
1
1
  module CouchScheduler
2
2
  module CouchVisibleCouchPublishIntegration
3
3
  def self.included(base)
4
- base.extend ClassMethods
5
- base.view_by :couch_schedule_and_published_and_shown, :map => Map.new(base, :published, :shown).to_s, :reduce => '_count'
6
- base.view_by :couch_schedule_and_published_and_hidden, :map => Map.new(base, :published, :hidden).to_s, :reduce => '_count'
7
- end
8
-
9
- module ClassMethods
10
- def by_schedule(options={})
11
- shown = options[:shown]
12
- hidden = options[:hidden]
13
- published = options[:published]
14
- unpublished = options[:unpublished]
15
-
16
- if shown == true && published == true
17
- by_couch_schedule_and_published_and_shown CouchScheduler::Options.new(options).to_hash
18
- elsif hidden == true && published == true
19
- by_couch_schedule_and_published_and_hidden CouchScheduler::Options.new(options).to_hash
20
- else
21
- super
4
+ base.couch_view :within_couch_schedule do
5
+ map CouchScheduler::Map do
6
+ conditions ::Published, ::Unpublished, ::Hidden, ::Shown
22
7
  end
23
8
  end
24
9
  end
@@ -1,24 +1,12 @@
1
1
  module CouchScheduler
2
2
  module CouchPublishIntegration
3
3
  def self.included(base)
4
- base.extend ClassMethods
5
- base.view_by :couch_schedule_and_unpublished, :map => Map.new(base, :unpublished).to_s, :reduce => '_count'
6
- base.view_by :couch_schedule_and_published, :map => Map.new(base, :published).to_s, :reduce => '_count'
7
- end
8
-
9
- module ClassMethods
10
- def by_schedule(options={})
11
- published = options[:published]
12
- unpublished = options[:unpublished]
13
-
14
- if published == true || unpublished == false
15
- by_couch_schedule_and_published CouchScheduler::Options.new(options).to_hash
16
- elsif unpublished == true || published == false
17
- by_couch_schedule_and_unpublished CouchScheduler::Options.new(options).to_hash
18
- else
19
- super
4
+ base.couch_view :within_couch_schedule do
5
+ map CouchScheduler::Map do
6
+ conditions ::Published, ::Unpublished
20
7
  end
21
8
  end
22
9
  end
23
10
  end
24
11
  end
12
+
@@ -1,37 +1,43 @@
1
1
  module CouchScheduler
2
2
  def self.included(base)
3
- base.property :start, Time
4
- base.property :end, Time
3
+ unless base.ancestors.include?(CouchView)
4
+ base.send :include, CouchView
5
+ end
6
+
7
+ base.property :start, Date
8
+ base.property :end, Date
5
9
  base.validate :validate_start_and_end
6
- base.view_by :couch_schedule, :map => Map.new(base).to_s, :reduce => '_count'
7
10
  base.extend ClassMethods
8
11
 
9
- if defined?(CouchPublish) && base.ancestors.include?(CouchPublish)
12
+ if defined?(CouchVisible) && base.ancestors.include?(CouchVisible) && defined?(CouchPublish) && base.ancestors.include?(CouchPublish)
13
+ base.send :include, CouchVisibleCouchPublishIntegration
14
+ elsif defined?(CouchPublish) && base.ancestors.include?(CouchPublish)
10
15
  base.send :include, CouchPublishIntegration
11
- end
12
-
13
- if defined?(CouchVisible) && base.ancestors.include?(CouchVisible)
16
+ elsif defined?(CouchVisible) && base.ancestors.include?(CouchVisible)
14
17
  base.send :include, CouchVisibleIntegration
18
+ else
19
+ base.couch_view :within_couch_schedule do
20
+ map CouchScheduler::Map
21
+ end
15
22
  end
16
23
 
17
- if defined?(CouchVisible) && base.ancestors.include?(CouchVisible) && defined?(CouchPublish) && base.ancestors.include?(CouchPublish)
18
- base.send :include, CouchVisibleCouchPublishIntegration
19
- end
20
24
  end
21
25
 
22
26
  module ClassMethods
23
- def by_schedule(options={})
24
- by_couch_schedule CouchScheduler::Options.new(options).to_hash
27
+ def map_within_schedule!
28
+ map_within_schedule.get!
29
+ end
30
+
31
+ def count_within_schedule!
32
+ count_within_schedule.get!
25
33
  end
26
34
 
27
- def count_schedule(options={})
28
- count_by_map :by_schedule, options
35
+ def map_within_schedule
36
+ map_within_couch_schedule.key(Time.now.to_date)
29
37
  end
30
38
 
31
- private
32
- def count_by_map(map, options)
33
- result = self.send(map, options.merge(:reduce => true))['rows'].first
34
- result ? result['value'] : 0
39
+ def count_within_schedule
40
+ count_within_couch_schedule.key(Time.now.to_date)
35
41
  end
36
42
  end
37
43
 
@@ -1,22 +1,9 @@
1
1
  module CouchScheduler
2
2
  module CouchVisibleIntegration
3
3
  def self.included(base)
4
- base.extend ClassMethods
5
- base.view_by :couch_schedule_and_shown, :map => Map.new(base, :shown).to_s, :reduce => '_count'
6
- base.view_by :couch_schedule_and_hidden, :map => Map.new(base, :hidden).to_s, :reduce => '_count'
7
- end
8
-
9
- module ClassMethods
10
- def by_schedule(options={})
11
- shown = options.delete :shown
12
- hidden = options.delete :hidden
13
-
14
- if shown == true || hidden == false
15
- by_couch_schedule_and_shown CouchScheduler::Options.new(options).to_hash
16
- elsif hidden == true || shown == false
17
- by_couch_schedule_and_hidden CouchScheduler::Options.new(options).to_hash
18
- else
19
- super
4
+ base.couch_view :within_couch_schedule do
5
+ map CouchScheduler::Map do
6
+ conditions ::Shown, ::Hidden
20
7
  end
21
8
  end
22
9
  end
@@ -1,27 +1,40 @@
1
1
  module CouchScheduler
2
2
  class Map
3
- def initialize(original_base, *args)
4
- @base = original_base
5
- @options = [:base_check] + args
6
- end
3
+ include CouchView::Map
7
4
 
8
- def to_s
5
+ def map
9
6
  %{
10
7
  function(doc){
11
8
  if (#{conditions}){
12
- var start = doc.start ? new Date(doc.start) : new Date(2011, 0, 1, 0, 0, 0, 0)
13
- var end = doc.end ? new Date(doc.end ) : new Date(2025, 0, 1, 0, 0, 0, 0)
9
+ if (doc.start){
10
+ startDate = doc.start.replace(/-/g, '/')
11
+ var start = new Date(startDate)
12
+ } else {
13
+ var start = new Date()
14
+ }
15
+
16
+ if (doc.end){
17
+ endDate = doc.end.replace(/-/g, '/')
18
+ var end = new Date(endDate)
19
+ } else {
20
+ var end = new Date("2025/01/01")
21
+ }
14
22
 
15
23
  while(start < end){
24
+ year = start.getFullYear()
25
+
26
+ month = start.getMonth() + 1
27
+ if (month < 10)
28
+ month = "0" + month.toString()
29
+
30
+ day = start.getDate()
31
+ if (day < 10)
32
+ day = "0" + day.toString()
33
+
16
34
  emit(
17
- [
18
- start.getFullYear(),
19
- start.getMonth(),
20
- start.getDate(),
21
- 0,
22
- 0,
23
- 0
24
- ],
35
+ year + "-" +
36
+ month + "-" +
37
+ day,
25
38
  null
26
39
  );
27
40
  start.setDate(start.getDate() + 1)
@@ -30,32 +43,5 @@ module CouchScheduler
30
43
  }
31
44
  }
32
45
  end
33
-
34
- private
35
- def conditions
36
- @options.map do |option|
37
- self.send option
38
- end.join " && "
39
- end
40
-
41
- def base_check
42
- "doc['couchrest-type'] == '#{@base.to_s}'"
43
- end
44
-
45
- def shown
46
- "doc.couch_visible === true"
47
- end
48
-
49
- def hidden
50
- "doc.couch_visible === false"
51
- end
52
-
53
- def published
54
- "doc.milestone_memories.length > 0"
55
- end
56
-
57
- def unpublished
58
- "doc.milestone_memories.length == 0"
59
- end
60
46
  end
61
47
  end
data/readme.markdown CHANGED
@@ -48,28 +48,30 @@ Let's imagine that you only to display this article on your website between the
48
48
 
49
49
  @article.within_schedule? #==> false
50
50
 
51
- ### Query (.by_schedule)
51
+ ### Query (.map_within_schedule)
52
52
 
53
- You can also query the databases for all of the articles currently within their schedule via `by_schedule`:
53
+ You can also query the databases for all of the articles currently within their schedule via `map_within_schedule`:
54
54
 
55
- Article.by_schedule
55
+ Article.map_within_schedule!
56
56
 
57
- `by_schedule` simply queries a map with a default key of `Time.now.start_of_day`. You can pass any options to it that you would normally pass to a map function in `CouchRest::Model::Base`:
57
+ `map_within_schedule` simply queries a map with a default key of `Time.now.start_of_day`. You can pass any options to it that you would normally pass to a map function in `CouchRest::Model::Base`:
58
58
 
59
- Article.by_schedule :key => 10.days.from_now
59
+ Article.map_within_schedule.key(10.days.from_now.to_date).get!
60
60
  #==> all the articles active 10 days from now
61
61
 
62
- Article.by_schedule :startkey => Time.now, :endkey => 10.days.from_now
63
- #==> all the articles active between now and 10 days from now
62
+ Article.map_within_schedule.startkey(Time.now.to_date).endkey(10.days.from_now.to_date).get!
63
+ #==> all the articles active between today and 10 days from now
64
64
 
65
- `CouchVisible` also provides you with a convenience method for getting the count of the `by_schedule` map/reduce:
65
+ If you're coming from a regular `CouchRest::Model::Base` background, you've likely not seen this lazy, chainable API before. `CouchScheduler` utilizes `CouchView` to create this functionality. Checkout the `couch_view` gem at http://github.com/moonmaster9000/couch_view
66
+
67
+ `CouchScheduler` also provides you with a convenience method for getting the count of the `map_within_schedule` map/reduce:
66
68
 
67
- Article.count_schedule
69
+ Article.count_within_schedule!
68
70
  #==> the number of documents that are currently within their schedule
69
71
 
70
- Like `by_schedule`, `count_schedule` supports all the usual map/reduce options:
72
+ Like `map_within_schedule`, `count_within_schedule` supports all the usual map/reduce options:
71
73
 
72
- Article.count_schedule :key => 10.days.from_now
74
+ Article.count_within_schedule.key(10.days.from_now).get!
73
75
  #==> the count of all articles that are within their start/end dates 10 days from now
74
76
 
75
77
 
@@ -86,37 +88,37 @@ If you include `CouchScheduler` into a model that already includes `CouchPublish
86
88
  end
87
89
 
88
90
  # you can query for all published and currently scheduled documents like this:
89
- Article.by_schedule :published => true
91
+ Article.map_within_schedule.published.get!
90
92
  #==> returns all documents that are published and currently within their schedule
91
93
 
92
94
  # you can also query for the unpublished and currently scheduled documents like this:
93
- Article.by_schedule :unpublished => true
95
+ Article.map_within_schedule.unpublished.get!
94
96
 
95
- You can also pass `:published => true` and `:unpublished => true` to the `count_schedule` method.
97
+ You can also pass use the "published" and "unpublished" query proxy methods on the `count_within_schedule` method.
96
98
 
97
99
 
98
100
  ## CouchVisible Integration
99
101
 
100
102
  If you include `CouchScheduler` into a model that includes `CouchVisible`, you'll get the following map/reduce functions for free:
101
103
 
102
- Article.by_schedule :shown => true
104
+ Article.map_within_schedule.shown.get!
103
105
  #==> all articles that are currently within their start and end dates and are shown
104
106
 
105
- Article.by_schedule :hidden => true
107
+ Article.map_within_schedule.hidden.get!
106
108
  #==> all articles that are currently within their start and end dates and are hidden
107
109
 
108
- Article.count_schedule :shown => true
109
- Article.count_schedule :hidden => true
110
+ Article.count_within_schedule.shown.get!
111
+ Article.count_within_schedule.hidden.get!
110
112
 
111
113
 
112
114
  ## CouchPublish/CouchVisible integration
113
115
 
114
116
  If you include `CouchScheduler` into a model that includes both `CouchVisible` and `CouchPublish`, you can pass `:published => true`, `:unpublished => true`, `:shown => true`, `:hidden => true` to your schedule query methods:
115
117
 
116
- Article.by_schedule :published => true, :shown => true
117
- Article.by_schedule :unpublished => true, :hidden => true
118
- Article.count_schedule :published => true, :shown => true
119
- Article.count_schedule :unpublished => true, :hidden => true
118
+ Article.map_within_schedule.published.shown.get!
119
+ Article.map_within_schedule.unpublished.hidden.get!
120
+ Article.count_within_schedule.published.shown.get!
121
+ Article.count_within_schedule.unpublished.hidden.get!
120
122
 
121
123
 
122
124
  ## LICENSE