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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Maybe require it in environment.rb? require ''calcualtions'' On Jun 19, 3:16 pm, "Bill Walton" <bill.wal...-xwVYE8SWAR3R7s880joybQ@public.gmane.org> wrote:> 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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''
On Jun 19, 3:20 pm, julian
<thefool...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Maybe require it in environment.rb?
>
> require ''calcualtions''
>
> On Jun 19, 3:16 pm, "Bill Walton"
<bill.wal...-xwVYE8SWAR3R7s880joybQ@public.gmane.org> wrote:
>
> > 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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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'' ;-) Best regards, Bill --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Bill Walton 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 > endjust 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi Matthew, Matthew Rudy 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 ;-) Best regards, Bill --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 20 Jun 2008, at 13:26, Bill Walton wrote:> > Hi Matthew, > > Matthew Rudy 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''sUntil you run on a localised system where strftime returns Freitag or something :-) Fred> > 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 ;-) > > Best regards, > Bill > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi Fred, Frederick Cheung wrote:> > Until you run on a localised system where strftime returns > Freitag or something :-)Very good point. The nature of "the simplest thing that could possibly work" is context-sensitive ;-) Best regards, Bill --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---