Module: DateFormatter

Defined in:
lib/date_formatter.rb

Constant Summary collapse

FORMATTED_METHOD =
/^formatted_(.+)$/

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object

generic date formatter called like formatted_method_name if your class responds to method_name, call format_date with it



19
20
21
22
23
24
25
26
# File 'lib/date_formatter.rb', line 19

def method_missing method_name, *args
  if name = valid_formatted_method_name?(method_name)
    value = send name
    return format_date value
  else
    super
  end
end

Instance Method Details

#format_date(value) ⇒ Object

call strftime on the given date, with this class’s strftime_date_string



12
13
14
# File 'lib/date_formatter.rb', line 12

def format_date value
  value.respond_to?(:strftime) ? value.strftime(strftime_date_string) : value
end

#respond_to?(method_name, some_random_argument = nil) ⇒ Boolean

ensure that respond_to? returns true if passed a valid formatted_method_name

Returns:

  • (Boolean)


6
7
8
9
# File 'lib/date_formatter.rb', line 6

def respond_to? method_name, some_random_argument = nil
  return true if valid_formatted_method_name? method_name
  super
end

#strftime_date_stringObject

set a default value for strftime_date_string override this method in your class to customize the date format



30
31
32
# File 'lib/date_formatter.rb', line 30

def strftime_date_string
  '%m/%d/%Y'
end