Chronicle

Chronicle groups collections of ruby objects into time periods. It uses Chronic to parse natural language date strings.

# Before:
[obj_1, obj_2, obj_3, obj_4, obj_5, obj_6, obj_7, obj_8, obj_9, obj_10]

# After:
{
  "just now": [obj_10, obj_9],
  "two hours ago": [obj_8, obj_7, obj_6],
  "yesterday": [obj_5, obj_4, obj_3],
  "6 months ago": [obj_2],
  "1 year ago": [obj_1]
}

Chronicle was created for Sniphr, a pet project of mine that makes bookmarking awesome. I wanted to reduce UI chatter by displaying a minimal timeline beside the content, instead of a timestamp under every element. Here's what it looks like:

"Chronicle on Sniphr"

Installation

# Put this in your Gemfile and smoke it.
# Ruby 1.9 or greater is required, because chronicle relies on ordered hashes.
gem 'chronicle'

Usage

# Fetch some objects (presumably ActiveRecord)
things = Thing.all

# Put them into buckets, using Chronicle's default eras
chronicle = Chronicle.new(things)

# To deviate from the default eras...
chronicle = Chronicle.new(things, :eras => ["5 minutes ago", "2 hours ago", "three weeks ago"])

# To sort based on an attribute other than :created_at
chronicle = Chronicle.new(things, :date_attr => :updated_at)

To see the default eras used by Chronicle, have a look at chronicle.rb.

License

MIT License. Do whatever you want.