timerizer 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 4180c0ffb871e50a47486d122ae17fc42f37cab8
4
- data.tar.gz: dad648d7c6fb1f996b61d5aa1c55a7c2637abc52
2
+ SHA256:
3
+ metadata.gz: e03bc2d5660e629ad3545c610bf09efa55fbe07cec5c364970eabb8001add9fd
4
+ data.tar.gz: 050b2b058e5c63eda87f973e5adde7c84e34c9c230833c47f84eb7189ef0d774
5
5
  SHA512:
6
- metadata.gz: 369ea12d3065905306b0649f81b6517fe49d50cd2ddd601cb7d7132571ea07b21846760bc24e75b3dee56aab841c7b42a6107f54283472e014905910ba1bbca5
7
- data.tar.gz: 02487b58f59677d6cf481f3a3a78a6fe10f04bb598a67a445e8e1e81cb73395267526913041a53c95477882f3bd7fe95868da9a2c190bf13791775b31b4a1510
6
+ metadata.gz: bcc860c64450e0e1f099dca114205f727410a216846c25a61ce75b5052783e95a7b748df72227b9b1b108d44ca0eb11f4dbb81ac7ddcf715629f3a4d9e3135a1
7
+ data.tar.gz: a9836b0cb9c429a8ed66fad757cea99f022934af83e7b5757f8bd84fdf805fe102624d6e14b41d6e9711cbb10dbb53fbd315798b86633de18edfb1e560d2b6ed
@@ -135,6 +135,17 @@ module Timerizer
135
135
  months: ['month', 'months'],
136
136
  years: ['year', 'years']
137
137
  }
138
+ },
139
+ min_long: {
140
+ units: {
141
+ seconds: ['second', 'seconds'],
142
+ minutes: ['minute', 'minutes'],
143
+ hours: ['hour', 'hours'],
144
+ days: ['day', 'days'],
145
+ months: ['month', 'months'],
146
+ years: ['year', 'years']
147
+ },
148
+ count: 2
138
149
  }
139
150
  }
140
151
 
@@ -642,6 +653,74 @@ module Timerizer
642
653
  end.join(format[:delimiter] || ', ')
643
654
  end
644
655
 
656
+ # Convert a Duration to a human-readable string using a rounded value.
657
+ #
658
+ # By 'rounded', we mean that the resulting value is rounded up if the input
659
+ # includes a value of more than half of one of the least-significant unit to
660
+ # be returned. For example, `(17.hours 43.minutes 31.seconds)`, when rounded
661
+ # to two units (hours and minutes), would return "17 hours, 44 minutes". By
662
+ # contrast, `#to_s`, with a `:count` option of 2, would return a value of
663
+ # "17 hours, 43 minutes": truncating, rather than rounding.
664
+ #
665
+ # Note that this method overloads the meaning of the `:count` option value
666
+ # as documented below. If the passed-in option value is numeric, it will be
667
+ # honored, and rounding will take place to that number of units. If the
668
+ # value is either `:all` or the default `nil`, then _rounding_ will be done
669
+ # to two units, and the rounded value will be passed on to `#to_s` with the
670
+ # options specified (which will result in a maximum of two time units being
671
+ # output).
672
+ #
673
+ # @param [Symbol, Hash] format The format type to format the duration with.
674
+ # `format` can either be a key from the {FORMATS} hash or a hash with
675
+ # the same shape as `options`. The default is `:min_long`, which strongly
676
+ # resembles `:long` with the omission of `:weeks` units and a default
677
+ # `:count` of 2.
678
+ # @param [Hash, nil] options Additional options to use to override default
679
+ # format options.
680
+ #
681
+ # @option options [Hash<Symbol, String>] :units The full list of unit names
682
+ # to use. Keys are unit names (see {UNIT_ALIASES} for a full list) and
683
+ # values are strings to use when converting that unit to a string. Values
684
+ # can also be an array, where the first item of the array will be used
685
+ # for singular unit names and the second item will be used for plural
686
+ # unit names. Note that this option will completely override the input
687
+ # formats' list of names, so all units that should be used must be
688
+ # specified!
689
+ # @option options [String] :separator The separator to use between a unit
690
+ # quantity and the unit's name. For example, the string `"1 second"` uses
691
+ # a separator of `" "`.
692
+ # @option options [String] :delimiter The delimiter to use between separate
693
+ # units. For example, the string `"1 minute, 1 second"` uses a separator
694
+ # of `", "`
695
+ # @option options [Integer, nil, :all] :count The number of significant
696
+ # units to use in the string, or `nil` / `:all` to use all units.
697
+ # For example, if the given duration is `1.day 1.week 1.month`, and
698
+ # `options[:count]` is 2, then the resulting string will only include
699
+ # the month and the week components of the string.
700
+ #
701
+ # @return [String] The rounded duration formatted as a string.
702
+ def to_rounded_s(format = :min_long, options = nil)
703
+ format =
704
+ case format
705
+ when Symbol
706
+ FORMATS.fetch(format)
707
+ when Hash
708
+ FORMATS.fetch(:long).merge(format)
709
+ else
710
+ raise ArgumentError, "Expected #{format.inspect} to be a Symbol or Hash"
711
+ end
712
+
713
+ format = format.merge(Hash(options))
714
+ places = format[:count]
715
+ begin
716
+ places = Integer(places) # raise if nil or `:all` supplied as value
717
+ rescue TypeError
718
+ places = 2
719
+ end
720
+ q = RoundedTime.call(self, places)
721
+ q.to_s(format, options)
722
+ end
723
+
645
724
  private
646
725
 
647
726
  # This method is like {#to_unit}, except it does not perform normalization
@@ -764,3 +843,5 @@ module Timerizer
764
843
  self.define_to_unit(:millennium)
765
844
  end
766
845
  end
846
+
847
+ require_relative './duration/rounded_time'
@@ -1,3 +1,3 @@
1
1
  module Timerizer
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: timerizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kyle Lacy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-03 00:00:00.000000000 Z
11
+ date: 2018-12-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -98,7 +98,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
98
98
  version: '0'
99
99
  requirements: []
100
100
  rubyforge_project:
101
- rubygems_version: 2.4.5.1
101
+ rubygems_version: 2.7.7
102
102
  signing_key:
103
103
  specification_version: 4
104
104
  summary: Rails time helpers... without the Rails