Module: StringDateAccessors

Defined in:
lib/string_date_accessors.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.date_formatObject



9
10
11
# File 'lib/string_date_accessors.rb', line 9

def self.date_format
  @date_format
end

.date_format=(string) ⇒ Object



5
6
7
# File 'lib/string_date_accessors.rb', line 5

def self.date_format=(string)
  @date_format = string
end

.datetime_formatObject



17
18
19
# File 'lib/string_date_accessors.rb', line 17

def self.datetime_format
  @datetime_format
end

.datetime_format=(string) ⇒ Object



13
14
15
# File 'lib/string_date_accessors.rb', line 13

def self.datetime_format=(string)
  @datetime_format = string
end

.formatted(input) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/string_date_accessors.rb', line 25

def self.formatted(input)
  date = Date.strptime(input, date_format)
  zone = date.to_time.zone
  datetime_utc = DateTime.strptime("#{input} #{zone}", "#{datetime_format} %Z").to_time.utc
  Time.utc(datetime_utc.year, 
           datetime_utc.month, 
           datetime_utc.day, 
           datetime_utc.hour,
           datetime_utc.min,
           datetime_utc.sec)
rescue ArgumentError, NoMethodError => e
  date ? date : input
end

.included(klass) ⇒ Object



21
22
23
# File 'lib/string_date_accessors.rb', line 21

def self.included(klass)
  klass.extend(ClassMethods)
end

Instance Method Details

#invalid_date_accessorsObject



39
40
41
# File 'lib/string_date_accessors.rb', line 39

def invalid_date_accessors
  string_date_accessors_set.reject {|attribute| send(attribute).respond_to?(:strftime)}
end

#string_date_accessors_setObject



43
44
45
# File 'lib/string_date_accessors.rb', line 43

def string_date_accessors_set
  @string_date_accessors_set ||= []
end