Module: Clevic::GenericFormat
- Included in:
- Field
- Defined in:
- lib/clevic/generic_format.rb
Overview
Format values for display / edit. Essentially a common interface for % for Strings and strftime for Dates and Times.
includers must provide meta and display
Instance Method Summary collapse
-
#do_generic_format(format, value) ⇒ Object
apply format to value.
-
#is_date_time?(value) ⇒ Boolean
Return true if the field is a date, datetime, time or timestamp.
Instance Method Details
#do_generic_format(format, value) ⇒ Object
apply format to value. Use strftime for date_time types, or % for everything else. If format is a proc, pass value to it.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/clevic/generic_format.rb', line 28 def do_generic_format( format, value ) begin unless format.nil? if format.is_a? Proc format.call( value ) else if is_date_time?( value ) value.strftime( format ) else format % value end end else value end rescue Exception => e puts "self: #{self.inspect}" puts "format: #{format.inspect}" puts "value.class: #{value.class.inspect}" puts "value: #{value.inspect}" puts e. puts e.backtrace nil end end |
#is_date_time?(value) ⇒ Boolean
Return true if the field is a date, datetime, time or timestamp. If display is nil, the value is calculated, so we need to check the value. Otherwise use the field metadata. Cache the result for the first non-nil value.
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/clevic/generic_format.rb', line 12 def is_date_time?( value ) if value.nil? false else @is_date_time ||= if display.nil? [:time, :date, :datetime, :timestamp].include?( .type ) else # it's a virtual field, so we need to use the value value.is_a?( Date ) || value.is_a?( Time ) end end end |