Sha256: 75a21928be08f561ae83cba19fdb00268688322cf0df7860d17f8046ccdcac38

Contents?: true

Size: 960 Bytes

Versions: 5

Compression:

Stored size: 960 Bytes

Contents

class TimeField < Field
  def default_input_type
    :datetime
  end
  
  def before_create(record)
    return unless name == 'created_at' || name == 'updated_at'
    record.set(name, Time.now.utc)
  end

  def before_update(record)
    return unless name == 'updated_at'
    record.set(name, Time.now.utc)
  end
  
  def typecast(value, record)
    value.nil? ? nil : Time.at(value)
  end
  
  def untypecast(value, record)
    value.blank? ? nil : Time.at(value.to_i).utc
  end
  
  def from_json(value, record)
    return nil unless value.present? && (value.is_a?(String) || value.is_a?(Hash))
    if value.is_a?(Hash)
      return nil unless ['year', 'month', 'day', 'hour', 'min'].all? {|field| value.key?(field) && !value[field].blank?}
      sec = value['sec'] || 0
      Time.new(value['year'], value['month'], value['day'], value['hour'], value['min'], sec).utc
    else
      Time.parse(value).utc
    end
  end
end

Field::TYPES['time'] = TimeField

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
yodel-0.0.7 lib/yodel/models/core/fields/time_field.rb
yodel-0.0.4 lib/yodel/models/core/fields/time_field.rb
yodel-0.0.3 lib/yodel/models/core/fields/time_field.rb
yodel-0.0.2 lib/yodel/models/core/fields/time_field.rb
yodel-0.0.1 lib/yodel/models/core/fields/time_field.rb