Skip to content

TylerRick/activesupport-duration-change

Repository files navigation

ActiveSupport::Duration#change Gem Version

Change/manipulate ActiveSupport::Duration objects with these added methods:

API

#change, #change_cascade

(9.hours + 10.minutes + 40.seconds).change(hours: 12)             #=> 12 hours, 10 minutes, and 40 seconds
(9.hours + 10.minutes + 40.seconds).change(hours: 12, minutes: 5) #=> 12 hours, 5 minutes, and 40 seconds
(9.hours + 10.minutes + 40.seconds).change(minutes: 5)            #=> 9 hours, 5 minutes, and 40 seconds

(9.hours + 10.minutes + 40.seconds).change_cascade(hours: 12)  # => 12 hours
(9.hours + 10.minutes + 40.seconds).change_cascade(minutes: 5) # => 9 hours and 5 minutes

#normalize

90.seconds.normalize                          #=> 1 minute and 30 seconds
23.hours + 59.minutes + 60.seconds).normalize #=> 1 day

#round

30.seconds.round(:minutes)        #=> 1 minute
89.seconds.round(:minutes)        #=> 1 minute
90.seconds.round(:minutes)        #=> 2 minutes
(1.hour + 30.seconds).round(:minutes)  #=> 1 hour and 1 minute

2.5.seconds.round                 #=> 3 seconds
2.5.seconds.round(half: :down)    #=> 2 seconds

#truncate

30.seconds.truncate(:minutes)      #=> 0 seconds

# This could be surprising, but remember that it just looks at the values as they exist in each part.
90.seconds.truncate(:minutes)      #=> 0 seconds

# If needed, you can always normalize the duration first...
90.seconds.normalize                    #=> 1 minute and 30 seconds
90.seconds.normalize.truncate(:minutes) #=> 1 minute

.from_parts

# This is the inverse of #parts.
duration = ActiveSupport::Duration.from_parts({hours: 9, minutes: 10, seconds: 40})

Installation

Add this line to your application's Gemfile:

gem 'activesupport-duration-change'

See also

Development

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.

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.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/TylerRick/activesupport-duration-change.

About

Methods for changing Durations including #change, #truncate, and #round

Resources

License

Stars

Watchers

Forks

Packages

No packages published