Module: Chronological::AbsoluteTimeframe::ClassMethods
- Defined in:
- lib/chronological/absolute_timeframe.rb
Instance Method Summary collapse
-
#absolute_timeframe(options = {}) ⇒ Object
TODO: Needs to be able to add a validation option which can do the typical timeliness validation such as ended_at should be after started_at and that both should validate timeliness.
Instance Method Details
#absolute_timeframe(options = {}) ⇒ Object
TODO: Needs to be able to add a validation option which can do the typical timeliness validation such as ended_at should be after started_at and that both should validate timeliness
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/chronological/absolute_timeframe.rb', line 7 def absolute_timeframe( = {}) start_time_field = [:start_utc] || [:start] start_date_field = start_time_field.to_s.gsub(/_at/, '_on') end_time_field = [:end_utc] || [:end] end_date_field = end_time_field.to_s.gsub(/_at/, '_on') time_zone = [:time_zone] start_time_field_is_utc = .has_key? :start_utc end_time_field_is_utc = .has_key? :end_utc start_time_field_utc_suffix = start_time_field_is_utc ? '_utc' : '' end_time_field_utc_suffix = end_time_field_is_utc ? '_utc' : '' define_method(:scheduled?) do optional_time_zone = ![:time_zone].nil? ? send(time_zone) : true send(start_time_field).present? && send(end_time_field).present? && optional_time_zone end define_method(:partially_scheduled?) do optional_time_zone = ![:time_zone].nil? ? send(time_zone) : false send(start_time_field).present? || send(end_time_field).present? || optional_time_zone end ### # Scopes # self.class.send(:define_method, :by_date) do order "#{table_name}.#{start_time_field} ASC, #{table_name}.#{end_time_field} ASC" end self.class.send(:define_method, :by_date_reversed) do order "#{table_name}.#{start_time_field} DESC, #{table_name}.#{end_time_field} DESC" end self.class.send(:define_method, :expired) do where("#{end_time_field} <= :now", :now => Time.now.utc) end self.class.send(:define_method, :current) do where("#{end_time_field} > :now", :now => Time.now.utc) end self.class.send(:define_method, :in_progress) do where("#{start_time_field} <= :now AND #{end_time_field} > :now", :now => Time.now.utc) end self.class.send(:define_method, :started) do where("#{start_time_field} <= :now", :now => Time.now.utc) end self.class.send(:define_method, :in_progress?) do in_progress.any? end ### # Aliases # # Aliasing date methods to make code more readable instance_eval do alias active? in_progress? alias active in_progress end class_eval do alias_attribute :"starts_at#{start_time_field_utc_suffix}", start_time_field.to_sym alias_attribute :"starting_at#{start_time_field_utc_suffix}", start_time_field.to_sym alias_attribute :"ends_at#{start_time_field_utc_suffix}", end_time_field.to_sym alias_attribute :"ending_at#{start_time_field_utc_suffix}", end_time_field.to_sym end base_timeframe start_date_field: start_date_field, start_time_field: start_time_field, end_date_field: end_date_field, end_time_field: end_time_field private define_method(:has_absolute_timeframe?) do send(start_time_field).present? && send(end_time_field).present? end define_method(:duration_in_seconds) do (send(end_time_field) - send(start_time_field)) end end |