go_duration 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a9a3aad48456c17c83fbe7180b66fb218c632106
4
+ data.tar.gz: a2b0cd5b6e1fea3ccd2aa42f95f06a65d13aa138
5
+ SHA512:
6
+ metadata.gz: eb2244362e60766cda6e6d0077013018213845ec7aa11d2728dc7936078e9b8b235153c8ea4adbfded20e11d1709f2073a6150abc666ebd8266be98c2aa3372c
7
+ data.tar.gz: af6a6a5588eb1c751c430bb96166ebab27e62bb121390cba362b12cfb4d9c3c8a93881422fdfe417a71031456e6262d597be80e9c001b20dd4fa646f7e98e010
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 2.2
5
+ - 2.3
6
+ - 2.4
7
+ - ruby-head
8
+
9
+ matrix:
10
+ allow_failures:
11
+ - rvm: ruby-head
@@ -0,0 +1 @@
1
+ # 0.1.0 (Initial release)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in go_duration.gemspec
4
+ gemspec
@@ -0,0 +1,45 @@
1
+ # GoDuration for Ruby
2
+ [![Gem Version](https://badge.fury.io/rb/go_duration.svg)](https://badge.fury.io/rb/go_duration)
3
+ [![Build Status](https://travis-ci.org/justincampbell/go_duration_ruby.svg?branch=master)](https://travis-ci.org/justincampbell/go_duration_ruby)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'go_duration'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install go_duration
20
+
21
+ ## Usage
22
+
23
+ ### Parsing
24
+
25
+ ```rb
26
+ require 'go_duration'
27
+
28
+ GoDuration.parse("2m30s") # => 150
29
+ ```
30
+
31
+ ### Generating
32
+
33
+ ```rb
34
+ require 'go_duration'
35
+
36
+ GoDuration.generate(150) # => "2m30s"
37
+
38
+ require 'go_duration/core_ext'
39
+
40
+ 150.to_go_duration # => "2m30"
41
+
42
+ require 'active_support/core_ext'
43
+
44
+ (2.hours + 45.minutes).to_go_duration # => "2h45m"
45
+ ```
@@ -0,0 +1,8 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ task default: :test
5
+
6
+ Rake::TestTask.new(:test) do |test|
7
+ test.pattern = "test/**/*_test.rb"
8
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "go_duration"
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
@@ -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,21 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'go_duration/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "go_duration"
8
+ spec.version = GoDuration::VERSION
9
+ spec.authors = ["Justin Campbell"]
10
+ spec.email = ["justin@justincampbell.me"]
11
+
12
+ spec.summary = "Go's time.Duration parsing/serialization for Ruby"
13
+ spec.homepage = "https://github.com/justincampbell/go_duration_ruby"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.require_paths = ["lib"]
17
+
18
+ spec.add_development_dependency "bundler", "~> 1.0"
19
+ spec.add_development_dependency "minitest", "~> 5.0"
20
+ spec.add_development_dependency "rake", "~> 10.0"
21
+ end
@@ -0,0 +1,21 @@
1
+ require 'go_duration/generator'
2
+ require 'go_duration/parser'
3
+ require 'go_duration/version'
4
+
5
+ module GoDuration
6
+ SECOND = 1
7
+ MINUTE = SECOND * 60
8
+ HOUR = MINUTE * 60
9
+
10
+ SECOND_SUFFIX = ?s.freeze
11
+ MINUTE_SUFFIX = ?m.freeze
12
+ HOUR_SUFFIX = ?h.freeze
13
+
14
+ def self.generate(seconds)
15
+ Generator.new(seconds).to_s
16
+ end
17
+
18
+ def self.parse(input)
19
+ Parser.new(input).seconds
20
+ end
21
+ end
@@ -0,0 +1,11 @@
1
+ module GoDuration
2
+ module CoreExt
3
+ module Numeric
4
+ def to_go_duration
5
+ GoDuration.generate(self)
6
+ end
7
+ end
8
+ end
9
+ end
10
+
11
+ Numeric.include GoDuration::CoreExt::Numeric
@@ -0,0 +1,26 @@
1
+ module GoDuration
2
+ class Generator
3
+ attr_reader :seconds
4
+
5
+ def initialize(seconds)
6
+ @seconds = seconds
7
+ end
8
+
9
+ def to_s
10
+ seconds_remaining = seconds
11
+
12
+ minutes = seconds_remaining / 60
13
+ seconds_remaining = seconds_remaining % 60
14
+
15
+ hours = minutes / 60
16
+ minutes = minutes % 60
17
+
18
+ buffer = ""
19
+ buffer.prepend "#{seconds_remaining.to_i}s" if seconds_remaining.nonzero?
20
+ buffer.prepend "#{minutes.to_i}m" if minutes.nonzero?
21
+ buffer.prepend "#{hours.to_i}h" if hours.nonzero?
22
+ buffer = "0s" if buffer == ""
23
+ buffer
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,27 @@
1
+ module GoDuration
2
+ class Parser
3
+ PART_REGEXP = %r{\d+[hms]}.freeze
4
+
5
+ def self.parse_part(part)
6
+ case
7
+ when part.end_with?(SECOND_SUFFIX) then part.to_i
8
+ when part.end_with?(MINUTE_SUFFIX) then part.to_i * MINUTE
9
+ when part.end_with?(HOUR_SUFFIX) then part.to_i * HOUR
10
+ end
11
+ end
12
+
13
+ attr_reader :input
14
+
15
+ def initialize(input)
16
+ @input = input
17
+ end
18
+
19
+ def seconds
20
+ parts.map { |part| self.class.parse_part(part) }.reduce(:+)
21
+ end
22
+
23
+ def parts
24
+ @parts ||= input.scan(PART_REGEXP)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ module GoDuration
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: go_duration
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Justin Campbell
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-05-19 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.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description:
56
+ email:
57
+ - justin@justincampbell.me
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - CHANGELOG.md
65
+ - Gemfile
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - go_duration.gemspec
71
+ - lib/go_duration.rb
72
+ - lib/go_duration/core_ext.rb
73
+ - lib/go_duration/generator.rb
74
+ - lib/go_duration/parser.rb
75
+ - lib/go_duration/version.rb
76
+ homepage: https://github.com/justincampbell/go_duration_ruby
77
+ licenses: []
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.6.11
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Go's time.Duration parsing/serialization for Ruby
99
+ test_files: []