Class: SemanticGap::DateTimeForm::Form

Inherits:
ActiveForm
  • Object
show all
Defined in:
lib/semanticgap_date_time_form/form.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Form

Returns a new instance of Form.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/semanticgap_date_time_form/form.rb', line 6

def initialize(*args)
  super()

  options = args.extract_options!
  date = args.first

  @time_zone = options.fetch(:time_zone) { Time.zone || ActiveSupport::TimeZone.new('UTC') }
  @allow_blank = options.fetch(:allow_blank) { false }

  self.date = date if date.respond_to?(:strftime)
end

Instance Attribute Details

#ampmObject

Returns the value of attribute ampm.



27
28
29
# File 'lib/semanticgap_date_time_form/form.rb', line 27

def ampm
  @ampm
end

#dayObject

Returns the value of attribute day.



27
28
29
# File 'lib/semanticgap_date_time_form/form.rb', line 27

def day
  @day
end

#hourObject

Returns the value of attribute hour.



27
28
29
# File 'lib/semanticgap_date_time_form/form.rb', line 27

def hour
  @hour
end

#minuteObject

Returns the value of attribute minute.



27
28
29
# File 'lib/semanticgap_date_time_form/form.rb', line 27

def minute
  @minute
end

#monthObject

Returns the value of attribute month.



27
28
29
# File 'lib/semanticgap_date_time_form/form.rb', line 27

def month
  @month
end

#time_zoneObject

Returns the value of attribute time_zone.



27
28
29
# File 'lib/semanticgap_date_time_form/form.rb', line 27

def time_zone
  @time_zone
end

#yearObject

Returns the value of attribute year.



27
28
29
# File 'lib/semanticgap_date_time_form/form.rb', line 27

def year
  @year
end

Instance Method Details

#allow_blank?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/semanticgap_date_time_form/form.rb', line 53

def allow_blank?
  @allow_blank
end

#blank?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/semanticgap_date_time_form/form.rb', line 85

def blank?
  time_blank? && date_blank?
end

#date=(d) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/semanticgap_date_time_form/form.rb', line 18

def date=(d)
  d = time_zone.utc_to_local(d.to_datetime.utc)

  self.attributes = {
    :year => d.year, :month => d.month, :day => d.day,
    :hour => d.strftime("%I"), :minute => d.strftime("%M"), :ampm => d.strftime("%p")
  }
end

#pm?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/semanticgap_date_time_form/form.rb', line 81

def pm?
  ampm[0, 1].downcase == 'p' unless ampm.blank?
end

#time_zone_nameObject



40
41
42
# File 'lib/semanticgap_date_time_form/form.rb', line 40

def time_zone_name
  time_zone.name
end

#time_zone_name=(str) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/semanticgap_date_time_form/form.rb', line 44

def time_zone_name=(str)
  tz = ActiveSupport::TimeZone.new(str)

  if tz
    self.time_zone = tz
    time_zone_name
  end
end

#to_datetimeObject



89
90
91
92
93
# File 'lib/semanticgap_date_time_form/form.rb', line 89

def to_datetime
  return nil if (allow_blank? && blank?) || !valid?

  time_zone.local_to_utc(build_datetime)
end

#validate_dateObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/semanticgap_date_time_form/form.rb', line 65

def validate_date
  return if allow_blank? && date_blank?

  errors.add(:year, "#{year} is not a valid year") unless year.to_s =~ /\d+/
  errors.add(:month, "#{month} is not a valid month") unless (1..12).include?(month.to_i)
  errors.add(:day, "#{day} is not a valid day") unless day.to_s =~ /\d+/ && (1..31).include?(day.to_i)

  if errors[:month].blank? && errors[:month].blank?
    begin
      build_datetime
    rescue ArgumentError
      errors.add(:day, "is invalid")
    end
  end
end

#validate_timeObject



57
58
59
60
61
62
63
# File 'lib/semanticgap_date_time_form/form.rb', line 57

def validate_time
  return if allow_blank? && time_blank?

  errors.add(:hour, "is invalid") unless hour.to_s =~ /\d+/ && (0..23).include?(hour.to_i)
  errors.add(:minute, "is invalid") unless minute.to_s =~ /\d+/ && (0..59).include?(minute.to_i)
  errors.add(:ampm, "must be AM or PM") unless %W(AM PM am pm).include?(ampm)
end