time_elapser 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "http://rubygems.org"
2
+ gem 'rspec'
3
+
4
+ group :development do
5
+ gem "bundler", "~> 1.0.0"
6
+ gem "jeweler", "~> 1.5.1"
7
+ end
@@ -0,0 +1,26 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.2)
5
+ git (1.2.5)
6
+ jeweler (1.5.1)
7
+ bundler (~> 1.0.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rake (0.8.7)
11
+ rspec (2.2.0)
12
+ rspec-core (~> 2.2)
13
+ rspec-expectations (~> 2.2)
14
+ rspec-mocks (~> 2.2)
15
+ rspec-core (2.2.1)
16
+ rspec-expectations (2.2.0)
17
+ diff-lcs (~> 1.1.2)
18
+ rspec-mocks (2.2.0)
19
+
20
+ PLATFORMS
21
+ ruby
22
+
23
+ DEPENDENCIES
24
+ bundler (~> 1.0.0)
25
+ jeweler (~> 1.5.1)
26
+ rspec
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Edwin Cruz
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,38 @@
1
+ = time_elapser
2
+
3
+ Time-elapser Create timestamps query caching friendly
4
+
5
+ == Usage
6
+ TimeElapser.sanitize(interval)
7
+
8
+ *interval means how often it is going to change
9
+
10
+ == Example - Rails
11
+
12
+ Product.find(:all, :conditions => ['available_date <= ?', TimeElapser.sanitize(5)])
13
+
14
+ This will create queries during between 2000-01-01 00:00:00 and 2000-01-01 00:00:04 like this:
15
+
16
+ select * from products where available_date = '2000-01-01 00:00:00'
17
+
18
+ Allowing query cache to actually cache the results longer.
19
+
20
+ == TODO
21
+
22
+ * Support TimeZone for non rails applications
23
+
24
+ == Contributing to time_elapser
25
+
26
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
27
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
28
+ * Fork the project
29
+ * Start a feature/bugfix branch
30
+ * Commit and push until you are happy with your contribution
31
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
32
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
33
+
34
+ == Copyright
35
+
36
+ Copyright (c) 2010 CrowdInt. See LICENSE.txt for
37
+ further details.
38
+
@@ -0,0 +1,49 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "time_elapser"
16
+ gem.homepage = "http://github.com/crowdint/time_elapser"
17
+ gem.license = "MIT"
18
+ gem.summary = "Time-elapser Query cache helper"
19
+ gem.description = "Helper to create queries using time intervals helping databases to cache most used queries easier"
20
+ gem.email = "edwin.cruz@crowdint.com"
21
+ gem.authors = ["Edwin Cruz"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rspec/core'
30
+ require 'rspec/core/rake_task'
31
+ RSpec::Core::RakeTask.new(:spec) do |spec|
32
+ spec.pattern = FileList['spec/**/*_spec.rb']
33
+ end
34
+
35
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
36
+ spec.pattern = 'spec/**/*_spec.rb'
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rake/rdoctask'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "time_elapser #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,25 @@
1
+ ##
2
+ # TimeElapser
3
+ # Returns time in windows, making easier to query caching to save results
4
+ ##
5
+ class TimeElapser
6
+
7
+ # Saninitize current time and returns an starting point of time every interval (in seconds)
8
+ #
9
+ # *Example*
10
+ #
11
+ # If you want to cache results for 5 seconds, do this:
12
+ #
13
+ # @products = Product.find(:all, :conditions => ['available_date <= ?', TimeElapser.sanitize(5)])
14
+ #
15
+ # If RAILS_ROOT is defined, it will use Time.zone.now, otherwise it will use Time.now
16
+ #
17
+ def self.sanitize interval = 1
18
+ now = (defined?(RAILS_ROOT)) ? Time.zone.now : Time.now
19
+ if interval > 1
20
+ Time.at((now.to_i/interval)*interval)
21
+ else
22
+ now
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,13 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'time_elapser'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
13
+
@@ -0,0 +1,54 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe TimeElapser do
4
+
5
+ subject { TimeElapser }
6
+
7
+ context 'time elapser initialized without parameters' do
8
+ it 'should return the current date if no params are provided' do
9
+ Time.stub(:now).and_return(Time.at(946757701))
10
+ subject.sanitize.to_i.should eq(946757701)
11
+ end
12
+ end
13
+
14
+ context 'time elapser ready for query caching' do
15
+ it 'should return a second cero for the next 5 seconds' do
16
+ [946757700, 946757701, 946757702, 946757703, 946757704].each do |local_time|
17
+ Time.stub(:now).and_return(Time.at(local_time))
18
+ subject.sanitize(5).to_i.should eq(946757700)
19
+ end
20
+ end
21
+
22
+ it 'should jump to the next window when it has to' do
23
+ Time.stub(:now).and_return(Time.at(946757705))
24
+ subject.sanitize(5).to_i.should_not eq(946757700)
25
+ end
26
+ end
27
+
28
+ context 'time elapser initialized without parameters and rails is used' do
29
+ before(:each) do
30
+ RAILS_ROOT = true
31
+ @time_zone = mock('TimeZone')
32
+ end
33
+ it 'should return the current date if no params are provided' do
34
+ @time_zone.stub(:now).and_return(Time.at(946757701))
35
+ Time.stub(:zone).and_return(@time_zone)
36
+ subject.sanitize.to_i.should eq(946757701)
37
+ end
38
+
39
+ it 'should return a second cero for the next 5 seconds' do
40
+ [946757700, 946757701, 946757702, 946757703, 946757704].each do |local_time|
41
+ @time_zone.stub(:now).and_return(Time.at(local_time))
42
+ Time.stub(:zone).and_return(@time_zone)
43
+ subject.sanitize(5).to_i.should eq(946757700)
44
+ end
45
+ end
46
+
47
+ it 'should jump to the next window when it has to' do
48
+ @time_zone.stub(:now).and_return(Time.at(946757706))
49
+ Time.stub(:zone).and_return(@time_zone)
50
+ subject.sanitize(5).to_i.should_not eq(946757700)
51
+ end
52
+ end
53
+
54
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: time_elapser
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Edwin Cruz
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-14 00:00:00 -08:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ type: :runtime
23
+ version_requirements: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ requirement: *id001
33
+ prerelease: false
34
+ name: rspec
35
+ - !ruby/object:Gem::Dependency
36
+ type: :development
37
+ version_requirements: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ~>
41
+ - !ruby/object:Gem::Version
42
+ hash: 23
43
+ segments:
44
+ - 1
45
+ - 0
46
+ - 0
47
+ version: 1.0.0
48
+ requirement: *id002
49
+ prerelease: false
50
+ name: bundler
51
+ - !ruby/object:Gem::Dependency
52
+ type: :development
53
+ version_requirements: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ~>
57
+ - !ruby/object:Gem::Version
58
+ hash: 1
59
+ segments:
60
+ - 1
61
+ - 5
62
+ - 1
63
+ version: 1.5.1
64
+ requirement: *id003
65
+ prerelease: false
66
+ name: jeweler
67
+ description: Helper to create queries using time intervals helping databases to cache most used queries easier
68
+ email: edwin.cruz@crowdint.com
69
+ executables: []
70
+
71
+ extensions: []
72
+
73
+ extra_rdoc_files:
74
+ - LICENSE.txt
75
+ - README.rdoc
76
+ files:
77
+ - .document
78
+ - Gemfile
79
+ - Gemfile.lock
80
+ - LICENSE.txt
81
+ - README.rdoc
82
+ - Rakefile
83
+ - VERSION
84
+ - lib/time_elapser.rb
85
+ - spec/spec_helper.rb
86
+ - spec/time_elapser_spec.rb
87
+ has_rdoc: true
88
+ homepage: http://github.com/crowdint/time_elapser
89
+ licenses:
90
+ - MIT
91
+ post_install_message:
92
+ rdoc_options: []
93
+
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ hash: 3
102
+ segments:
103
+ - 0
104
+ version: "0"
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ hash: 3
111
+ segments:
112
+ - 0
113
+ version: "0"
114
+ requirements: []
115
+
116
+ rubyforge_project:
117
+ rubygems_version: 1.4.2
118
+ signing_key:
119
+ specification_version: 3
120
+ summary: Time-elapser Query cache helper
121
+ test_files:
122
+ - spec/spec_helper.rb
123
+ - spec/time_elapser_spec.rb