simple_time_series 0.0.9 → 0.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cd605120e06fab71a1bccb9dac79ab16a6880474
4
- data.tar.gz: 5a1c66e55f196de999eb07011a60ab3a801e09bb
3
+ metadata.gz: a1de65d10a8508cd676b70cf80ed57c89ec66d2f
4
+ data.tar.gz: 662431d9c143297aa5b992cde8331a6c39892035
5
5
  SHA512:
6
- metadata.gz: 7a7bc9c117b71584071b279137b3d2db9cc80599dc3021495b9f2318d51d7f25dd48877350a6413bd5613954b999e4d014eb3bdfa82159440eb1aedafb56118d
7
- data.tar.gz: 1be9e907f24713542848055f1dd1a9cd0d2c256798ad55b3b8b64cd158edddb57a8163ea342b5a864e7cff55304212eefb942a141cdeca3861a4c42796a25935
6
+ metadata.gz: ed5334c0dc23e5ad262eac7bcedbceedaed3ecf0b7a2c89e86029fd5365e6bd9b4605053f1480e54b8748532dc039fea43e322ef324d7df22f3ee9366350e870
7
+ data.tar.gz: 8b705e90fb271cd18e7d36576ba52f04f9251602630aca9a2dc6bcf8f88b0bac12a16482341971abfceb2b6f6167c31a02f57c8ada9ac79f9aa5383d5669bfe5
@@ -1,5 +1,7 @@
1
1
  class SimpleTimeSeries
2
2
 
3
+ DEBUG = false
4
+
3
5
  attr_accessor :time_vars, :data_vars
4
6
 
5
7
  def initialize(opts)
@@ -9,12 +11,9 @@ class SimpleTimeSeries
9
11
  define_time_methods_and_set_values
10
12
  end
11
13
 
12
- def find(what, date, end_date=nil)
13
- if end_date
14
- send (what + '_on').to_sym, date, end_date
15
- else
16
- send (what + '_on').to_sym, date
17
- end
14
+ def find(what, date, end_date=nil, opts={})
15
+ puts "Calling send(#{what}_on, #{date}, #{end_date}, #{opts})" if DEBUG
16
+ send((what + '_on').to_sym, date, end_date, opts)
18
17
  end
19
18
 
20
19
  def find_plus_label(what, date, end_date=nil)
@@ -26,19 +25,20 @@ class SimpleTimeSeries
26
25
  err_msg += "Example: {:start => 'Tuesday', :end => '2014-01-06'}\n"
27
26
  err_msg += "If you want to use all observations, you can pass a blank hash, like {}\n"
28
27
  raise err_msg unless opts
29
- data_array = []
28
+ data_arr = []
30
29
  Array(data_var_names).each do |name|
31
30
  if opts[:start] && opts[:end]
32
- data_array << find(name, opts[:start], opts[:end])
31
+ puts "Calling find(#{name}, #{opts[:start]}, #{opts[:end]}, #{opts})" if DEBUG
32
+ data_arr << find(name, opts[:start], opts[:end], opts)
33
33
  else
34
- data_array << current(name)
34
+ data_arr << current(name, opts)
35
35
  end
36
36
  end
37
- data_array
37
+ data_arr
38
38
  end
39
39
 
40
- def current(what)
41
- send what.to_sym
40
+ def current(what, opts={})
41
+ send(what.to_sym, opts)
42
42
  end
43
43
 
44
44
  def index_of_date_value(date)
@@ -62,7 +62,6 @@ class SimpleTimeSeries
62
62
 
63
63
  def new_data_var(var, vals)
64
64
  define_getter_and_setter(var)
65
- var_on = "#{var}_on"
66
65
  self.class.class_eval do
67
66
  define_method("#{var}_subset") do |first, last=first|
68
67
  start_idx = index_of_date_value(first)
@@ -98,26 +97,27 @@ class SimpleTimeSeries
98
97
  return answer.length == 1 ? answer[0] : answer
99
98
  end
100
99
  end
101
- define_method(var_on) do |first, last=nil|
100
+ define_method("#{var}_on") do |first, last=nil, opts={}|
102
101
  time_vars.each do |tv_key, tv_val|
103
102
  # tv_key is something like 'dows' or 'dates'
104
103
  # tv_val is an array of associated values
105
104
  start_idx = index_of_date_value(first) || 0
106
105
  last_idx = index_of_date_value(last) || (first.nil? ? -1 : start_idx)
107
- if start_idx != last_idx #&& tv_val.include?(last)
108
- return eval(var)[start_idx..last_idx]
109
- elsif tv_val.include?(first)
106
+ puts "Called with #{first}, #{last}, #{opts}" if DEBUG
107
+ puts "start_idx = #{start_idx}; last_idx = #{last_idx}" if DEBUG
108
+ if start_idx != last_idx
109
+ arr = eval(var)[start_idx..last_idx]
110
+ return (opts[:prepend_names] ? arr.unshift(var) : arr)
111
+ elsif start_idx
110
112
  return eval(var)[start_idx]
113
+ else
114
+ raise "Can't find #{var}_on for #{first}"
111
115
  end
