simple_time_series 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 854ef6e94d122e68388000a7c63705a989b20e3d
4
+ data.tar.gz: 827f82c991da060d9a2e3e46ac7b62f6ecd8e630
5
+ SHA512:
6
+ metadata.gz: 6ce2c910fa78063cbfcb97f2432b0c08f469741d8554d2751c05bddbd80d83a0dca6c0efad1e493aea111e778d577cef01122a642ea1eb8157642036440b161b
7
+ data.tar.gz: 5af15fd7ce32aa5edc34d868edc5e333d1a9c6af9ba1c71ad4a32c79000cac3bc3f69b8364076e291013ff1bb4e30a5274d7885dd1a0d948df65401d58663213
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 James Lavin
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,66 @@
1
+ # SimpleTimeSeries
2
+
3
+ Packages a set of time series variables into an object that allows easy data access and manipulation
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'simple_time_series'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install simple_time_series
18
+
19
+ ## Usage
20
+
21
+ Example:
22
+
23
+ Imagine you have recorded over the past week the number of pizzas consumed, the number of miles run, and the number of tasks done.
24
+
25
+ You have these stored in simple Ruby arrays (that you can name anything you want):
26
+
27
+ pizzas = [0, 0, 1, 0, 0.5, 0, 2]
28
+ miles = [2.2, 3.1, 0.0, 4.3, 1.2, 12.2, 2.3]
29
+ tasks_done = [2, 3, 0, 14, 3, 11, 0]
30
+
31
+ To associate these observations with the days they belong to, you create arrays of days-of-the-week ("dows") and/or "dates". (You can put whatever time/date values in these arrays that you wish. Currently, however, only arrays named "dows" and "dates" are supported, but I will soon make it so the names you choose don't matter. Only their order in the array(s) matters.)
32
+
33
+ dows = ['Sunday', 'Monday', 'Tuesday', 'Wednesday',
34
+ 'Thursday', 'Friday', 'Saturday']
35
+ dates = ['2014-01-01', '2014-01-02', '2014-01-03',
36
+ '2014-01-04', '2014-01-05', '2014-01-06', '2014-01-07']
37
+
38
+ This is sufficient to package up your data into a SimpleTimeSeries object:
39
+
40
+ require 'simple_time_series'
41
+ my_data = SimpleTimeSeries.new(:time_vars => {'dows' => dows,
42
+ 'dates' => dates},
43
+ :data_vars => {'pizzas' => pizzas,
44
+ 'miles' => miles,
45
+ 'tasks_done' => tasks_done})
46
+
47
+ You can now easily access the value of any data variable for any value of one of your time variables:
48
+
49
+ puts "Pizzas on Tuesday: #{my_data.pizzas_on('Tuesday')}"
50
+ puts "Pizzas on 2014-01-03: #{my_data.pizzas_on('2014-01-03')}"
51
+ puts "Miles on Friday: #{my_data.find('miles', 'Friday')}"
52
+ puts "Miles on 2014-01-05: #{my_data.find('miles','2014-01-05')}"
53
+ puts "Tasks done on Friday: #{my_data.find('tasks_done', 'Friday')}"
54
+ puts "Tasks done on 2014-01-05: #{my_data.find('tasks_done', '2014-01-05')}"
55
+
56
+ ## Disclaimer
57
+
58
+ This began as a simple code example for a collague who needed to do something similar. When he said he might actually use it, I decided to create this gem. But it's really simple, with very basic functionality.
59
+
60
+ ## Contributing
61
+
62
+ 1. Fork it ( http://github.com/JamesLavin/simple_time_series/fork )
63
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
64
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
65
+ 4. Push to the branch (`git push origin my-new-feature`)
66
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
7
+ task :test => :spec
@@ -0,0 +1,61 @@
1
+ class SimpleTimeSeries
2
+
3
+ attr_accessor :time_vars, :data_vars
4
+
5
+ def initialize(opts)
6
+ @time_vars = opts[:time_vars]
7
+ @data_vars = opts[:data_vars]
8
+ define_data_methods_and_set_values
9
+ define_time_methods_and_set_values
10
+ end
11
+
12
+ def find(what, date)
13
+ send (what + '_on').to_sym, date
14
+ end
15
+
16
+ private
17
+
18
+ def define_time_methods_and_set_values
19
+ time_vars.each do |var, vals|
20
+ define_getter_and_setter(var)
21
+ instance_variable_set("@#{var}", vals) if vals
22
+ end
23
+ end
24
+
25
+ def define_data_methods_and_set_values
26
+ data_vars.each do |var, vals|
27
+ define_getter_and_setter(var)
28
+ var_on = "#{var}_on"
29
+ self.class.class_eval do
30
+ define_method(var_on) do |date|
31
+ time_vars.each do |tv_key, tv_val|
32
+ # tv_key is 'dows' or 'dates'
33
+ # tv_val is an array of associated values
34
+ return eval(var)[tv_val.index(date)] if tv_val.include?(date)
35
+ end
36
+ raise "Can't find #{var_on} for #{date}"
37
+ end
38
+ end
39
+ instance_variable_set("@#{var}", vals) if vals
40
+ end
41
+ end
42
+
43
+ def define_getter_and_setter(var)
44
+ ivar = "@#{var}"
45
+ self.class.class_eval do
46
+ define_method(var) { instance_variable_get ivar }
47
+ define_method "#{var}=" do |val|
48
+ instance_variable_set ivar, val
49
+ end
50
+ end
51
+ end
52
+
53
+ def dows_index(date)
54
+ dows.index(date)
55
+ end
56
+
57
+ def date_index(date)
58
+ dates.index(date)
59
+ end
60
+
61
+ end
@@ -0,0 +1,3 @@
1
+ class SimpleTimeSeries
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,3 @@
1
+ require "simple_time_series/version"
2
+ require "simple_time_series/simple_time_series"
3
+
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'simple_time_series/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "simple_time_series"
8
+ spec.version = SimpleTimeSeries::VERSION
9
+ spec.authors = ["James Lavin"]
10
+ spec.email = ["simple_time_series@futureresearch.com"]
11
+ spec.summary = %q{Packages a set of time series variables into an object for easy data access and manipulation}
12
+ spec.description = %q{Packages a set of time series variables into an object for easy data access and manipulation}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ describe SimpleTimeSeries do
4
+
5
+ before do
6
+ @dows = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
7
+ @dates = ['2014-01-01', '2014-01-02', '2014-01-03', '2014-01-04', '2014-01-05', '2014-01-06', '2014-01-07']
8
+ @pizzas = [0, 0, 1, 0, 0.5, 0, 2]
9
+ @miles = [2.2, 3.1, 0.0, 4.3, 1.2, 12.2, 2.3]
10
+ @tasks_done = [2, 3, 0, 14, 3, 11, 0]
11
+ @my_data = SimpleTimeSeries.new(:data_vars =>
12
+ {'pizzas' => @pizzas, 'miles' => @miles,
13
+ 'tasks_done' => @tasks_done},
14
+ :time_vars =>
15
+ {'dows' => @dows, 'dates' => @dates})
16
+ end
17
+
18
+ it "should be creatable" do
19
+ @my_data.should be_a(SimpleTimeSeries)
20
+ end
21
+
22
+ it "should set #time_vars correctly" do
23
+ @my_data.time_vars["dates"].should == @dates
24
+ @my_data.time_vars["dows"].should == @dows
25
+ end
26
+
27
+ it "should have the correct methods" do
28
+ [:time_vars, :time_vars=, :data_vars, :data_vars=, :find, :pizzas, :pizzas=, :pizzas_on, :miles, :miles=, :miles_on, :tasks_done, :tasks_done=, :tasks_done_on, :dows, :dows=, :dates, :dates=].each do |mthd|
29
+ @my_data.methods.should include(mthd)
30
+ end
31
+ end
32
+
33
+ it "should create accessor methods for looking up data values for any time observation" do
34
+ @my_data.pizzas_on('Tuesday').should == 1
35
+ @my_data.pizzas_on('Thursday').should == 0.5
36
+ @my_data.miles_on('Sunday').should == 2.2
37
+ @my_data.miles_on('2014-01-06').should == 12.2
38
+ @my_data.tasks_done_on('2014-01-02').should == 3
39
+ end
40
+
41
+ it "should #find any data value for any time observation" do
42
+ @my_data.find('pizzas', 'Tuesday').should == 1
43
+ @my_data.find('pizzas', 'Thursday').should == 0.5
44
+ @my_data.find('miles', 'Sunday').should == 2.2
45
+ @my_data.find('miles', '2014-01-06').should == 12.2
46
+ @my_data.find('tasks_done', '2014-01-02').should == 3
47
+ end
48
+
49
+ it "should create setter methods for updating a data series" do
50
+ @my_data.pizzas = [10, 11, 12, 13, 14, 15, 16]
51
+ @my_data.pizzas_on('Tuesday').should == 12
52
+ @my_data.pizzas_on('Thursday').should == 14
53
+ @my_data.pizzas_on('2014-01-04').should == 13
54
+ @my_data.pizzas_on('2014-01-07').should == 16
55
+ end
56
+
57
+ end
@@ -0,0 +1 @@
1
+ require 'simple_time_series'
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_time_series
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - James Lavin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Packages a set of time series variables into an object for easy data
56
+ access and manipulation
57
+ email:
58
+ - simple_time_series@futureresearch.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - .gitignore
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/simple_time_series.rb
69
+ - lib/simple_time_series/simple_time_series.rb
70
+ - lib/simple_time_series/version.rb
71
+ - simple_time_series.gemspec
72
+ - spec/lib/simple_time_series_spec.rb
73
+ - spec/spec_helper.rb
74
+ homepage: ''
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.2.2
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Packages a set of time series variables into an object for easy data access
98
+ and manipulation
99
+ test_files:
100
+ - spec/lib/simple_time_series_spec.rb
101
+ - spec/spec_helper.rb