diff --git a/README.md b/README.md index f6622de..7ec3729 100644 --- a/README.md +++ b/README.md @@ -10,20 +10,54 @@ This gem extends the abilities of Ruby's built-in Time class by building on top ## Basic Usage - require "time/ext" - Time.now.round(:week) +### Pure Ruby + + require "time/ext" # or "time_ext" + Time.utc(2010, 6, 19).round(:month) #=> Thu Jul 01 00:00:00 UTC 2010 #=> Beginning of this week, or next week depending on which date is closest + Time.now.each_hour.until(6.hours.from_now) { |t| puts t.to_s } + #=> Prints the time at one hour interals from now till 6 hours from now + +### Rails 2.x + + config.gem "time_ext" + +### Rails 3.x + + gem "time_ext" ## Documentation -A decently informative documentation is available [here][docs] on [rdoc.info][]. +Complete class and method documentation is available [here][docs] on [rdoc.info][]. -The main reason I created this gem was for the `round`, `floor`, and `ceil` methods. Each of them takes a unit argument, which can be one of the following: `:sec`, `:min`, `:hour`, `:day`, `:week`, `:month`, `:quarter`, and `:year`. +### Calculations + +ActiveSupport includes some handy `#beginning_of_*` and `#end_of_*` methods among others for year, month, week, and day. TimeExt adds the same methods for quarter (3 months), hour, minute, and second. Additionally it also adds a set of familiar math methods, `#floor`, `#ceil`, and `#round`. Each of them takes a unit argument (day, month, etc.), and goes about it's operation to that unit type. + +### Iterations + +You can easily iterate over specific units of time with the `#each`, `#beginning_of_each`, `#map_each`, and `#map_beginning_of_each` methods. Each method takes a unit input, just like the `#floor` method for example. But the iteration methods also have dynamic unit methods, like so: + + time = Time.local(2010, 07, 10) + time.map_each(:hour).until(time + 6.hours) { |t| t.hour } #=> [1, 2, 3, 4, 5, 6] + time.map_each_hour.until(time + 6.hours) { |t| t.hour } #=> [1, 2, 3, 4, 5, 6] + +The `#until` method defines when to stop the iteration. You can also use the `#from` method, which then iterates from the time passed into `#from` till the time of the object you're calling each on. If don't specify a end/start time, it'll assume you want all units within it's scope. Or in less gibberish, calling `#each_hour` will iterate every hour for a whole day, or 24 hours. If you're iterating by the minute, it'll stop after an hour. + +Here's a couple of more examples: + + time.map_each_hour.until(time + 6.hours) { |t| t.hour } #=> [1, 2, 3, 4, 5, 6] + time.until(time + 6.hours).map_each_hour { |t| t.hour } #=> [1, 2, 3, 4, 5, 6] + (time + 6.hours).map_each_hour.from(time) { |t| t.hour } #=> [1, 2, 3, 4, 5, 6] + (time + 6.hours).from(time).map_each_hour { |t| t.hour } #=> [1, 2, 3, 4, 5, 6] + +I recommend you read the complete class [documentation][docs] for the Time object to see all the new methods available. ## To-Do +* Some cleaner code. * Improve ReadMe file. * Improve documentation.