nostradamus 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ *.rvmrc
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ bin/
11
+ coverage
12
+ doc/
13
+ lib/bundler/man
14
+ log
15
+ pkg
16
+ rdoc
17
+ spec/reports
18
+ test/tmp
19
+ test/version_tmp
20
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,5 @@
1
+ module Nostradamus
2
+ module Version
3
+ STRING = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,58 @@
1
+ module Nostradamus
2
+ extend self
3
+ HOURS_REGEX = /^([0-9]*):.*/
4
+ MINUTES_REGEX = /[0-9]*:([0-9]*).*/
5
+ SECONDS_REGEX = /[0-9]*:[0-9]*:([0-9]*)/
6
+
7
+ def parser(human_time)
8
+ convert(human_time, :to => :seconds)
9
+ end
10
+
11
+ def humanize(seconds, format = nil)
12
+ convert(seconds, :to => :human_time, :format => format)
13
+ end
14
+
15
+ private
16
+
17
+ def convert(time, params)
18
+ case params[:to]
19
+ when :seconds
20
+ hours = extract_hours_from_human_time(time)
21
+ minutes = extract_minutes_from_human_time(time)
22
+ seconds = extract_seconds_from_human_time(time)
23
+
24
+ (hours * 3600) + (minutes * 60) + (seconds)
25
+ when :human_time
26
+ hours, minutes, seconds = extract_time_from_seconds(time)
27
+
28
+ if params[:format] && params[:format] == :short
29
+ "#{hours}:#{"%02d" % minutes}"
30
+ else
31
+ "#{hours}:#{"%02d" % minutes}:#{"%02d" % seconds}"
32
+ end
33
+ end
34
+ end
35
+
36
+ def extract_hours_from_human_time(time)
37
+ time.match(HOURS_REGEX)
38
+ matched($1)
39
+ end
40
+
41
+ def extract_minutes_from_human_time(time)
42
+ time.match(MINUTES_REGEX)
43
+ matched($1)
44
+ end
45
+
46
+ def extract_seconds_from_human_time(time)
47
+ time.match(SECONDS_REGEX)
48
+ matched($1)
49
+ end
50
+
51
+ def matched(extracted)
52
+ (extracted || 0).to_i
53
+ end
54
+
55
+ def extract_time_from_seconds(seconds)
56
+ [(seconds / 3600), ((seconds / 60) % 60), (seconds % 60)]
57
+ end
58
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ $LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
4
+ require 'nostradamus/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.authors = ["Cairo Noleto"]
8
+ gem.email = ["caironoleto@gmail.com"]
9
+ gem.description = %q{Time calculation}
10
+ gem.summary = %q{Time calculation}
11
+ gem.homepage = "https://github.com/caironoleto/nostradamus"
12
+
13
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
14
+ gem.files = `git ls-files`.split("\n")
15
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ gem.name = "nostradamus"
17
+ gem.require_paths = ["lib"]
18
+ gem.version = Nostradamus::Version::STRING
19
+
20
+ gem.add_development_dependency 'rspec', '>= 2.11.0'
21
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+ require 'nostradamus'
3
+
4
+ describe Nostradamus do
5
+ context "human time to seconds" do
6
+ it "should parse human time to seconds" do
7
+ described_class.parser("12:00").should eq 43200
8
+ described_class.parser("12:00:00").should eq 43200
9
+ described_class.parser("12:15").should eq 44100
10
+ described_class.parser("12:15:15").should eq 44115
11
+ end
12
+ end
13
+
14
+ context "seconds to human time" do
15
+ it "should convert seconds to human time" do
16
+ described_class.humanize(43200, :short).should eq "12:00"
17
+ described_class.humanize(44100).should eq "12:15:00"
18
+ described_class.humanize(44115).should eq "12:15:15"
19
+ described_class.humanize(44115, :short).should eq "12:15"
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,17 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nostradamus
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Cairo Noleto
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 2.11.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 2.11.0
30
+ description: Time calculation
31
+ email:
32
+ - caironoleto@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - .rspec
39
+ - Gemfile
40
+ - lib/nostradamus.rb
41
+ - lib/nostradamus/version.rb
42
+ - nostradamus.gemspec
43
+ - spec/nostradamus/nostradamus_spec.rb
44
+ - spec/spec_helper.rb
45
+ homepage: https://github.com/caironoleto/nostradamus
46
+ licenses: []
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubyforge_project:
65
+ rubygems_version: 1.8.19
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Time calculation
69
+ test_files:
70
+ - spec/nostradamus/nostradamus_spec.rb
71
+ - spec/spec_helper.rb