Having trouble extending Date

Hi all,

I need to extend the Date class. Or more accurately, the
ActiveSupport::CoreExtensions::date::Calculations module. Everything
works
fine when I include the new method in that module itself. But when I
try to
move the extension out of that module and into my application space
(e.g.,
in the lib directory), I get a method not found error. I’m sure I’m
missing
something basic, but could sure use some help identifying what that
thing
is. I’ve tried various locations thinking that maybe I was just putting
it
in the wrong place, but nothing’s worked. Any help’s much appreciated!

Thanks,
Bill

----------- calculations.rb module extension ---------------

module ActiveSupport #:nodoc:
module CoreExtensions #:nodoc:
module Date #:nodoc:
# Enables the use of time calculations within Time itself
module Calculations

     def adjust_start_for_weekends
        if self.strftime('%a') == 'Fri'
           return self.advance(:days => 2)
        elsif self.strftime('%a') == 'Sat'
           return self.advance(:days => 1)
        else
           return self
        end
     end

  end
end

end
end

Maybe require it in environment.rb?

require ‘calcualtions’

Alright… sorry for that first stupid answer.

You could write a special method and include it in
ActiveSupport::CoreExtensions::date::Calculations but it probably
won’t do you any good. Without digging around too much it appears the
methods in ActiveSupport::CoreExtensions::date::Calculations are being
included into the Date class. Adding your special method to
ActiveSupport::CoreExtensions::date::Calculations probably won’t get
it included in the Date class.

The solution? Include your method directly into the Date class.

#my_calculations.rb
module MyCalculations
def adjust_start_for_weekends
if self.strftime(’%a’) == ‘Fri’
return self.advance(:days => 2)
elsif self.strftime(’%a’) == ‘Sat’
return self.advance(:days => 1)
else
return self
end
end
end

Date.send :include, MyCalculations

and then require it in environment.rb

require ‘my_calculations’

Hi Julian,

julian wrote:

Alright… sorry for that first stupid answer.

Not stupid at all. It got me thinking. As soon as I put a ‘require’ in
application.rb, everything works as expected. Despite having ‘said out
loud’ that I needed to extend the calculations module, my brain seems to
have held onto the notion that I was exending the Date class. Rails
picks
up Class extentions automagically. We have to tell it to pick up
modules.
Duh ;-p

Thanks for helping me clear the ‘brain fart’ :wink:

Best regards,
Bill

Hi Matthew,

Matthew R. Jacobs wrote:

just a quick question;
isn’t “date.wday == 5” a cleaner way of saying is it friday?

Depends on what you mean by ‘cleaner’ I guess. I try to write code
that’s
understandable to anybody, not just folks who know Ruby well enough, in
this
case, to know what day of the week its calendar starts on. As far as
that
goes, I don’t trust my own memory well enough to use the above without
at
least testing it with strftime to make sure wday == 5 is Friday :wink:

Best regards,
Bill

On 20 Jun 2008, at 13:26, Bill W. wrote:

Hi Matthew,

Matthew R. Jacobs wrote:

just a quick question;
isn’t “date.wday == 5” a cleaner way of saying is it friday?

Depends on what you mean by ‘cleaner’ I guess. I try to write code
that’s

Until you run on a localised system where strftime returns Freitag or
something :slight_smile:

Fred

Hi Fred,

Frederick C. wrote:

Until you run on a localised system where strftime returns
Freitag or something :slight_smile:

Very good point. The nature of “the simplest thing that could possibly
work” is context-sensitive :wink:

Best regards,
Bill

Bill W. wrote:

----------- calculations.rb module extension ---------------

module ActiveSupport #:nodoc:
module CoreExtensions #:nodoc:
module Date #:nodoc:
# Enables the use of time calculations within Time itself
module Calculations

     def adjust_start_for_weekends
        if self.strftime('%a') == 'Fri'
           return self.advance(:days => 2)
        elsif self.strftime('%a') == 'Sat'
           return self.advance(:days => 1)
        else
           return self
        end
     end

  end
end

end
end

just a quick question;
isn’t “date.wday == 5” a cleaner way of saying is it friday?

don’t really like strftime.

although
Date::DAYNAMES[day.wday] == “Friday”

is probably worse