timespan 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.0
1
+ 0.5.1
@@ -2,6 +2,18 @@ module Mongoid
2
2
  module Timespanned
3
3
  extend ActiveSupport::Concern
4
4
 
5
+ class << self
6
+ attr_accessor :log
7
+
8
+ def log msg
9
+ puts msg # if log?
10
+ end
11
+
12
+ def log?
13
+ @log
14
+ end
15
+ end
16
+
5
17
  module ClassMethods
6
18
  attr_writer :max_asap, :min_asap
7
19
 
@@ -50,22 +62,29 @@ module Mongoid
50
62
 
51
63
  def timespan_container_delegate container, timespan, name, options = {}
52
64
  override = options[:override]
65
+ mt = Mongoid::Timespanned
53
66
  case name.to_sym
54
67
  when :start
55
68
  meth = "start_date="
56
69
  raise ArgumentError, "method #{meth} already defined on #{self}" if self.respond_to?(meth) && ! override
70
+
71
+ mt.log "#{self} define container delegate: #{meth} to #{container}.#{timespan}_start="
57
72
  define_method meth do |date|
58
73
  self.send(container).send("#{timespan}_start=", date)
59
74
  end
60
75
  when :end
61
76
  meth = "end_date="
62
77
  raise ArgumentError, "method #{meth} already defined on #{self}" if self.respond_to?(meth) && !override
78
+
79
+ mt.log "#{self} define container delegate: #{meth} to #{container}.#{timespan}_end="
63
80
  define_method meth do |date|
64
81
  self.send(container).send("#{timespan}_end=", date)
65
82
  end
66
83
  when :duration
67
84
  meth = "duration="
68
85
  raise ArgumentError, "method duration= already defined on #{self}" if self.respond_to?(meth) && !override
86
+
87
+ mt.log "#{self} define container delegate: #{meth} to #{container}.#{timespan}_duration="
69
88
  define_method meth do |date|
70
89
  self.send(container).send("#{timespan}_duration=", date)
71
90
  end
@@ -98,7 +117,10 @@ module Mongoid
98
117
 
99
118
  def timespan_delegate meth, target = :period, options = {}
100
119
  override = options[:override]
120
+ mt = Mongoid::Timespanned
101
121
  raise ArgumentError, "method #{meth} already defined on #{self}" if self.respond_to?(meth) && !override
122
+
123
+ mt.log "#{self} define delegate: #{meth} to #{target}"
102
124
  delegate meth, to: target
103
125
  end
104
126
 
@@ -113,12 +135,14 @@ module Mongoid
113
135
 
114
136
  def timespan_setter name, meth_name, options = {}
115
137
  override = options[:override]
138
+ mt = Mongoid::Timespanned
116
139
  case meth_name.to_sym
117
140
  when :start
118
141
  meth = "#{name}_start="
119
142
  raise ArgumentError, "method #{meth} already defined on #{self}" if self.respond_to?(meth) && !override
120
143
 
121
- define_method :"#{name}_start=" do |date|
144
+ mt.log "#{self} define setter: #{meth}"
145
+ define_method meth do |date|
122
146
  options = self.send(name) ? {end_date: self.send(name).end_date} : {}
123
147
  timespan = ::Timespan.new(options.merge(start_date: date))
124
148
  self.send "#{name}=", timespan
@@ -127,7 +151,8 @@ module Mongoid
127
151
  meth = "#{name}_end="
128
152
  raise ArgumentError, "method #{meth} already defined on #{self}" if self.respond_to?(meth) && !override
129
153
 
130
- define_method :"#{name}_end=" do |date|
154
+ mt.log "#{self} define setter: #{meth}"
155
+ define_method meth do |date|
131
156
  options = self.send(name) ? {start_date: self.send(name).start_date} : {}
132
157
  timespan = ::Timespan.new(options.merge(end_date: date))
133
158
  self.send "#{name}=", timespan
@@ -136,7 +161,9 @@ module Mongoid
136
161
  meth = "#{name}_duration="
137
162
  raise ArgumentError, "method #{meth} already defined on #{self}" if self.respond_to?(meth) && !override
138
163
 
139
- define_method :"#{name}_duration=" do |duration|
164
+ mt.log "#{self} define setter: #{meth}"
165
+
166
+ define_method meth do |duration|
140
167
  options = self.send(name) ? {start_date: self.send(name).start_date} : {}
141
168
  timespan = ::Timespan.new(options.merge(duration: duration))
142
169
  self.send "#{name}=", timespan
@@ -1,4 +1,5 @@
1
1
  require 'timespan/mongoid/spec_helper'
2
+ load_models!
2
3
 
3
4
  describe TimeSpan do
4
5
  subject { account }
@@ -12,7 +12,6 @@ class Account
12
12
  embeds_one :time_period
13
13
 
14
14
  timespan_container_delegates :time_period, :dates, :all
15
- timespan_container_delegates :time_period, :flex, :all
16
15
 
17
16
  def self.create_it! duration
18
17
  acc = self.new period: ::Timespan.new(duration: duration), time_period: ::TimePeriod.new
@@ -1,4 +1,5 @@
1
1
  require 'timespan/mongoid/spec_helper'
2
+ load_models!
2
3
 
3
4
  describe TimeSpan do
4
5
  subject { account }
@@ -1,5 +1,8 @@
1
1
  require 'timespan/mongoid/spec_helper'
2
2
 
3
+ Mongoid::Timespanned.log = true
4
+ load_models!
5
+
3
6
  describe TimeSpan do
4
7
  subject { account }
5
8
 
@@ -13,7 +16,15 @@ describe TimeSpan do
13
16
  context 'factory method #from' do
14
17
  describe ':today' do
15
18
  let(:account) do
16
- Account.create period: Timespan.from(:today, 5.days)
19
+ Account.create period: ::Timespan.from(:today, 5.days)
20
+ end
21
+
22
+ describe '.to_s' do
23
+ it 'should return a non-empty String starting with from' do
24
+ subject.period.to_s.should be_a String
25
+ subject.period.to_s.should_not be_empty
26
+ subject.period.to_s.should match(/^from/)
27
+ end
17
28
  end
18
29
 
19
30
  describe '.start_date' do
@@ -154,6 +165,7 @@ describe TimeSpan do
154
165
  subject.period_start = tomorrow
155
166
  subject.period_end = tomorrow + 5.days
156
167
 
168
+ subject.time_period.dates_end = tomorrow + 3.days
157
169
  subject.end_date = tomorrow + 3.days
158
170
  end
159
171
 
@@ -28,4 +28,6 @@ end
28
28
 
29
29
  require 'timespan/mongoid'
30
30
 
31
- require "timespan/mongoid/models/account_#{Mongoid::MAJOR_VERSION}x"
31
+ def load_models!
32
+ require "timespan/mongoid/models/account_#{Mongoid::MAJOR_VERSION}x"
33
+ end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "timespan"
8
- s.version = "0.5.0"
8
+ s.version = "0.5.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kristian Mandrup"]
12
- s.date = "2012-09-26"
12
+ s.date = "2012-10-18"
13
13
  s.description = "Makes it easy to calculate time distance in different units"
14
14
  s.email = "kmandrup@gmail.com"
15
15
  s.extra_rdoc_files = [
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: timespan
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-26 00:00:00.000000000 Z
12
+ date: 2012-10-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: chronic
@@ -302,7 +302,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
302
302
  version: '0'
303
303
  segments:
304
304
  - 0
305
- hash: 3316997137060640016
305
+ hash: -4354095327562693822
306
306
  required_rubygems_version: !ruby/object:Gem::Requirement
307
307
  none: false
308
308
  requirements: