Module: ConcertoHardware::TimeAccessible
- Included in:
- Player
- Defined in:
- lib/concerto_hardware/time_accessible.rb
Overview
Provides the time_accessor method, which is useful for creating a virtual parameter of type Time. The time is read and written as a standard Time-parasable string, which makes it possible to have form fields that reference the virtual attribute directly.
Note that it is possible to set as a Time object (to save instantiations). You cannot read the Time object directly, however.
Example
class MyModel
extend ConcertoHardware::TimeAccessible
time_accessor :party_time
def is_it_party_time?
Time.now > Time.new(self.party_time || "9001")
end
end
inst = MyModel.new
inst.party_time = "2011-11-11 11:11:11 -0000"
inst.party_time # => "2011-11-11 11:11:11 -0000"
inst.party_time = Time.now + 24*60*60 # the party is tommorrow
inst.party_time # => "2014-10-01 23:01:06 -0400"
Instance Method Summary collapse
Instance Method Details
#time_accessor(*syms) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/concerto_hardware/time_accessible.rb', line 29 def time_accessor(*syms) syms.each do |sym| self.class_eval <<-RUBY, __FILE__, __LINE__ + 1 def #{sym} if @#{sym}_timeobj.is_a? Time @#{sym}_timeobj else nil end end def #{sym}=(val) @#{sym}_timeobj = nil if val.is_a? Time @#{sym}_timeobj = val elsif val.is_a? String begin @#{sym}_timeobj = Time.parse(val) rescue ArgumentError => e # Example: empty string or not a time # Value gets nil, which validations will flag. end end end RUBY end end |