Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Released version 0.14

  • Loading branch information...
commit 0b4a30ba13b90c9889d1483fb4c7458afa54271d 2 parents 649f8cd + 233dc5a
@ingydotnet authored
Showing with 55 additions and 23 deletions.
  1. +2 −1  Changes
  2. +17 −6 README
  3. +10 −10 lib/Mo.pm
  4. +17 −5 lib/Mo.pod
  5. +9 −1 t/test.t
View
3  Changes
@@ -1,8 +1,9 @@
---
version: 0.14
-date:
+date: Sat Sep 10 22:25:40 CEST 2011
changes:
- Make default called on exists rather than undef
+- Add builder. Very cheap to do so.
---
version: 0.13
date: Fri Sep 9 19:15:05 CEST 2011
View
23 README
@@ -21,7 +21,7 @@ SYNOPSIS
}
DESCRIPTION
- use Mo. Mo is less.
+ use Mo. Mo is less. Much less.
Moose led to Mouse led to Moo led to Mo. M is nothing. Mo is more. Not
much.
@@ -35,6 +35,10 @@ FEATURES
Mo provides a "new" object constructor. It will call the "BUILD"
method after creation if it "can".
+ NOTE: Unlike other Moose modules, BUILD is not called in a chained
+ fashion. You would need to call "SUPER::BUILD" yourself, if you
+ needed that.
+
"extends"
Mo exports the "extends" keyword, to name your parent class. "Mo"
itself is your default parent class, of course.
@@ -52,18 +56,25 @@ FEATURES
supports:
default
- Should be a code reference. The object instance is passed in,
- and it should return the default value for this attribute.
- Default is always called lazily.
+ Must be a code reference. The object instance is passed in, and
+ it should return the default value for this attribute. "default"
+ is always called lazily. ie It is called when you try to get the
+ value and it does not(exists()).
+
+ builder
+ Must be a method name. This method should return the default
+ value for this attribute. "builder" is always called lazily.
Any other arguments are ignored. This lets you switch from Moo to Mo
and back, without having to change all your accessors.
"strict" and "warnings"
- Mo turns on "use strict" and "use warnings".
+ Mo turns on "use strict" and "use warnings" for you.
Embeddable
- Mo is tiny. You can easily inline it in your code, if you want to.
+ Mo is tiny. It is currently 10 lines, <= 80 chars.
+
+ You could easily inline it in your code, if you wanted to.
AUTHORS
Ingy döt Net <ingy@cpan.org>
View
20 lib/Mo.pm
@@ -1,10 +1,10 @@
-package Mo; require strict; require warnings;
-our $VERSION = '0.13';
-sub import {
- strict->import; warnings->import; my $p = caller; @{$p.'::ISA'} = $_[0];
- *{$p.'::extends'} = sub {@{(caller).'::ISA'} = $_[0]};
- *{$p.'::has'} = sub { my ($n, %a) = @_; *{(caller)."::$n"} = $a{default}
- ? sub { $#_ ? ($_[0]{$n} = $_[1]) : (exists $_[0]{$n})
- ? $_[0]{$n} : ($_[0]{$n} = $a{default}($_[0])) }
- : sub { $#_ ? $_[0]{$n} = $_[1] : $_[0]{$n} } };
-} sub new { my $s = bless {@_[1..$#_]},$_[0];$s->can('BUILD') && $s->BUILD;$s}
+package Mo; require strict; require warnings; $Mo::VERSION = '0.14';
+sub import {strict->import;warnings->import;my $p=caller;@{$p.'::ISA'}=$_[0];
+ *{$p.'::extends'} = sub {@{(caller).'::ISA'} = $_[0]}; *{$p.'::has'} =
+ sub { my ($n, %a) = @_; my($d,$b)=@a{qw(default builder)};*{(caller)."::$n"} =
+ $d ? sub { $#_ ? ($_[0]{$n} = $_[1]) : (exists $_[0]{$n})
+ ? $_[0]{$n} : ($_[0]{$n} = $d->($_[0])) } :
+ $b ? sub { $#_ ? ($_[0]{$n} = $_[1]) : (exists $_[0]{$n})
+ ? $_[0]{$n} : ($_[0]{$n} = $_[0]->$b) }
+ : sub { $#_ ? $_[0]{$n} = $_[1] : $_[0]{$n} } };
+} sub new {my $s=bless {@_[1..$#_]},$_[0]; $s->can('BUILD')&&$s->BUILD;$s}
View
22 lib/Mo.pod
@@ -26,7 +26,7 @@ Mo - Micro Objects. Mo is less.
=head1 DESCRIPTION
-use Mo. Mo is less.
+use Mo. Mo is less. Much less.
Moose led to Mouse led to Moo led to Mo. M is nothing. Mo is more. Not much.
@@ -43,6 +43,9 @@ This is what you get. Nothing Mo.
Mo provides a C<new> object constructor. It will call the C<BUILD> method
after creation if it C<can>.
+NOTE: Unlike other Moose modules, BUILD is not called in a chained fashion.
+You would need to call C<SUPER::BUILD> yourself, if you needed that.
+
=item C<extends>
Mo exports the C<extends> keyword, to name your parent class. C<Mo> itself is
@@ -64,8 +67,15 @@ C<has> takes arguments after the name. Here is what it currently supports:
=item default
-Should be a code reference. The object instance is passed in, and it should
-return the default value for this attribute. Default is always called lazily.
+Must be a code reference. The object instance is passed in, and it should
+return the default value for this attribute. C<default> is always called
+lazily. ie It is called when you try to get the value and it does
+not(exists()).
+
+=item builder
+
+Must be a method name. This method should return the default value for this
+attribute. C<builder> is always called lazily.
=back
@@ -74,11 +84,13 @@ without having to change all your accessors.
=item C<strict> and C<warnings>
-Mo turns on C<use strict> and C<use warnings>.
+Mo turns on C<use strict> and C<use warnings> for you.
=item Embeddable
-Mo is tiny. You can easily inline it in your code, if you want to.
+Mo is tiny. It is currently 10 lines, <= 80 chars.
+
+You could easily inline it in your code, if you wanted to.
=back
View
10 t/test.t
@@ -1,6 +1,6 @@
use Test::More;
-plan tests => 37;
+plan tests => 39;
#============
package Foo;
@@ -51,6 +51,12 @@ has plop => (
default => sub { my $self = shift; "plop: " . $self->that },
);
has 'plip';
+has bridge => builder => 'bridge_builder';
+use constant bridge_builder => 'A Bridge';
+has guess => (
+ default => sub {'me me me'},
+ builder => 'bridge_builder',
+);
#============
package main;
@@ -83,6 +89,8 @@ is $b->plop, 'plop: thung', 'default works again';
$b->that("thyng");
is $b->plop, 'plop: thung', 'default works again';
is $b->plip, undef, 'no default is undef';
+is $b->bridge, 'A Bridge', 'builder works';
+is $b->guess, 'me me me', 'default trumps builder';
#============
package Baz;
Please sign in to comment.
Something went wrong with that request. Please try again.