112
116
  end
113
- raise "Can't find #{var_on} for #{first}"
114
117
  end
115
118
  end
116
119
  instance_variable_set("@#{var}", vals) if vals
117
120
  data_vars[var] = vals unless data_vars.has_key?(var)
118
- #def eval(var).[] (first, last)
119
- # send "#{var}_subset".to_sym, first, last
120
- #end
121
121
  end
122
122
 
123
123
  private
@@ -137,7 +137,9 @@ class SimpleTimeSeries
137
137
  def define_getter_and_setter(var)
138
138
  ivar = "@#{var}"
139
139
  self.class.class_eval do
140
- define_method(var) { instance_variable_get ivar }
140
+ define_method(var) do |opts={}|
141
+ instance_variable_get ivar
142
+ end
141
143
  define_method "#{var}=" do |val|
142
144
  instance_variable_set ivar, val
143
145
  end
@@ -1,3 +1,3 @@
1
1
  class SimpleTimeSeries
2
- VERSION = "0.0.9"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -276,17 +276,49 @@ describe SimpleTimeSeries do
276
276
 
277
277
  describe "#data_array" do
278
278
 
279
- it "builds an array of arrays with all time_vars" do
279
+ it "builds an array of specified arrays" do
280
280
  @my_data.data_array('tasks_done', {}).should == [ @my_data.current('tasks_done') ]
281
- @my_data.data_array('tasks_done', 'pizzas', {}).should == [ @my_data.current('tasks_done'),
282
- @my_data.current('pizzas') ]
281
+ @my_data.data_array('tasks_done', {}).should == [ [2, 3, 0, 14, 3, 11, 0] ]
282
+ @my_data.data_array('dates', 'tasks_done', 'pizzas', {}).
283
+ should == [ @my_data.current('dates'),
284
+ @my_data.current('tasks_done'),
285
+ @my_data.current('pizzas') ]
283
286
  @my_data.data_array('tasks_done', 'pizzas', {:start => 'Tuesday', :end => 'Thursday'}).
284
- should == [ @my_data.find('tasks_done','Tuesday','Thursday'),
285
- @my_data.find('pizzas', '2014-01-03', 'Jan 5, 2014') ]
286
- #@my_data.data_array(['tasks_done', 'pizzas']).should == [ @my_data.current('tasks_done'),
287
- # @my_data.current('pizzas') ]
287
+ should == [ @my_data.find('tasks_done','Tuesday','Thursday'),
288
+ @my_data.find('pizzas', '2014-01-03', 'Jan 5, 2014') ]
288
289
  end
289
290
 
291
+ it "builds an array of specified arrays with variable names prepended to each array" do
292
+ @my_data.data_array('tasks_done', {:prepend_names => true}).
293
+ should == [ @my_data.current('tasks_done').unshift('tasks_done') ]
294
+ @my_data.data_array('tasks_done', {:prepend_names => true}).
295
+ should == [ ['tasks_done', 2, 3, 0, 14, 3, 11, 0] ]
296
+ @my_data.data_array('dates', 'tasks_done', 'pizzas', {:prepend_names => true}).
297
+ should == [ @my_data.current('dates').unshift('dates'),
298
+ @my_data.current('tasks_done').unshift('tasks_done'),
299
+ @my_data.current('pizzas').unshift('pizzas') ]
300
+ end
301
+
302
+ it "builds an array of specified arrays subsetted correctly with variable names prepended to each array" do
303
+ da = @my_data.data_array('tasks_done', 'pizzas', {:start => 'Monday', :end => 'Friday',
304
+ :prepend_names => true})
305
+ da[0].should == ['tasks_done', 3, 0, 14, 3, 11]
306
+ da[1].should == ['pizzas', 0, 1, 0, 0.5, 0]
307
+ #should == [ ['tasks_done', 3, 0, 14, 3, 11], ['pizzas', 0, 1, 0, 0.5, 0] ]
308
+ #should == [ ['tasks_done', 3, 0, 14, 3, 11], ['pizzas', 0, 1, 0, 0.5, 0] ]
309
+ #should == [ @my_data.find('tasks_done','Tuesday','Thursday'),
310
+ # @my_data.find('pizzas', '2014-01-03', 'Jan 5, 2014') ]
311
+ end
312
+
313
+ #it "builds an array of specified arrays with variable names prepended to each array" do
314
+ # @my_data.data_array('tasks_done', {}).should == [ @my_data.current('tasks_done') ]
315
+ # @my_data.data_array('tasks_done', 'pizzas', {:prepend_names => true}).should == [ @my_data.current('tasks_done'),
316
+ # @my_data.current('pizzas') ]
317
+ # @my_data.data_array('tasks_done', 'pizzas', {:start => 'Tuesday', :end => 'Thursday'}).
318
+ # should == [ @my_data.find('tasks_done','Tuesday','Thursday'),
319
+ # @my_data.find('pizzas', '2014-01-03', 'Jan 5, 2014') ]
320
+ #end
321
+
290
322
  end
291
323
 
292
324
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_time_series
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Lavin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-05 00:00:00.000000000 Z
11
+ date: 2014-06-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler