Module: SmarterDates
- Defined in:
- lib/smarter_dates/parser.rb,
lib/smarter_dates/version.rb,
lib/generators/smarter_dates/install_generator.rb
Defined Under Namespace
Classes: InstallGenerator
Constant Summary collapse
- VERSION =
'0.2.9'
Class Method Summary collapse
-
.included(klass) ⇒ Object
:nodoc:.
Class Method Details
.included(klass) ⇒ Object
:nodoc:
5 6 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 |
# File 'lib/smarter_dates/parser.rb', line 5 def self.included(klass) # :nodoc: attr_accessor :dt_attributes @dt_attributes = [] db_migrated = true if defined?(Rails) begin @dt_attributes.concat(klass.column_names.select { |k| k.match(/_(?:at|on|dt|d)$/) }) Rails.logger.debug(RuntimeError, "unused include - #{klass.class.to_s} does not have any attributes ending in _at, _on, _dt, or _d") if db_migrated && @dt_attributes.empty? rescue ActiveRecord::StatementInvalid => _ rescue => err Rails.logger.debug(err.inspect) end else @dt_attributes.concat(klass.instance_methods.select { |k| k.match(/_(?:at|on|dt|d)$/) }) end # :call-seq: # _on(string) # _at(string) # _dt(string) # _d(string) # # Any attribute ending in _at, _on, _dt, or _d are parsed by Chronic.parse # (for flexibility). Values are passed as is to Chronic.parse() # # == Arguments # <tt>string</tt>:: A string @dt_attributes.each do |k| parse = Proc.new do |val| begin dt = Chronic.parse(val.to_s) rescue dt = DateTime.parse(val.to_s) rescue dt = Date.parse(val) rescue dt = val end if defined?(Rails) if dt && k.match(/_(?:on|d)$/) self[k] = dt.to_date elsif dt self[k] = dt.to_datetime else self[k] = dt end else instance_variable_set(:"@#{k}", dt) end end klass.send(:define_method, "#{k}=".to_sym, &parse) end end |