• Stars
    star
    125
  • Rank 277,736 (Top 6 %)
  • Language
    Ruby
  • License
    MIT License
  • Created about 9 years ago
  • Updated almost 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Extraction of ActiveSupport::Duration from Rails

AS::Duration

This gem is an extraction of ActiveSupport::Duration from Rails, along with the related core extensions.

Ruby 2.0 or greater is required.

Why not simply use ActiveSupport?

If you're in a Rails project, then you should use ActiveSupport::Duration. Otherwise there are several reason why you might prefer as-duration:

  • You simply don't want to have ActiveSupport as a dependency
  • You want to control what you require. You may think that requiring active_support/core_ext/integer/time will only require what you want, but in fact it will require a total of 5000 LOC (a lot of those are additional core extensions which you may not have wanted). as-duration has only under 200 LOC, and only gives you what you've asked for.

Is it well tested?

It sure is! I copied all the related tests from Rails, and modified them so that they work standalone. So, as-duration passes all of Rails' tests.

Installation

gem 'as-duration'

Features

NOTE: In most cases as-duration should work exactly like ActiveSupport::Duration. However, there are a few modifications made, mostly removing some of the magic, see Modifications.

Numeric methods

The following methods are added on Numeric (Float and Integer):

# plural versions
2.seconds
3.minutes
4.hours
5.days
6.weeks
7.fortnights
8.months
9.years

# singular versions
1.second
1.minute
1.hour
1.day
1.week
1.fortnight
1.month
1.year

The only exception is #months and #years which are only added to Integer (to maintain precision in calculations).

Duration/Time arithmetics

You can add and subtract durations from Time or Date objects.

Time.now + 2.hours
Date.today + 1.year

When you add seconds/minutes/hours to a Date, the Date is automatically converted to a Time object.

(Date.today + 1.minute).class #=> Time

As syntax sugar, you can also call time methods on the duration object:

# forward in time
1.year.from_now
2.months.since(Date.new(2015,4,27))
2.months.after(Date.new(2015,4,27))
2.months.from(Date.new(2015,4,27))

# back in time
2.hours.ago
20.minutes.until(Time.now)
20.minutes.before(Time.now)
20.minutes.to(Time.now)

Duration/Duration arithmetics

You can add and subtract durations:

1.week + 1.day
2.minutes - 1.second

Unlike ActiveSupport::Duration, you can't add durations to integers and vice versa. You either have to convert the integer to a duration, or the duration to an integer with AS::Duration#to_i. This is to help you not to mix different time units.

# Bad
10 + 1.minute  # TypeError
1.minute + 10  # TypeError

# Good
10.seconds + 1.minute  # AS::Duration
1.minute.to_i + 10     # Integer

Modifications to ActiveSupport::Duration

The behaviour of ActiveSupport::Duration has been slightly modified, mostly to remove some magic:

  • Added #from, #after, #before and #to to AS::Duration
  • #from_now and #ago cannot take any arguments, they always use the current time (passing an argument doesn't read well, better to use #from and #until)
  • Removed support for DateTime
    • DateTime was first introduced in Ruby so that you can represent time that the Time class at the moment wasn't able to. However, the Time class improved over time and removed those limitations, so there is no more need to use DateTime
  • Year lasts 365 days instead of 365.25
  • AS::Duration doesn't act like an Integer
    • to compare it with an integer you have to either convert the integer to a duration or convert the duration to an integer (with #to_i)
    • you can only add and subtract two duration objects
  • Removed hash equality

License

MIT

More Repositories

1

down

Streaming downloads using net/http, http.rb, HTTPX or wget
Ruby
979
star
2

image_processing

High-level image processing wrapper for libvips and ImageMagick/GraphicsMagick
Ruby
825
star
3

rodauth-rails

Rails integration for Rodauth authentication framework
HTML
424
star
4

tus-ruby-server

Ruby server for tus resumable upload protocol
Ruby
215
star
5

paperclip-dropbox

[OBSOLETE] Extends Paperclip with Dropbox storage.
Ruby
148
star
6

sequel-activerecord_connection

Allows Sequel to reuse Active Record's database connection
Ruby
121
star
7

uppy-s3_multipart

Provides Ruby endpoints for aws-s3-multipart Uppy plugin
Ruby
62
star
8

tic-tac-toe

Play tic-tac-toe in your Terminal
Ruby
43
star
9

rodauth-demo-rails

Example Rails app that uses Rodauth for authentication
Ruby
36
star
10

rodauth-omniauth

OmniAuth login and registration for Rodauth authentication framework
Ruby
34
star
11

flickr-objects

An object-oriented wrapper for the Flickr API.
Ruby
28
star
12

shrine-example

[MOVED] Roda & Sequel demo for Shrine
JavaScript
19
star
13

rodauth-i18n

I18n integration and translations for Rodauth authentication framework
Ruby
16
star
14

flickrie

[DEPRECATED] A wrapper gem for the Flickr API
Ruby
14
star
15

rodauth-model

Password attribute and associations for Rodauth account model
Ruby
13
star
16

rodauth-pwned

Rodauth extension that checks user passwords against the Pwned Passwords API
Ruby
12
star
17

goliath-rack_proxy

[DEPRECATED] Goliath as a web server for your Rack app
Ruby
11
star
18

budget

Roda & Sequel app for tracking expenses
Ruby
8
star
19

janko.io

My blog
HTML
7
star
20

dotfiles

My dotfiles
Vim Script
6
star
21

hanami-rodauth-example

Example Hanami 2 application using Rodauth authentication framework
Ruby
6
star
22

musique

A Ruby gem for manipulating musical constructs.
Ruby
6
star
23

capybara-vs-webdriverio

Battle between Capybara and Webdriver.io for browser testing
JavaScript
5
star
24

flickr-login

A gem that provides Flickr authentication.
Ruby
2
star
25

roda-symbolized_params

A Roda plugin which symbolizes request params
Ruby
2
star
26

shrine-example-melbourne

Shrine tutorial application for Coder Factory Academy
Ruby
1
star
27

refactoring_practice

Ruby
1
star
28

sequel-jsonapi_eager

DEPRECATED in favor of `tactical_eager_loading` Sequel plugin
Ruby
1
star
29

rodauth-guest

Guest accounts for Rodauth authentication framework
Ruby
1
star