I need to extend the Date class. Or more accurately, the
ActiveSupport::CoreExtensions::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!
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
You could write a special method and include it in
ActiveSupport::CoreExtensions::Calculations but it probably
won’t do you any good. Without digging around too much it appears the
methods in ActiveSupport::CoreExtensions::Calculations are being
included into the Date class. Adding your special method to
ActiveSupport::CoreExtensions::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
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
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
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.