timespan 0.4.4 → 0.4.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -113,13 +113,16 @@ Custom Timespan datatype:
113
113
 
114
114
  `Mongoid::Timespanned` adds the following class level macros:
115
115
 
116
- * `timespan_methods name`
117
- * `timespan_delegates name`
118
- * `timespan_setters name`
116
+ * `timespan_methods target, *names`
117
+ * `timespan_delegates target, *names`
118
+ * `timespan_delegate name, target = :period`
119
+ * `timespan_setters target, *names`
120
+ * `timespan_setter target, name`
119
121
 
120
122
  * `timespan_container_delegates container, timespan_field, *names`
121
123
  * `timespan_container_delegate container, timespan_field, name`
122
124
 
125
+ Note that all the macros, take an `options` Hash as the last argument, where you can set `override: true` in order to override any existing methods. Otherwise an `ArgumentError` exception will be raised, to warn you of a method name conflict!
123
126
 
124
127
  ```ruby
125
128
  require 'timespan/mongoid'
@@ -148,6 +151,8 @@ class TimePeriod
148
151
  end
149
152
  ```
150
153
 
154
+ Note: See `mongoid_timespan_spec.rb` for more examples of usage, and also see the `ClassMethods` module in `timespanned.rb` :)
155
+
151
156
  Usage example:
152
157
 
