TickTack
TickTack is a lean wrapper for Rubys native Date object to provide some
helpful methods for easy calendar creation. See the Example section below for
a better unterstanding!
Installation
Add this line to your application's Gemfile:
gem 'tick_tack'And then execute:
$ bundle
Or install it yourself as:
$ gem install tick_tack
Usage
Calendar
Create a new calendar object
calendar = TickTack::Calendar.newTo overwrite the current date just pass in another date.
This date is used as a fallback for the factory methods.
For example - if you call calendar.month(2014) without a month,
it will return the current month but in year 2014.
TickTack::Calendar.new(Date.new(2015, 5, 27))Methods
-
calendar.nowReturns the calendars baseDate -
calendar.year(2013)Returns theYearfor the given year number -
calendar.month(2013, 1)Returns theMonthfor the given year and month -
calendar.day(2013, 1, 12)Returns theDayfor the given year, month and day
Year
To get a year from the calendar call the year on the calendar object
year = calendar.year(2014)Methods
-
year.first_dayGet the first day of the year -
year.last_dayGet the last day of the year -
year.month(2)Get theMonthfebruary in this year -
year.monthsGet all months as an array for this year -
year.contains?(other)Check if other is part of this year.othercan be either aMonthor aDay -
year.nextGet the nextYear -
year.previousGet the previousYear
Month
To get a month, you can either call the creator method on the calendar or on the Year:
month = calendar.month(2014, 5)
# or
month = year.month(5)Methods
-
month.first_dayGet the first day of the month -
month.last_dayGet the last day of the month -
month.day(2)Get the requestedDayon this month -
month.weekdaysGets an array of all weeks from this month containing all days. Including overlapping days in other months. This is useful for rendering a month-view of an calendar. -
month.contains?(day)Check if aDayis part of this month -
month.nextGet the nextMonth -
month.previousGet the previousMonth
Day
A day can be created from a calendar or month:
day = calendar.day(2014, 5, 30)
# or
day = month.day(30)Methods
-
day.week_startReturns the firstDayof the week which the current day is in. To configure the first day of the week, see Localization section below. -
day.week_endReturns the lastDayof the week which the current day is in. -
day.weekdaysReturns an array ofDays for the week in which the current day is in. -
day.nextReturns the nextDay -
day.previousReturns the previousDay
Localization
Currently only dow (day of week) value can be overwritten which usually starts with 0. To set the day to monday, just pass in the locale config as second argument:
calendar = TickTack::Calendar.new(nil, {dow: 1})Example
TODO... maybe tomorrow :)
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/tomraithel/tick_tack.
License
The gem is available as open source under the terms of the MIT License.