motion-duration 0.0.1
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 +7 -0
- data/README.md +61 -0
- data/lib/motion-duration.rb +10 -0
- data/lib/project/ext.rb +5 -0
- data/lib/project/motion-duration.rb +191 -0
- metadata +64 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4cbcecf706355f4e25da91d2fb26273343e0dc5c
|
4
|
+
data.tar.gz: 5c27161964749bbcc3ddfd0a036ad8fb11e47a53
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bcc4a70e1b9a18e8df7b374121a4d2fae377bdd20e265d381db0f202f67abd098f3a884c4f1e4987327cf30e497175b23f579b3f9c449464d9d5e353cda7a6ce
|
7
|
+
data.tar.gz: ee785e180abdd260d40080b06f41971d04794d087dc47b3881e5bf3dc34d33eb3e89654223e50a905dea1fa3876e2b9fe90266fb9105b29aea2d478d489f047a
|
data/README.md
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# motion-duration
|
2
|
+
|
3
|
+
[](https://travis-ci.org/OTGApps/motion-duration)
|
4
|
+
|
5
|
+
Motion::Duration is an immutable type that represents some amount of time with accuracy in seconds.
|
6
|
+
|
7
|
+
It was adapted for RubyMotion from [ruby-duration](https://github.com/peleteiro/ruby-duration) by Jose Peleteiro since it required ActiveRecord, iso8601, and i18n.
|
8
|
+
|
9
|
+
This version currently lacks i18n, iso8601, and formatting support.
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
gem 'motion-duration'
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install motion-duration
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
Duration.new(100) => #<Duration: minutes=1, seconds=40, total=100>
|
29
|
+
Duration.new(:hours => 5, :minutes => 70) => #<Duration: hours=6, minutes=10, total=22200>
|
30
|
+
```
|
31
|
+
|
32
|
+
## Contributing
|
33
|
+
|
34
|
+
1. Fork it
|
35
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
36
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
37
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
38
|
+
5. Create new Pull Request
|
39
|
+
|
40
|
+
## Original License
|
41
|
+
|
42
|
+
Copyright (c) 2010 Jose Peleteiro
|
43
|
+
|
44
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
45
|
+
a copy of this software and associated documentation files (the
|
46
|
+
"Software"), to deal in the Software without restriction, including
|
47
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
48
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
49
|
+
permit persons to whom the Software is furnished to do so, subject to
|
50
|
+
the following conditions:
|
51
|
+
|
52
|
+
The above copyright notice and this permission notice shall be
|
53
|
+
included in all copies or substantial portions of the Software.
|
54
|
+
|
55
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
56
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
57
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
58
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
59
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
60
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
61
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
unless defined?(Motion::Project::Config)
|
4
|
+
raise "This file must be required within a RubyMotion project Rakefile."
|
5
|
+
end
|
6
|
+
|
7
|
+
lib_dir_path = File.dirname(File.expand_path(__FILE__))
|
8
|
+
Motion::Project::App.setup do |app|
|
9
|
+
app.files.unshift(Dir.glob(File.join(lib_dir_path, "project/**/*.rb")))
|
10
|
+
end
|
data/lib/project/ext.rb
ADDED
@@ -0,0 +1,191 @@
|
|
1
|
+
class Motion
|
2
|
+
class Duration
|
3
|
+
UNITS = [:seconds, :minutes, :hours, :days, :weeks]
|
4
|
+
|
5
|
+
MULTIPLES = {:seconds => 1,
|
6
|
+
:minutes => 60,
|
7
|
+
:hours => 3600,
|
8
|
+
:days => 86400,
|
9
|
+
:weeks => 604800,
|
10
|
+
:second => 1,
|
11
|
+
:minute => 60,
|
12
|
+
:hour => 3600,
|
13
|
+
:day => 86400,
|
14
|
+
:week => 604800}
|
15
|
+
|
16
|
+
attr_reader :weeks, :days, :hours, :minutes, :seconds, :total
|
17
|
+
|
18
|
+
# Initialize a duration. 'args' can be a hash or anything else. If a hash is
|
19
|
+
# passed, it will be scanned for a key=>value pair of time units such as those
|
20
|
+
# listed in the Duration::UNITS array or Duration::MULTIPLES hash.
|
21
|
+
#
|
22
|
+
# If anything else except a hash is passed, #to_i is invoked on that object
|
23
|
+
# and expects that it return the number of seconds desired for the duration.
|
24
|
+
def initialize(args = 0)
|
25
|
+
if args.kind_of?(Hash)
|
26
|
+
@seconds = 0
|
27
|
+
MULTIPLES.each do |unit, multiple|
|
28
|
+
unit = unit.to_sym
|
29
|
+
@seconds += args[unit].to_i * multiple if args.key?(unit)
|
30
|
+
end
|
31
|
+
# elsif args.kind_of?(String) and args[0] == 'P'
|
32
|
+
# @seconds = ISO8601::Duration.new(args).to_seconds
|
33
|
+
else
|
34
|
+
@seconds = args.to_i
|
35
|
+
end
|
36
|
+
|
37
|
+
calculate!
|
38
|
+
end
|
39
|
+
|
40
|
+
# Compare this duration to another (or objects that respond to #to_i)
|
41
|
+
def <=>(other)
|
42
|
+
return false unless other.is_a?(Duration)
|
43
|
+
(@total <=> other.to_i).duration_comparable_bool
|
44
|
+
end
|
45
|
+
|
46
|
+
def +(other)
|
47
|
+
Duration.new(@total + other.to_i)
|
48
|
+
end
|
49
|
+
|
50
|
+
def -(other)
|
51
|
+
Duration.new(@total - other.to_i)
|
52
|
+
end
|
53
|
+
|
54
|
+
def *(other)
|
55
|
+
Duration.new(@total * other.to_i)
|
56
|
+
end
|
57
|
+
|
58
|
+
def /(other)
|
59
|
+
Duration.new(@total / other.to_i)
|
60
|
+
end
|
61
|
+
|
62
|
+
def %(other)
|
63
|
+
Duration.new(@total % other.to_i)
|
64
|
+
end
|
65
|
+
|
66
|
+
%w(minutes hours days).each do |meth|
|
67
|
+
define_method("total_#{meth}") { @total / MULTIPLES[meth.to_sym] }
|
68
|
+
end
|
69
|
+
|
70
|
+
# @return true if total is 0
|
71
|
+
def blank?
|
72
|
+
@total == 0
|
73
|
+
end
|
74
|
+
|
75
|
+
# @return true if total different than 0
|
76
|
+
def present?
|
77
|
+
!blank?
|
78
|
+
end
|
79
|
+
|
80
|
+
def negative?
|
81
|
+
@negative
|
82
|
+
end
|
83
|
+
|
84
|
+
# Format a duration into a human-readable string.
|
85
|
+
#
|
86
|
+
# %w => weeks
|
87
|
+
# %d => days
|
88
|
+
# %h => hours
|
89
|
+
# %m => minutes
|
90
|
+
# %s => seconds
|
91
|
+
# %td => total days
|
92
|
+
# %th => total hours
|
93
|
+
# %tm => total minutes
|
94
|
+
# %ts => total seconds
|
95
|
+
# %t => total seconds
|
96
|
+
# %MP => minutes with UTF-8 prime
|
97
|
+
# %SP => seconds with UTF-8 double-prime
|
98
|
+
# %MH => minutes with HTML prime
|
99
|
+
# %SH => seconds with HTML double-prime
|
100
|
+
# %H => zero-padded hours
|
101
|
+
# %M => zero-padded minutes
|
102
|
+
# %S => zero-padded seconds
|
103
|
+
# %~s => locale-dependent "seconds" terminology
|
104
|
+
# %~m => locale-dependent "minutes" terminology
|
105
|
+
# %~h => locale-dependent "hours" terminology
|
106
|
+
# %~d => locale-dependent "days" terminology
|
107
|
+
# %~w => locale-dependent "weeks" terminology
|
108
|
+
# %tdu => total days with locale-dependent unit
|
109
|
+
# %thu => total hours with locale-dependent unit
|
110
|
+
# %tmu => total minutes with locale-dependent unit
|
111
|
+
# %tsu => total seconds with locale-dependent unit
|
112
|
+
#
|
113
|
+
#
|
114
|
+
# def format(format_str)
|
115
|
+
# identifiers = {
|
116
|
+
# 'w' => @weeks,
|
117
|
+
# 'd' => @days,
|
118
|
+
# 'h' => @hours,
|
119
|
+
# 'm' => @minutes,
|
120
|
+
# 's' => @seconds,
|
121
|
+
# 'td' => Proc.new { total_days },
|
122
|
+
# 'th' => Proc.new { total_hours },
|
123
|
+
# 'tm' => Proc.new { total_minutes },
|
124
|
+
# 'ts' => @total,
|
125
|
+
# 't' => @total,
|
126
|
+
# 'MP' => Proc.new { "#{@minutes}′"},
|
127
|
+
# 'SP' => Proc.new { "#{@seconds}″"},
|
128
|
+
# 'MH' => Proc.new { "#{@minutes}′"},
|
129
|
+
# 'SH' => Proc.new { "#{@seconds}″"},
|
130
|
+
# 'H' => @hours.to_s.rjust(2, '0'),
|
131
|
+
# 'M' => @minutes.to_s.rjust(2, '0'),
|
132
|
+
# 'S' => @seconds.to_s.rjust(2, '0'),
|
133
|
+
# '~s' => i18n_for(:second),
|
134
|
+
# '~m' => i18n_for(:minute),
|
135
|
+
# '~h' => i18n_for(:hour),
|
136
|
+
# '~d' => i18n_for(:day),
|
137
|
+
# '~w' => i18n_for(:week),
|
138
|
+
# 'tdu'=> Proc.new { "#{total_days} #{i18n_for(:total_day)}"},
|
139
|
+
# 'thu'=> Proc.new { "#{total_hours} #{i18n_for(:total_hour)}"},
|
140
|
+
# 'tmu'=> Proc.new { "#{total_minutes} #{i18n_for(:total_minute)}"},
|
141
|
+
# 'tsu'=> Proc.new { "#{total} #{i18n_for(:total)}"},
|
142
|
+
# }
|
143
|
+
|
144
|
+
# format_str.gsub(/%?%(w|d|h|m|s|t([dhms]u?)?|MP|SP|MH|SH|H|M|S|~(?:s|m|h|d|w))/) do |match|
|
145
|
+
# match['%%'] ? match : (identifiers[match[1..-1]].class == Proc ? identifiers[match[1..-1]].call : identifiers[match[1..-1]])
|
146
|
+
# end.gsub('%%', '%')
|
147
|
+
# end
|
148
|
+
|
149
|
+
alias_method :to_i, :total
|
150
|
+
# alias_method :strftime, :format
|
151
|
+
|
152
|
+
private
|
153
|
+
|
154
|
+
# Calculates the duration from seconds and figures out what the actual
|
155
|
+
# durations are in specific units. This method is called internally, and
|
156
|
+
# does not need to be called by user code.
|
157
|
+
def calculate!
|
158
|
+
multiples = [MULTIPLES[:weeks], MULTIPLES[:days], MULTIPLES[:hours], MULTIPLES[:minutes], MULTIPLES[:seconds]]
|
159
|
+
units = []
|
160
|
+
@negative = @seconds < 0
|
161
|
+
@total = @seconds.abs.to_f.round
|
162
|
+
multiples.inject(@total) do |total, multiple|
|
163
|
+
# Divide into largest unit
|
164
|
+
units << total / multiple
|
165
|
+
total % multiple # The remainder will be divided as the next largest
|
166
|
+
end
|
167
|
+
|
168
|
+
# Gather the divided units
|
169
|
+
@weeks, @days, @hours, @minutes, @seconds = units
|
170
|
+
end
|
171
|
+
|
172
|
+
# def i18n_for(singular)
|
173
|
+
# if singular == :total
|
174
|
+
# fn_name = :total
|
175
|
+
# singular = :second
|
176
|
+
# plural = 'seconds'
|
177
|
+
# elsif singular.to_s.start_with?('total_')
|
178
|
+
# fn_name = "#{singular}s"
|
179
|
+
# singular = singular.to_s['total_'.length..-1]
|
180
|
+
# plural = "#{singular}s"
|
181
|
+
# else
|
182
|
+
# plural = "#{singular}s"
|
183
|
+
# fn_name = plural
|
184
|
+
# end
|
185
|
+
# label = send(fn_name) == 1 ? singular : plural
|
186
|
+
|
187
|
+
# I18n.t(label, :scope => :ruby_duration, :default => label.to_s)
|
188
|
+
# end
|
189
|
+
|
190
|
+
end
|
191
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: motion-duration
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mark Rickert
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-01-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: Motion::Duration is an immutable type that represents some amount of
|
28
|
+
time with accuracy in seconds.
|
29
|
+
email:
|
30
|
+
- mark@otgapps.io
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- README.md
|
36
|
+
- lib/motion-duration.rb
|
37
|
+
- lib/project/ext.rb
|
38
|
+
- lib/project/motion-duration.rb
|
39
|
+
homepage: https://github.com/otgapps/motion-duration
|
40
|
+
licenses:
|
41
|
+
- MIT
|
42
|
+
metadata: {}
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
requirements: []
|
58
|
+
rubyforge_project:
|
59
|
+
rubygems_version: 2.2.0
|
60
|
+
signing_key:
|
61
|
+
specification_version: 4
|
62
|
+
summary: Motion::Duration is an immutable type that represents some amount of time
|
63
|
+
with accuracy in seconds.
|
64
|
+
test_files: []
|