smd 1.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 006dee3f382e2b335bbcad9e6b66dd2f665778ea
4
+ data.tar.gz: 777345e69d03b4ba5eeeae22473a697076643966
5
+ SHA512:
6
+ metadata.gz: 7c855a4757e8b831ef9832de9c8228379f9987d825a85044d4189aa660daff6128bc5dae70f62e0fcc9aa564cdf2e28893e2dc59fddb843dffb8cb0b6d061c2f
7
+ data.tar.gz: 128bbfaff59a4919ac7d615ec410641e13f577cb229e752c14dcbf384b30ddb832bf0814553c359e1d05c3a388e867f9fbf798972aa03e12613d98e3205b8928
@@ -0,0 +1,2 @@
1
+ coverage
2
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in simple_secrets.gemspec
4
+ gemspec
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ruby-smd (1.0.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.2.5)
10
+ docile (1.1.3)
11
+ multi_json (1.9.2)
12
+ rake (10.1.1)
13
+ rspec (2.14.1)
14
+ rspec-core (~> 2.14.0)
15
+ rspec-expectations (~> 2.14.0)
16
+ rspec-mocks (~> 2.14.0)
17
+ rspec-core (2.14.8)
18
+ rspec-expectations (2.14.5)
19
+ diff-lcs (>= 1.1.3, < 2.0)
20
+ rspec-mocks (2.14.6)
21
+ simplecov (0.8.2)
22
+ docile (~> 1.1.0)
23
+ multi_json
24
+ simplecov-html (~> 0.8.0)
25
+ simplecov-html (0.8.0)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ bundler (~> 1.3)
32
+ rake
33
+ rspec
34
+ ruby-smd!
35
+ simplecov
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Tim Shadel
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,4 @@
1
+ ruby-smd
2
+ ========
3
+
4
+ Ruby implementation of Time Shadel's SmD (Small Dates) project.
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,59 @@
1
+ require "smd/version"
2
+
3
+ # A Ruby implementation of Small Dates:
4
+ # https://github.com/timshadel/smd
5
+ module SmD
6
+ class SmD
7
+ MS_PER_SECOND = 1000
8
+ MS_PER_MINUTE = 60 * MS_PER_SECOND
9
+ MS_PER_HOUR = 60 * MS_PER_MINUTE
10
+ DEFAULT_RANGE = (2 ** 16)
11
+
12
+ def initialize params = {}
13
+ @range = params[:range] || DEFAULT_RANGE
14
+ @ms_per_unit = params[:ms_per_unit] || MS_PER_HOUR
15
+ end
16
+
17
+ def range
18
+ @range
19
+ end
20
+
21
+ def range_in_ms
22
+ range * ms_per_unit
23
+ end
24
+
25
+ def ms_per_unit
26
+ @ms_per_unit
27
+ end
28
+
29
+ def at units
30
+ (units * ms_per_unit) + ((current_ms / range_in_ms).floor * range_in_ms)
31
+ end
32
+
33
+ def min
34
+ date (-1 * (range / 2))
35
+ end
36
+
37
+ def max
38
+ date (range - 1)
39
+ end
40
+
41
+ def date units
42
+ Time.at(at(units) / MS_PER_SECOND) # Convert from milliseconds to seconds
43
+ end
44
+
45
+ def from milliseconds
46
+ ((milliseconds % range_in_ms) / ms_per_unit).floor
47
+ end
48
+
49
+ def now
50
+ from current_ms
51
+ end
52
+
53
+ private
54
+
55
+ def current_ms
56
+ Time.now.gmtime.to_f * MS_PER_SECOND # Convert from seconds to milliseconds
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,3 @@
1
+ module SmD
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'smd/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "smd"
8
+ spec.version = SmD::VERSION
9
+ spec.authors = ["Tim Shadel", "Jay Wagnon"]
10
+ spec.email = ["tim@shadelsoftware.com", "jaywagnon@gmail.com"]
11
+ spec.description = %q{A Ruby implementation of Time Shadel's SmD (Small Dates) project.}
12
+ spec.summary = %q{A Ruby implementation of Time Shadel's SmD (Small Dates) project.}
13
+ spec.homepage = "https://github.com/jaywagnon/ruby-smd"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "simplecov"
25
+ end
@@ -0,0 +1,13 @@
1
+ require 'simplecov'
2
+ SimpleCov.minimum_coverage 100
3
+ SimpleCov.start 'rails' do
4
+ end
5
+
6
+ SimpleCov.at_exit do
7
+ SimpleCov.result.format!
8
+ if SimpleCov.result.covered_percent < SimpleCov.minimum_coverage
9
+ fail_msg = "#{'%.2f' % SimpleCov.result.covered_percent}% test coverage."
10
+ STDERR.puts "\033[0;31m#{fail_msg}\033[0m\nWrite more tests"
11
+ exit 1
12
+ end
13
+ end
@@ -0,0 +1,72 @@
1
+ require 'spec_helper'
2
+
3
+ describe SmD::SmD do
4
+
5
+ describe "#initialize" do
6
+ context "defaults" do
7
+ its(:range){ should be SmD::SmD::DEFAULT_RANGE }
8
+ its(:ms_per_unit){ should be SmD::SmD::MS_PER_HOUR }
9
+ end
10
+
11
+ context "with params" do
12
+ it "accepts a range" do
13
+ SmD::SmD.new({ range: (2 ** 32) }).range.should eq (2 ** 32)
14
+ end
15
+
16
+ it "accepts milliseconds per unit" do
17
+ SmD::SmD.new({ ms_per_unit: 2000 }).ms_per_unit.should eq (2000)
18
+ end
19
+ end
20
+ end
21
+
22
+ describe "#range_in_ms" do
23
+ its(:range_in_ms){ should be_kind_of Integer }
24
+ its(:range_in_ms){ should eq (subject.range * subject.ms_per_unit) }
25
+ end
26
+
27
+ describe "#date" do
28
+ it "should return a Time" do
29
+ subject.date(-1 * (2 ** 15)).should be_kind_of Time
30
+ end
31
+
32
+ it "should support negative values down to min" do
33
+ subject.date(-1 * (2 ** 15)).should eq Time.at(subject.min)
34
+ end
35
+
36
+ it "should support positive values up to max" do
37
+ subject.date((2 ** 16) - 1).should eq Time.at(subject.max)
38
+ end
39
+ end
40
+
41
+ describe "#min" do
42
+ it "should be -1/2 the range" do
43
+ subject.min.should eq subject.date(-1 * (subject.range / 2))
44
+ end
45
+ end
46
+
47
+ describe "#max" do
48
+ it "should be one less than the range" do
49
+ subject.max.should eq subject.date(subject.range - 1)
50
+ end
51
+ end
52
+
53
+ describe "#at" do
54
+ it "supports negative numbers" do
55
+ subject.at(-1 * (2 ** 15)).should eq (subject.min.to_i * 1000)
56
+ end
57
+ end
58
+
59
+ describe "#from" do
60
+ it "should be less than the range" do
61
+ subject.from(Time.now.gmtime.to_i * 1000).should be < subject.range
62
+ end
63
+ end
64
+
65
+ describe "#now" do
66
+ it "should be within one unit to Time.now" do
67
+ at = subject.at subject.now
68
+ time = (Time.now.gmtime.to_i * 1000)
69
+ (time - at).should be < subject.ms_per_unit
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,4 @@
1
+ require 'simplecov_helper'
2
+
3
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
4
+ require 'smd'
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smd
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Tim Shadel
8
+ - Jay Wagnon
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-04-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ~>
19
+ - !ruby/object:Gem::Version
20
+ version: '1.3'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ version: '1.3'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: simplecov
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ description: A Ruby implementation of Time Shadel's SmD (Small Dates) project.
71
+ email:
72
+ - tim@shadelsoftware.com
73
+ - jaywagnon@gmail.com
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - .gitignore
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - lib/smd.rb
85
+ - lib/smd/version.rb
86
+ - smd.gemspec
87
+ - spec/simplecov_helper.rb
88
+ - spec/smd_spec.rb
89
+ - spec/spec_helper.rb
90
+ homepage: https://github.com/jaywagnon/ruby-smd
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.0.3
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: A Ruby implementation of Time Shadel's SmD (Small Dates) project.
114
+ test_files:
115
+ - spec/simplecov_helper.rb
116
+ - spec/smd_spec.rb
117
+ - spec/spec_helper.rb