Class: ActiveRecord::Bixformer::Attribute::Time

Inherits:
Base
  • Object
show all
Defined in:
lib/activerecord-bixformer/attribute/time.rb

Instance Attribute Summary

Attributes inherited from Base

#model, #name, #options

Instance Method Summary collapse

Methods inherited from Base

#initialize, #should_be_included

Constructor Details

This class inherits a constructor from ActiveRecord::Bixformer::Attribute::Base

Instance Method Details

#export(record) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/activerecord-bixformer/attribute/time.rb', line 5

def export(record)
  record_attribute_value = record_attribute_value(record)

  return nil unless record_attribute_value

  record_attribute_value.to_s(option_format)
end

#import(value) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/activerecord-bixformer/attribute/time.rb', line 13

def import(value)
  return nil if value.blank?

  result = begin
             ::Time.parse(value)
           rescue
             format_string = ::Time::DATE_FORMATS[option_format]

             ::Time.strptime(value, format_string) rescue nil if format_string
           end

  return result if result

  raise ::ActiveRecord::Bixformer::AttributeError.new(self, value) if @options[:raise]
end