date_ranger 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,7 +4,11 @@ module DateRanger::Bootstrapper
4
4
  cattr_accessor :date_ranger_options
5
5
  self.date_ranger_options = options
6
6
  self.date_ranger_options[:timestep] ||= :daily
7
- self.send :include, DateRanger
7
+ if self.date_ranger_options[:class]
8
+ extend DateRanger
9
+ else
10
+ self.send :include, DateRanger
11
+ end
8
12
  end
9
13
 
10
14
  end
@@ -1,3 +1,3 @@
1
1
  module DateRanger
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/date_ranger.rb CHANGED
@@ -1,31 +1,38 @@
1
1
  module DateRanger
2
2
 
3
- def start_date
4
- @start_date ||= self.class.date_ranger_options[:default_start_date].call
5
- case self.class.date_ranger_options[:timestep]
6
- when :daily; @start_date
7
- when :monthly; @start_date.beginning_of_month
3
+ attr_writer :start_date, :end_date
4
+
5
+ def date_ranger_options
6
+ if self.class.respond_to?(:date_ranger_options)
7
+ self.class.date_ranger_options
8
+ else
9
+ super
8
10
  end
9
11
  end
10
12
 
11
- def start_date=(date)
12
- @start_date = date.respond_to?(:to_date) ? date.to_date : date
13
+ def start_date
14
+ @start_date ||= self.date_ranger_options[:default_start_date].call
15
+ @start_date = case self.date_ranger_options[:timestep]
16
+ when :daily; @start_date
17
+ when :monthly; @start_date.beginning_of_month
18
+ end
19
+ @start_date = if @start_date.respond_to?(:to_date)
20
+ @start_date.to_date
21
+ else
22
+ @start_date
23
+ end
13
24
  end
14
25
 
15
26
  def end_date
16
- @end_date ||= self.class.date_ranger_options[:default_end_date].call
17
- case self.class.date_ranger_options[:timestep]
18
- when :daily; @end_date
19
- when :monthly; @end_date.end_of_month
20
- end
21
- end
22
-
23
- def end_date=(date)
24
- @end_date = date.respond_to?(:to_date) ? date.to_date : date
27
+ @end_date ||= self.date_ranger_options[:default_end_date].call
28
+ @end_date = case self.date_ranger_options[:timestep]
29
+ when :daily; @end_date
30
+ when :monthly; @end_date.end_of_month
31
+ end
32
+ @end_date = @end_date.respond_to?(:to_date) ? @end_date.to_date : @end_date
25
33
  end
26
34
 
27
35
  end
28
36
 
29
37
  require "active_support/all"
30
- require "date_ranger/version"
31
38
  require 'date_ranger/bootstrapper'
@@ -5,11 +5,26 @@ describe DateRanger do
5
5
  class Something
6
6
  end
7
7
 
8
+ class Another
9
+ end
10
+
8
11
  describe '.has_date_range' do
9
12
  it 'should default timestep to :daily' do
10
13
  Something.has_date_range
11
14
  Something.date_ranger_options[:timestep].should == :daily
12
15
  end
16
+
17
+ context 'when applied to a class' do
18
+ it "should not overwrite another class' settings" do
19
+ Something.has_date_range :timestep => :daily, :class => true
20
+ Another.has_date_range :timestep => :monthly, :class => true
21
+ Something.date_ranger_options[:timestep].should == :daily
22
+ Another.date_ranger_options[:timestep].should == :monthly
23
+
24
+ Something.start_date = '2012-01-01'
25
+ Something.start_date.should == '2012-01-01'.to_date
26
+ end
27
+ end
13
28
  end
14
29
 
15
30
  describe "#start_date" do
@@ -20,9 +35,16 @@ describe DateRanger do
20
35
  something.start_date.should == '2012-01-01'.to_date
21
36
  end
22
37
 
38
+ it 'should return date given a time' do
39
+ Something.has_date_range
40
+ something = Something.new
41
+ something.start_date = 5.days.ago
42
+ something.start_date.should == 5.days.ago.to_date
43
+ end
44
+
23
45
  context 'when not overriden' do
24
46
  it 'should return the given default start date' do
25
- Something.has_date_range :default_start_date => lambda { 2.days.ago.to_date }
47
+ Something.has_date_range :default_start_date => lambda { 2.days.ago }
26
48
  something = Something.new
27
49
  something.start_date.should == 2.days.ago.to_date
28
50
  end
@@ -55,6 +77,13 @@ describe DateRanger do
55
77
  something.end_date.should == '2012-01-01'.to_date
56
78
  end
57
79
 
80
+ it 'should return date given a time' do
81
+ Something.has_date_range
82
+ something = Something.new
83
+ something.end_date = 5.days.ago
84
+ something.end_date.should == 5.days.ago.to_date
85
+ end
86
+
58
87
  context 'when not overriden' do
59
88
  it 'should return the given default end date' do
60
89
  Something.has_date_range :default_end_date => lambda { 2.days.ago.to_date }
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: date_ranger
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Nestor Pestelos and Ramon Tayag
@@ -18,7 +18,9 @@ cert_chain: []
18
18
  date: 2012-12-11 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
- version_requirements: &id001 !ruby/object:Gem::Requirement
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
22
24
  none: false
23
25
  requirements:
24
26
  - - ~>
@@ -29,12 +31,12 @@ dependencies:
29
31
  - 12
30
32
  - 0
31
33
  version: 2.12.0
32
- prerelease: false
33
34
  type: :development
34
- name: rspec
35
- requirement: *id001
35
+ version_requirements: *id001
36
36
  - !ruby/object:Gem::Dependency
37
- version_requirements: &id002 !ruby/object:Gem::Requirement
37
+ name: activesupport
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
38
40
  none: false
39
41
  requirements:
40
42
  - - ~>
@@ -44,10 +46,8 @@ dependencies:
44
46
  - 3
45
47
  - 0
46
48
  version: "3.0"
47
- prerelease: false
48
49
  type: :runtime
49
- name: activesupport
50
- requirement: *id002
50
+ version_requirements: *id002
51
51
  description: Provides date range methods
52
52
  email:
53
53
  - ngpestelos@gmail.com