153
158
  ```ruby
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.4
1
+ 0.4.5
@@ -3,56 +3,110 @@ module Mongoid
3
3
  extend ActiveSupport::Concern
4
4
 
5
5
  module ClassMethods
6
- def timespan_methods name
7
- timespan_delegates name
8
- timespan_setters name
9
- end
10
-
11
6
  # fx Account.timespan_container_delegates :period, :dates, :start, :end
12
7
  # start_date= -> period.dates_start=
13
8
  # end_date= -> period.dates_end=
14
9
  def timespan_container_delegates container, timespan, *names
15
- names = [:start, :end, :duration] if names.first == :all
10
+ options = names.extract_options!
11
+ names = [:start, :end, :duration] if names.first == :all || names.empty?
16
12
  names.flatten.each do |name|
17
- timespan_container_delegate container, timespan, name
13
+ timespan_container_delegate container, timespan, name, options = {}
18
14
  end
19
15
  end
20
16
 
21
- def timespan_container_delegate container, timespan, name
17
+ def timespan_container_delegate container, timespan, name, options = {}
18
+ override = options[:override]
22
19
  case name.to_sym
23
20
  when :start
24
- define_method "start_date=" do |date|
25
- send(container).send("#{timespan}_start=", date)
21
+ meth = "start_date="
22
+ raise ArgumentError, "method #{meth} already defined on #{self}" if self.respond_to?(meth) && ! override
23
+ define_method meth do |date|
24
+ self.send(container).send("#{timespan}_start=", date)
26
25
  end
27
26
  when :end
28
- define_method "end_date=" do |date|
29
- send(container).send("#{timespan}_end=", date)
27
+ meth = "end_date="
28
+ raise ArgumentError, "method #{meth} already defined on #{self}" if self.respond_to?(meth) && !override
29
+ define_method meth do |date|
30
+ self.send(container).send("#{timespan}_end=", date)
30
31
  end
31
32
  when :duration
32
- define_method "duration=" do |date|
33
- send(container).send("#{timespan}_duration=", date)
33
+ meth = "duration="
34
+ raise ArgumentError, "method duration= already defined on #{self}" if self.respond_to?(meth) && !override
35
+ define_method meth do |date|
36
+ self.send(container).send("#{timespan}_duration=", date)
34
37
  end
35
38
  end
36
39
  end
37
40
 
38
- def timespan_delegates name = :period
39
- delegate :time_left, :duration, :start_date, :end_date, to: name
40
- end
41
+ def timespan_methods target = :period, *names
42
+ options = names.extract_options!
43
+ names = [:start, :end, :duration] if names.first == :all || names.empty?
44
+ timespan_delegates target, names, options
45
+ timespan_setters target, names, options
46
+ end
41
47
 
42
- def timespan_setters name = :period
43
- define_method :"#{name}_start=" do |date|
44
- options = self.send(name) ? {end_date: self.send(name).end_date} : {}
45
- self.send "#{name}=", ::Timespan.new(options.merge(start_date: date))
48
+ def timespan_delegates target = :period, *names
49
+ options = names.extract_options!
50
+ names = names.flatten
51
+ names = [:start_date, :end_date, :duration] if names.first == :all || names.empty?
52
+ names.map! do |name|
53
+ case name.to_sym
54
+ when :start then :start_date
55
+ when :end then :end_date
56
+ else
57
+ name.to_sym
58
+ end
59
+ end
60
+ names.flatten.each do |name|
61
+ timespan_delegate name, target, options
46
62
  end
63
+ end
64
+
65
+ def timespan_delegate meth, target = :period, options = {}
66
+ override = options[:override]
67
+ raise ArgumentError, "method #{meth} already defined on #{self}" if self.respond_to?(meth) && !override
68
+ delegate meth, to: target
69
+ end
47
70
 
48
- define_method :"#{name}_end=" do |date|
49
- options = self.send(name) ? {start_date: self.send(name).start_date} : {}
50
- self.send "#{name}=", ::Timespan.new(options.merge(end_date: date))
71
+ def timespan_setters target = :period, *names
72
+ options = names.extract_options!
73
+ names = [:start, :end, :duration] if names.first == :all || names.empty?
74
+
75
+ names.flatten.each do |name|
76
+ timespan_setter target, name, options
51
77
  end
78
+ end
79
+
80
+ def timespan_setter name, meth_name, options = {}
81
+ override = options[:override]
82
+ case meth_name.to_sym
83
+ when :start
84
+ meth = "#{name}_start="
85
+ raise ArgumentError, "method #{meth} already defined on #{self}" if self.respond_to?(meth) && !override
86
+
87
+ define_method :"#{name}_start=" do |date|
88
+ options = self.send(name) ? {end_date: self.send(name).end_date} : {}
89
+ timespan = ::Timespan.new(options.merge(start_date: date))
90
+ self.send "#{name}=", timespan
91
+ end
92
+ when :end
93
+ meth = "#{name}_end="
94
+ raise ArgumentError, "method #{meth} already defined on #{self}" if self.respond_to?(meth) && !override
52
95
 
53
- define_method :"#{name}_duration=" do |duration|
54
- options = self.send(name) ? {start_date: self.send(name).start_date} : {}
55
- self.send "#{name}=", ::Timespan.new(options.merge(duration: duration))
96
+ define_method :"#{name}_end=" do |date|
97
+ options = self.send(name) ? {start_date: self.send(name).start_date} : {}
98
+ timespan = ::Timespan.new(options.merge(end_date: date))
99
+ self.send "#{name}=", timespan
100
+ end
101
+ when :duration
102
+ meth = "#{name}_duration="
103
+ raise ArgumentError, "method #{meth} already defined on #{self}" if self.respond_to?(meth) && !override
104
+
105
+ define_method :"#{name}_duration=" do |duration|
106
+ options = self.send(name) ? {start_date: self.send(name).start_date} : {}
107
+ timespan = ::Timespan.new(options.merge(duration: duration))
108
+ self.send "#{name}=", timespan
109
+ end
56
110
  end
57
111
  end
58
112
  end
@@ -9,7 +9,7 @@ class Account
9
9
  timespan_methods :period
10
10
 
11
11
  embeds_one :time_period
12
- timespan_container_delegates :time_period, :dates, :all#:start, :end
12
+ timespan_container_delegates :time_period, :dates, :all #:start, :end
13
13
 
14
14
  def self.create_it! duration
15
15
  acc = self.new period: ::Timespan.new(duration: duration), time_period: ::TimePeriod.new
@@ -9,8 +9,6 @@ describe TimeSpan do
9
9
  @acc1 = Account.create :period => Timespan.new(:start_date => 2.days.ago)
10
10
  @acc2 = Account.create :period => Timespan.new(:start_date => 5.days.ago, :end_date => 2.days.ago)
11
11
  @acc3 = Account.create :period => Timespan.new(:start_date => 10.days.ago)
12
-
13
- # puts [@acc1._id, @acc2._id, @acc3._id]
14
12
  end
15
13
 
16
14
  describe 'find accounts within specific period' do
data/timespan.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "timespan"
8
- s.version = "0.4.4"
8
+ s.version = "0.4.5"
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"]
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.4.4
4
+ version: 0.4.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -248,7 +248,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
248
248
  version: '0'
249
249
  segments:
250
250
  - 0
251
- hash: 1165470586481426530
251
+ hash: 4117334554078744124
252
252
  required_rubygems_version: !ruby/object:Gem::Requirement
253
253
  none: false
254
254
  requirements: