Module: DatetimeSplitter

Defined in:
lib/datetime_splitter.rb,
lib/datetime_splitter/version.rb,
lib/datetime_splitter/split_datetime.rb

Constant Summary collapse

VERSION =
"0.0.1"

Instance Method Summary collapse

Instance Method Details

#split_datetime(field, options = {}) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/datetime_splitter/split_datetime.rb', line 2

def split_datetime(field, options={})

	define_method("#{field}_date=") do |date|
		date = Date.parse(date.to_s)
		new_date =  (self.send("#{field}") || DateTime.new).change(year: date.year, month: date.month, day: date.day)
		self.send("#{field}=",new_date)
	end
	
	define_method("#{field}_date") do 
		date = self.send(field).try :to_date
     		options[:date_format] ? date.strftime(options[:date_format]) : date
	end

	define_method("#{field}_time=") do |time|
		time = DateTime.parse(time.to_s)
		new_time =  (self.send("#{field}") || DateTime.new).change(hour: time.hour, min: time.min, sec: time.sec)
		self.send("#{field}=",new_time)
	end

	define_method("#{field}_time") do 
		time = self.send(field)
     		options[:time_format] ? time.strftime(options[:time_format]) : time
	end

end