easy_timer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in easy_timer.gemspec
4
+ gemspec
5
+
6
+ gem 'rspec'
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Isaac Sloan
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.
@@ -0,0 +1,46 @@
1
+ # EasyTimer
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'easy_timer'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install easy_timer
18
+
19
+ ## Usage
20
+ Calling timer will return a time object.
21
+
22
+ $ Time.timer{sleep 1} #=> Time object
23
+
24
+ Calling verbose on a Time object will return a formatted string.
25
+
26
+ $ Time.timer{sleep 1}.verbose #=> 1.00 seconds
27
+
28
+ You can also pass :v => true or :verbose => true.
29
+
30
+ $ Time.timer(:v => true){sleep 1} #=> 1.00 seconds
31
+
32
+ A practical usage would be:
33
+
34
+ $ puts Time.timer do
35
+ $ Model.update_all({:criteria => false}, {:criteria => true})
36
+ $ puts "All models with criteria=false have been updated."
37
+ $ end.verbose
38
+
39
+
40
+ ## Contributing
41
+
42
+ 1. Fork it
43
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
44
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
45
+ 4. Push to the branch (`git push origin my-new-feature`)
46
+ 5. Create new Pull Request
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new('spec')
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/easy_timer/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Isaac Sloan"]
6
+ gem.email = ["isaac@isaacsloan.com"]
7
+ gem.description = %q{Easy Timer}
8
+ gem.summary = %q{Adds easy to use timer method to Time class.}
9
+ gem.homepage = "https://github.com/isaacsloan/easy_timer"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "easy_timer"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = EasyTimer::VERSION
17
+ end
@@ -0,0 +1,39 @@
1
+ require "easy_timer/version"
2
+
3
+ module EasyTimer
4
+ def self.included(base)
5
+ base.extend(ClassMethods)
6
+ end
7
+
8
+ module ClassMethods
9
+ def timer(options = {}, &block)
10
+ start_time = Time.now
11
+ yield
12
+ total_time = Time.at(Time.now - start_time)
13
+ if options[:verbose] || options[:v]
14
+ return total_time.verbose
15
+ else
16
+ return total_time
17
+ end
18
+ end
19
+ end
20
+
21
+ def verbose
22
+ time_hash = {}
23
+ seconds = self.to_i
24
+ milliseconds = (self.to_f - seconds)
25
+ time_hash[:weeks], seconds = seconds.divmod(604800)
26
+ time_hash[:days], seconds = seconds.divmod(86400)
27
+ time_hash[:hours], seconds = seconds.divmod(3600)
28
+ time_hash[:minutes], seconds = seconds.divmod(60)
29
+ time_hash[:seconds] = seconds + milliseconds
30
+ return time_hash.select{|k,v| v > 0}.inject([]){|verbose, array|
31
+ k, v = array
32
+ verbose << "#{v.is_a?(Float) ? sprintf("%.2f", v) : v} #{k}"
33
+ }.join(" ")
34
+ end
35
+ end
36
+
37
+ class Time
38
+ include EasyTimer
39
+ end
@@ -0,0 +1,3 @@
1
+ module EasyTimer
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easy_timer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Isaac Sloan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-01 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Easy Timer
15
+ email:
16
+ - isaac@isaacsloan.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE
24
+ - README.md
25
+ - Rakefile
26
+ - easy_timer.gemspec
27
+ - lib/easy_timer.rb
28
+ - lib/easy_timer/version.rb
29
+ homepage: https://github.com/isaacsloan/easy_timer
30
+ licenses: []
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubyforge_project:
49
+ rubygems_version: 1.8.24
50
+ signing_key:
51
+ specification_version: 3
52
+ summary: Adds easy to use timer method to Time class.
53
+ test_files: []