Class: Duration
- Inherits:
-
Object
show all
- Defined in:
- lib/quickbooks/extlib/days_and_times/duration.rb
Instance Attribute Summary collapse
-
#length ⇒ Object
Length is the length of the time span, in seconds Unit is a length of time (in seconds) to use in collection methods StartTime is an optional attribute that can ‘anchor’ a duration to a specific real time.
-
#start_time ⇒ Object
Length is the length of the time span, in seconds Unit is a length of time (in seconds) to use in collection methods StartTime is an optional attribute that can ‘anchor’ a duration to a specific real time.
-
#unit ⇒ Object
Length is the length of the time span, in seconds Unit is a length of time (in seconds) to use in collection methods StartTime is an optional attribute that can ‘anchor’ a duration to a specific real time.
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(count = 0, unit = 1, start_time = nil, auto_klass = {}) ⇒ Duration
Returns a new instance of Duration.
8
9
10
11
12
13
14
15
16
17
18
|
# File 'lib/quickbooks/extlib/days_and_times/duration.rb', line 8
def initialize(count=0,unit=1,start_time=nil,auto_klass={})
if unit.is_a?(Time) || unit.is_a?(DateTime)
start_time = unit
unit = 1
end
options = {:count => count || 0, :unit => unit || 1, :start_time => start_time}.merge(count.is_a?(Hash) ? count : {})
@unit = options[:unit]
@length = (@unit * options[:count].to_f).round
@start_time = options[:start_time]
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
261
262
263
264
265
|
# File 'lib/quickbooks/extlib/days_and_times/duration.rb', line 261
def method_missing(method_name, *args)
return self.start_time.send(method_name, *args) if self.anchored? && self.start_time.respond_to?(method_name)
super
end
|
Instance Attribute Details
Length is the length of the time span, in seconds Unit is a length of time (in seconds) to use in collection methods StartTime is an optional attribute that can ‘anchor’ a duration
to a specific real time.
7
8
9
|
# File 'lib/quickbooks/extlib/days_and_times/duration.rb', line 7
def length
@length
end
|
#start_time ⇒ Object
Length is the length of the time span, in seconds Unit is a length of time (in seconds) to use in collection methods StartTime is an optional attribute that can ‘anchor’ a duration
to a specific real time.
7
8
9
|
# File 'lib/quickbooks/extlib/days_and_times/duration.rb', line 7
def start_time
@start_time
end
|
Length is the length of the time span, in seconds Unit is a length of time (in seconds) to use in collection methods StartTime is an optional attribute that can ‘anchor’ a duration
to a specific real time.
7
8
9
|
# File 'lib/quickbooks/extlib/days_and_times/duration.rb', line 7
def unit
@unit
end
|
Class Method Details
.create_find_within_method_for(other, method_name, other_method_name) ⇒ Object
256
257
258
|
# File 'lib/quickbooks/extlib/days_and_times/duration.rb', line 256
def self.create_find_within_method_for(other, method_name, other_method_name)
self.bind_class_object_method(other, method_name, other_method_name, [[], ['self.start_time', 'self.end_time']])
end
|
27
28
29
|
# File 'lib/quickbooks/extlib/days_and_times/duration.rb', line 27
def self.length
1
end
|
.new(*args) ⇒ Object
19
20
21
22
23
24
25
26
|
# File 'lib/quickbooks/extlib/days_and_times/duration.rb', line 19
def self.new(*args)
a = super
if self.name == 'Duration' && (args.last.is_a?(Hash) ? args.last[:auto_class] == true : true)
a.send(:auto_class)
else
a
end
end
|
Instance Method Details
103
104
105
106
107
108
109
110
111
|
# File 'lib/quickbooks/extlib/days_and_times/duration.rb', line 103
def *(value)
if value.is_a?(Duration)
@length * value.length * value.unit
elsif value.respond_to?(:to_i)
auto_class(Duration.new(@length * value))
else
raise TypeError, "Can't convert #{value.class.name} to an integer."
end
end
|
96
97
98
99
100
101
102
|
# File 'lib/quickbooks/extlib/days_and_times/duration.rb', line 96
def +(value)
if value.respond_to?(:to_i)
auto_class(Duration.new(@length + value.to_i))
else
raise TypeError, "Can't convert #{value.class.name} to an integer."
end
end
|
89
90
91
92
93
94
95
|
# File 'lib/quickbooks/extlib/days_and_times/duration.rb', line 89
def -(value)
if value.respond_to?(:to_i)
auto_class(Duration.new(@length - value.to_i))
else
raise TypeError, "Can't convert #{value.class.name} to an integer."
end
end
|
112
113
114
115
116
117
118
119
120
|
# File 'lib/quickbooks/extlib/days_and_times/duration.rb', line 112
def /(value)
if value.is_a?(Duration)
@length / (value.length * value.unit)
elsif value.respond_to?(:to_i)
auto_class(Duration.new(@length / value))
else
raise TypeError, "Can't convert #{value.class.name} to an integer."
end
end
|
#===(other) ⇒ Object
70
71
72
|
# File 'lib/quickbooks/extlib/days_and_times/duration.rb', line 70
def ===(other)
self.to_f == other.to_f
end
|
#abs_length ⇒ Object
54
55
56
|
# File 'lib/quickbooks/extlib/days_and_times/duration.rb', line 54
def abs_length
@length
end
|
#abs_length=(value) ⇒ Object
47
48
49
50
51
52
53
|
# File 'lib/quickbooks/extlib/days_and_times/duration.rb', line 47
def abs_length=(value)
if value.respond_to?(:to_i)
@length = value.to_i
else
raise TypeError, "Can't set a Duration's length to a #{value.class.name} object."
end
end
|
189
190
191
|
# File 'lib/quickbooks/extlib/days_and_times/duration.rb', line 189
def ago
self.before(Time.now)
end
|
173
174
175
|
# File 'lib/quickbooks/extlib/days_and_times/duration.rb', line 173
def anchored?
!self.start_time.nil?
end
|
#before(time) ⇒ Object
183
184
185
|
# File 'lib/quickbooks/extlib/days_and_times/duration.rb', line 183
def before(time)
time - @length
end
|
#coerce(*args) ⇒ Object
66
67
68
|
# File 'lib/quickbooks/extlib/days_and_times/duration.rb', line 66
def coerce(*args)
to_f.coerce(*args)
end
|
#collect(use_unit = self.class.length, &block) ⇒ Object
219
220
221
222
223
224
225
|
# File 'lib/quickbooks/extlib/days_and_times/duration.rb', line 219
def collect(use_unit=self.class.length,&block)
ary = []
self.each(use_unit) do |x|
ary << (block_given? ? yield(x) : x)
end
ary
end
|
#create_find_within_method_for(other, method_name, other_method_name) ⇒ Object
Through some ingenious metacoding (see ‘def bind_object_method’) below, it is possible to create a new method on a Duration object to a method on another object, in order to gather information based on the duration mentioned.
253
254
255
|
# File 'lib/quickbooks/extlib/days_and_times/duration.rb', line 253
def create_find_within_method_for(other, method_name, other_method_name)
self.bind_object_method(other, method_name, other_method_name, [[], ['self.start_time', 'self.end_time']])
end
|
139
140
141
|
# File 'lib/quickbooks/extlib/days_and_times/duration.rb', line 139
def days
@length.to_f / Day.length
end
|
#each(use_unit = self.class.length) ⇒ Object
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
# File 'lib/quickbooks/extlib/days_and_times/duration.rb', line 226
def each(use_unit=self.class.length)
remainder = @length.to_f % use_unit
ret = []
if self.start_time.nil?
(@length.to_f / use_unit).to_i.times do |i|
ret << Duration.new(1, use_unit)
yield(ret[-1])
end
else
(@length.to_f / use_unit).to_i.times do |i|
ret << Duration.new(1, use_unit, (self.start_time + (use_unit * i)))
yield(ret[-1])
end
end
if remainder > 0
ret << (self.start_time.nil? ? Duration.new(remainder, 1) : Duration.new(remainder, 1, (self.start_time + @length - remainder)))
yield(ret[-1])
end
ret
end
|
#each_day(&block) ⇒ Object
207
208
209
|
# File 'lib/quickbooks/extlib/days_and_times/duration.rb', line 207
def each_day(&block)
self.each(Day.length,&block)
end
|
#each_hour(&block) ⇒ Object
210
211
212
|
# File 'lib/quickbooks/extlib/days_and_times/duration.rb', line 210
def each_hour(&block)
self.each(Hour.length,&block)
end
|
#each_minute(&block) ⇒ Object
213
214
215
|
# File 'lib/quickbooks/extlib/days_and_times/duration.rb', line 213
def each_minute(&block)
self.each(Minute.length,&block)
end
|
#each_second(&block) ⇒ Object
216
217
218
|
# File 'lib/quickbooks/extlib/days_and_times/duration.rb', line 216
def each_second(&block)
self.each(Second.length,&block)
end
|
#each_week(&block) ⇒ Object
A Duration can be treated as a ‘collection’ of units
204
205
206
|
# File 'lib/quickbooks/extlib/days_and_times/duration.rb', line 204
def each_week(&block)
self.each(Week.length,&block)
end
|
170
171
172
|
# File 'lib/quickbooks/extlib/days_and_times/duration.rb', line 170
def end_time
@start_time + self
end
|
#end_time=(value) ⇒ Object
163
164
165
166
167
168
169
|
# File 'lib/quickbooks/extlib/days_and_times/duration.rb', line 163
def end_time=(value)
if value.is_a?(Time) || value.is_a?(DateTime)
@start_time = value.to_time - self else
raise TypeError, "A Duration's end_time must be a Time or DateTime object."
end
end
|
#ending(time) ⇒ Object
196
197
198
199
|
# File 'lib/quickbooks/extlib/days_and_times/duration.rb', line 196
def ending(time)
self.end_time = time
self
end
|
#from(time) ⇒ Object
Calculations using Duration as an intermediate
180
181
182
|
# File 'lib/quickbooks/extlib/days_and_times/duration.rb', line 180
def from(time)
time + @length
end
|
186
187
188
|
# File 'lib/quickbooks/extlib/days_and_times/duration.rb', line 186
def from_now
self.from(Time.now)
end
|
142
143
144
|
# File 'lib/quickbooks/extlib/days_and_times/duration.rb', line 142
def hours
@length.to_f / Hour.length
end
|
124
125
126
|
# File 'lib/quickbooks/extlib/days_and_times/duration.rb', line 124
def in_days
Duration.new(@length.to_f / Day.length, Day.length)
end
|
73
74
75
|
# File 'lib/quickbooks/extlib/days_and_times/duration.rb', line 73
def inspect
"#<#{self.class.name}:#{self.object_id} (length=#{self.length.inspect}) #{self.instance_variables.reject {|d| d=='@length' || self.instance_variable_get(d).nil?}.collect {|iv| "#{iv}=#{self.instance_variable_get(iv).inspect}"}.join(' ')}>"
end
|
145
146
147
|
# File 'lib/quickbooks/extlib/days_and_times/duration.rb', line 145
def minutes
@length.to_f / Minute.length
end
|
148
149
150
|
# File 'lib/quickbooks/extlib/days_and_times/duration.rb', line 148
def seconds
@length.to_f / Second.length
end
|
#starting(time) ⇒ Object
192
193
194
195
|
# File 'lib/quickbooks/extlib/days_and_times/duration.rb', line 192
def starting(time)
self.start_time = time
self
end
|
60
61
62
|
# File 'lib/quickbooks/extlib/days_and_times/duration.rb', line 60
def to_f
self.abs_length.to_f
end
|
57
58
59
|
# File 'lib/quickbooks/extlib/days_and_times/duration.rb', line 57
def to_i
self.abs_length.to_i
end
|
63
64
65
|
# File 'lib/quickbooks/extlib/days_and_times/duration.rb', line 63
def to_s
"#{self.length} #{self.class.name}"
end
|
136
137
138
|
# File 'lib/quickbooks/extlib/days_and_times/duration.rb', line 136
def weeks
@length.to_f / Week.length
end
|