DateTime attribute for Ruby!
Splits access of DateTime attribute(s) into seperate Date, Time and TimeZone attributes. Compatible with ActiveRecord as well as with Rails.
Install
gem install date_time_attribute
Enjoy
require 'date_time_attribute'
class Task
include DateTimeAttribute
date_time_attribute :due_at
end
task = Task.new
task.due_at
task.due_at_date = '2001-02-03'
task.due_at_date
task.due_at_time
task.due_at
task.due_at_time = '10:00pm'
task.due_at_time
task.due_at_date
task.due_at
task.due_at_time_zone = 'Moscow'
task.due_at
task.due_at_time_zone = 'London'
task.due_at
You can also use it with already existing/initialized attributes
class Task
include DateTimeAttribute
attr_accessor :due_at
date_time_attribute :due_at
def initialize
self.due_at = Time.zone.now.tomorrow
end
end
Using time zones
Default time zone can be applied to an attribute:
class Task
include DateTimeAttribute
date_time_attribute :due_at, time_zone: 'Moscow'
end
class Task
include DateTimeAttribute
date_time_attribute :due_at, time_zone: Proc.new { 'Moscow' }
end
class Task
include DateTimeAttribute
date_time_attribute :due_at, time_zone: :my_time_zone
def my_time_zone
self.in_da_moscow? ? 'Moscow' : 'Birobidgan'
end
end
You can also explicitly set timezone:
Time.zone = 'Krasnoyarsk'
task.due_at
task.due_at_time = '02:00am'
task.due_at
task.due_at_time_zone = 'Moscow'
task.due_at
Rails users
You don't need to set up anything, it just works out of the box through railtie
class MyModel < ActiveRecord::Base
date_time_attribute :created_at
end
ActiveRecord users (no Rails)
In order to include globally in your models:
ActiveRecord::Base.send(:include, DateTimeAttribute)
Then add attributes into your models:
class MyModel < ActiveRecord::Base
date_time_attribute :created_at, :updated_at
end
Using Chronic gem
DateTimeAttribute.parser = Chronic
task.due_at_date = 'next saturday'
task.due_at_time = '10:00pm'
Advanced usage
my_date_time = DateTimeAttribute::Container.new
my_date_time.date_time
my_date_time.date = '2001-02-03'
my_date_time.date_time
my_date_time.time = '10:00pm'
my_date_time.date_time
my_date_time = DateTimeAttribute::Container.new(Time.zone.now)
my_date_time.date_time