[Catalyst] Date::Calc and Date::Calendar with Catalyst?
Dave Morriss
m.paradoxa at gmail.com
Sat Feb 24 23:22:06 GMT 2007
On 23/02/07, Jonathan Rockway <jon at jrock.us> wrote:
> Dave Morriss wrote:
> > Question 1: Will I need to build my own plugin to achieve what I want,
> > or is there another way?
>
> It's unlikely that you'll want to use a plugin. Plugins should be
> reserved for things that modify Catalyst's usual request cycle.
> Pagecache, Static::Simple, and ConfigLoader are good examples of
> plugins; Prototype is a bad example.
I thought, based on the way C::P::DateTime is put together, that
plugins are also used as a way of making non-Catalyst modules
available in the Catalyst namespace. From the way I interpret the
documentation you referred to I reckon I _could_ do something like
that. You seem to be recommending against it.
> I'm not exactly sure what you're trying to do, but it sounds like you
> want a regular Perl module:
>
> package MyOrg::Calendar;
> use base 'Exporter';
> my @EXPORT = qw(workdays_between_dates);
>
> sub workdays_between_dates {
> my ($from, $to) = @_;
> return 42; # modify to taste
> }
Yes, I'm doing something like this already.
In brief, I need to do some basic stuff such as:
- define the organisation's non-workday calendar, which
Date::Calendar can do
- manage a holiday allocation per person (different people have
different rates, and can
earn more by working overtime)
- record "bookings" which have a start date & am/pm, end date &
am/pm, and a duration
- compute duration such that the guy who takes the Friday before a
Monday holiday and
the Tuesday after is billed for only 2 days; decrement the allocation
- etc
I'm using Date::Calc date objects, delta times, and related stuff for
all of this, so I'd like constant data and
Date::Calc/Date::Calendar/DateTime methods as well as my own to be
available in several controllers.
> Then in Catalyst, you'd do something like:
>
> package MyOrg::Vacations::Controller::GiveMeADayOff;
> use base 'Catalyst::Controller';
> use MyOrg::Calendar;
>
> sub get_a_day_off : Path {
> my ($self, $c, $date) = @_;
>
> $c->stash(template => 'day_off.tt');
>
> my $last_day_off =
> $c->model('Vacataions')->get_last_vacation_day_for($c->user);
>
> if(workdays_between_dates($last_day_off, $date) > 5){
> $c->stash(error =>
> "Only one vacation day per week is allowed,
> slacker!");
> }
> else {
> $c->model('Vacations')->add_vacation($date, $c->user);
> }
>
> ... # you get the idea
> }
>
> Hope this helps a bit.
Yes, thanks. It's broadly what I am doing now. Some of the basic
functionality is working using this technique.
> Basically you should read up on writing Perl
> modules; I think that's what you really want here. man perlmod should help.
Ow! I didn't realise I was so transparent. Yes, I'm an old-fashioned
programmer from the pre-OO days :-)
> > Question 2: Is there a plugin writer's HOWTO?
>
> There is; at:
>
> http://search.cpan.org/~jrockway/Catalyst-Manual-5.700501/lib/Catalyst/Manual/WritingPlugins.pod
>
> But I don't think you need a plugin.
OK, thanks.
--
Dave Morriss
More information about the Catalyst
mailing list