time_distance 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1c6121a16f43b7406a84c8d5ad1ac04b5333f49b
4
+ data.tar.gz: '01718557147fb68b124ba1d07fe24246526780cb'
5
+ SHA512:
6
+ metadata.gz: c3947b1a92a155b56c902ddd09e3e51fbdf0ff7063de7a1b9ccdce6b28de902b26514a938a8eeeb610c1ddcca0953540880cfcd74dbbc5bcc84d5f940e4153bb
7
+ data.tar.gz: 8a791c58f029a7e8e3db6248996b442a01d1583f1b5932c6cbdd2fbbed088567744c5922dc65855213bfeed1c92fd58327322df953479a97f07c010d3f4cbea8
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,36 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.4
3
+
4
+ ##################### Styles ##################################
5
+
6
+ Style/Documentation:
7
+ Enabled: false
8
+
9
+ Style/SymbolArray:
10
+ Enabled: false
11
+
12
+ ##################### Metrics ##################################
13
+
14
+ Metrics/LineLength:
15
+ Max: 110
16
+
17
+ Metrics/ClassLength:
18
+ Max: 200
19
+
20
+ Metrics/ModuleLength:
21
+ Max: 200
22
+ Exclude:
23
+ - "**/*_spec.rb"
24
+
25
+ Metrics/BlockLength:
26
+ Max: 50
27
+ Exclude:
28
+ - "**/*_spec.rb"
29
+
30
+ ##################### Rails ##################################
31
+
32
+ Rails:
33
+ Enabled: true
34
+
35
+ Rails/SkipsModelValidations:
36
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.4
5
+ before_install: gem install bundler -v 1.15.4
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in time_distance.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Igor Malinovskiy
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # TimeDistance
2
+
3
+ This gem provides precise time distance between dates
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'time_distance'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install time_distance
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ TimeDistance::Calculator.new(743.minutes.from_now, Time.zone.now).to_s
25
+ => "12h 23min"
26
+ ```
27
+
28
+ ## Development
29
+
30
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
31
+
32
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
33
+
34
+ ## Contributing
35
+
36
+ Bug reports and pull requests are welcome on [GitHub](https://github.com/psy_ipm/time_distance).
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -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
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "time_distance"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,12 @@
1
+ en:
2
+ datetime:
3
+ time_distance:
4
+ units:
5
+ seconds: '%{count} seconds'
6
+ minutes: '%{count} minutes'
7
+ hours: '%{count} hours'
8
+ days: '%{count} days'
9
+ weeks: '%{count} weeks'
10
+ months: '%{count} month'
11
+ years: '%{count} years'
12
+ ago: 'ago'
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/time'
4
+
5
+ module TimeDistance
6
+ class Calculator
7
+ MULTIPLES = {
8
+ seconds: 1,
9
+ minutes: 1.minute,
10
+ hours: 1.hour,
11
+ days: 1.day,
12
+ weeks: 1.week,
13
+ months: 1.month,
14
+ years: 1.year
15
+ }.freeze
16
+
17
+ DEFAULTS = {
18
+ format: [:months, :days, :hours, :minutes],
19
+ translation_scope: 'time_distance'
20
+ }.freeze
21
+
22
+ attr_reader :options
23
+
24
+ def initialize(time_from, time_to, options = {})
25
+ @time_from = time_from
26
+ @time_to = time_to
27
+ @options = DEFAULTS.merge(options)
28
+
29
+ @seconds = @time_from - @time_to
30
+
31
+ @negative = @seconds.negative?
32
+ end
33
+
34
+ def to_s
35
+ array = to_a.compact
36
+ array << I18n.t(:ago, scope: options[:translation_scope]) if @negative
37
+
38
+ array.join(' ')
39
+ end
40
+
41
+ def units
42
+ @units ||= calculate
43
+ end
44
+
45
+ private
46
+
47
+ def to_a
48
+ options[:format].map do |unit|
49
+ value = units[unit].to_i
50
+ next unless value.positive?
51
+
52
+ I18n.t(unit, scope: [options[:translation_scope], :units], count: value)
53
+ end
54
+ end
55
+
56
+ def desc_sorted_unit_format
57
+ options[:format].sort { |p, n| MULTIPLES[n] <=> MULTIPLES[p] }
58
+ end
59
+
60
+ def calculate
61
+ values = {}
62
+
63
+ desc_sorted_unit_format.inject(@seconds.abs.to_f.round) do |remainder, unit|
64
+ multiple = MULTIPLES[unit]
65
+
66
+ values[unit] = remainder / multiple
67
+ remainder % multiple
68
+ end
69
+
70
+ values
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TimeDistance
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'time_distance/version'
4
+
5
+ module TimeDistance
6
+ autoload :Calculator, 'time_distance/calculator'
7
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'time_distance/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'time_distance'
9
+ spec.version = TimeDistance::VERSION
10
+ spec.authors = ['Igor Malinovskiy']
11
+ spec.email = ['igor.malinovskiy@netfixllc.org']
12
+
13
+ spec.summary = 'Time distance in words'
14
+ spec.description = 'This gem provides precise time distance between dates'
15
+ spec.homepage = 'https://github.com/psyipm/time_distance'
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.15'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'rspec', '~> 3.0'
28
+
29
+ spec.add_dependency 'activesupport', '>= 4.0'
30
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: time_distance
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Igor Malinovskiy
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-03-26 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.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.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: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '4.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '4.0'
69
+ description: This gem provides precise time distance between dates
70
+ email:
71
+ - igor.malinovskiy@netfixllc.org
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".rubocop.yml"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - config/locales/en.yml
87
+ - lib/time_distance.rb
88
+ - lib/time_distance/calculator.rb
89
+ - lib/time_distance/version.rb
90
+ - time_distance.gemspec
91
+ homepage: https://github.com/psyipm/time_distance
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.5.2
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Time distance in words
115
+ test_files: []