Class: Congo::Types::Date

Inherits:
Date
  • Object
show all
Defined in:
lib/congo/types.rb

Class Method Summary collapse

Class Method Details

.apply(klass, scope, name) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/congo/types.rb', line 21

def self.apply(klass, scope, name)
  klass.class_eval <<-EOV
    
    def localized_#{name}
      if self.#{name}
        self.#{name}.strftime(I18n.t('congo.date.formats.default'))
      else
        nil
      end
    end
    
    def localized_#{name}=(value)
      self.#{name} = value
    end
    
  EOV
end

.from_mongo(value) ⇒ Object



44
45
46
# File 'lib/congo/types.rb', line 44

def self.from_mongo(value)
  value.to_date if value.present?
end

.increment_year(year) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/congo/types.rb', line 55

def self.increment_year(year)
  if year < 100
    year < 30 ? 2000 : 1900
  else
    0
  end
end

.parse_with_i18n(str) ⇒ Object



49
50
51
52
53
# File 'lib/congo/types.rb', line 49

def self.parse_with_i18n(str)
  date = ::Date._strptime(str, I18n.t('congo.date.formats.default')) || self._parse(str)
  date[:year] += self.increment_year(date[:year].to_i) if date[:year]
  date
end

.to_mongo(value) ⇒ Object



39
40
41
42
# File 'lib/congo/types.rb', line 39

def self.to_mongo(value)
  date = self.parse_with_i18n(value.to_s)
  Time.utc(date[:year], date[:mon], date[:mday])
end