Class: SemanticGap::DateTimeForm::Form
- Inherits:
-
ActiveForm
- Object
- ActiveForm
- SemanticGap::DateTimeForm::Form
- Defined in:
- lib/semanticgap_date_time_form/form.rb
Instance Attribute Summary collapse
-
#ampm ⇒ Object
Returns the value of attribute ampm.
-
#day ⇒ Object
Returns the value of attribute day.
-
#hour ⇒ Object
Returns the value of attribute hour.
-
#minute ⇒ Object
Returns the value of attribute minute.
-
#month ⇒ Object
Returns the value of attribute month.
-
#time_zone ⇒ Object
Returns the value of attribute time_zone.
-
#year ⇒ Object
Returns the value of attribute year.
Instance Method Summary collapse
- #allow_blank? ⇒ Boolean
- #blank? ⇒ Boolean
- #date=(d) ⇒ Object
-
#initialize(*args) ⇒ Form
constructor
A new instance of Form.
- #pm? ⇒ Boolean
- #time_zone_name ⇒ Object
- #time_zone_name=(str) ⇒ Object
- #to_datetime ⇒ Object
- #validate_date ⇒ Object
- #validate_time ⇒ Object
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() = args. date = args.first @time_zone = .fetch(:time_zone) { Time.zone || ActiveSupport::TimeZone.new('UTC') } @allow_blank = .fetch(:allow_blank) { false } self.date = date if date.respond_to?(:strftime) end |
Instance Attribute Details
#ampm ⇒ Object
Returns the value of attribute ampm.
27 28 29 |
# File 'lib/semanticgap_date_time_form/form.rb', line 27 def ampm @ampm end |
#day ⇒ Object
Returns the value of attribute day.
27 28 29 |
# File 'lib/semanticgap_date_time_form/form.rb', line 27 def day @day end |
#hour ⇒ Object
Returns the value of attribute hour.
27 28 29 |
# File 'lib/semanticgap_date_time_form/form.rb', line 27 def hour @hour end |
#minute ⇒ Object
Returns the value of attribute minute.
27 28 29 |
# File 'lib/semanticgap_date_time_form/form.rb', line 27 def minute @minute end |
#month ⇒ Object
Returns the value of attribute month.
27 28 29 |
# File 'lib/semanticgap_date_time_form/form.rb', line 27 def month @month end |
#time_zone ⇒ Object
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 |
#year ⇒ Object
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
53 54 55 |
# File 'lib/semanticgap_date_time_form/form.rb', line 53 def allow_blank? @allow_blank end |
#blank? ⇒ 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
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_name ⇒ Object
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_datetime ⇒ Object
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_date ⇒ Object
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_time ⇒ Object
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 |