Sha256: 6faecba8c6a0c860bd68f1f1d14cb449bb91fa26da03494698167f2ec8410c64

Contents?: true

Size: 1.3 KB

Versions: 3

Compression:

Stored size: 1.3 KB

Contents

module Clevic

#includers must provide meta and display
module GenericFormat
  # 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.
  def is_date_time?( value )
    if value.nil?
      false
    else
      @is_date_time ||=
      if display.nil?
        [:time, :date, :datetime, :timestamp].include?( meta.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
  
  # apply format to value. Use strftime for date_time types, or % for everything else.
  # If format is a proc, pass value to it.
  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 "format: #{format.inspect}"
      puts "value.class: #{value.class.inspect}"
      puts "value: #{value.inspect}"
      puts e.message
      puts e.backtrace
      nil
    end
  end
  
end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
clevic-0.13.0.b3 lib/clevic/generic_format.rb
clevic-0.13.0.b2 lib/clevic/generic_format.rb
clevic-0.13.0.b1 lib/clevic/generic_format.rb