Newer Older
241 lines | 5.618kb
copy gitweblite soruce code
root authored on 2012-11-23
1
package Mojo::Base;
2

            
3
use strict;
4
use warnings;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
5
use utf8;
copy gitweblite soruce code
root authored on 2012-11-23
6

            
7
# No imports because we get subclassed, a lot!
8
use Carp ();
9

            
10
sub say(@) {print @_, "\n"}
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
11
# Only Perl 5.14+ requires it on demand
12
use IO::Handle ();
copy gitweblite soruce code
root authored on 2012-11-23
13

            
14
sub import {
15
  my $class = shift;
16
  return unless my $flag = shift;
17

            
18
  # Base
19
  if ($flag eq '-base') { $flag = $class }
20

            
21
  # Strict
22
  elsif ($flag eq '-strict') { $flag = undef }
23

            
24
  # Module
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
25
  elsif ((my $file = $flag) && !$flag->can('new')) {
26
    $file =~ s!::|'!/!g;
27
    require "$file.pm";
copy gitweblite soruce code
root authored on 2012-11-23
28
  }
29

            
30
  # ISA
31
  if ($flag) {
32
    my $caller = caller;
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
33
    no strict 'refs';
copy gitweblite soruce code
root authored on 2012-11-23
34
    push @{"${caller}::ISA"}, $flag;
35
    *{"${caller}::has"} = sub { attr($caller, @_) };
36
  }
37
  
38
  my $caller = caller;
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
39
  {
40
    no strict 'refs';
41
    *{"${caller}::say"} = sub { say(@_) };
42
  }
copy gitweblite soruce code
root authored on 2012-11-23
43

            
44
  # Mojo modules are strict!
45
  strict->import;
46
  warnings->import;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
47
  utf8->import;
copy gitweblite soruce code
root authored on 2012-11-23
48
  feature->import(':5.10');
49
}
50

            
51
sub new {
52
  my $class = shift;
53
  bless @_ ? @_ > 1 ? {@_} : {%{$_[0]}} : {}, ref $class || $class;
54
}
55

            
56
sub attr {
57
  my ($class, $attrs, $default) = @_;
58
  return unless ($class = ref $class || $class) && $attrs;
59

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
60
  Carp::croak 'Default has to be a code reference or constant value'
copy gitweblite soruce code
root authored on 2012-11-23
61
    if ref $default && ref $default ne 'CODE';
62

            
63
  for my $attr (@{ref $attrs eq 'ARRAY' ? $attrs : [$attrs]}) {
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
64
    Carp::croak qq{Attribute "$attr" invalid} unless $attr =~ /^[a-zA-Z_]\w*$/;
copy gitweblite soruce code
root authored on 2012-11-23
65

            
66
    # Header (check arguments)
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
67
    my $code = "package $class;\nsub $attr {\n  if (\@_ == 1) {\n";
copy gitweblite soruce code
root authored on 2012-11-23
68

            
69
    # No default value (return value)
70
    unless (defined $default) { $code .= "    return \$_[0]{'$attr'};" }
71

            
72
    # Default value
73
    else {
74

            
75
      # Return value
76
      $code .= "    return \$_[0]{'$attr'} if exists \$_[0]{'$attr'};\n";
77

            
78
      # Return default value
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
79
      $code .= "    no warnings 'closure';\n";
copy gitweblite soruce code
root authored on 2012-11-23
80
      $code .= "    return \$_[0]{'$attr'} = ";
81
      $code .= ref $default eq 'CODE' ? '$default->($_[0]);' : '$default;';
82
    }
83

            
84
    # Store value
85
    $code .= "\n  }\n  \$_[0]{'$attr'} = \$_[1];\n";
86

            
87
    # Footer (return invocant)
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
88
    $code .= "  \$_[0];\n}";
copy gitweblite soruce code
root authored on 2012-11-23
89

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
90
    warn "-- Attribute $attr in $class\n$code\n\n" if $ENV{MOJO_BASE_DEBUG};
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
91
    Carp::croak "Mojo::Base error: $@" unless eval "$code;1";
copy gitweblite soruce code
root authored on 2012-11-23
92
  }
93
}
94

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
95
sub tap {
96
  my ($self, $cb) = @_;
97
  $_->$cb for $self;
98
  return $self;
99
}
100

            
copy gitweblite soruce code
root authored on 2012-11-23
101
1;
102

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
103
=encoding utf8
104

            
copy gitweblite soruce code
root authored on 2012-11-23
105
=head1 NAME
106

            
107
Mojo::Base - Minimal base class for Mojo projects
108

            
109
=head1 SYNOPSIS
110

            
111
  package Cat;
112
  use Mojo::Base -base;
113

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
114
  has name => 'Nyan';
115
  has [qw(birds mice)] => 2;
copy gitweblite soruce code
root authored on 2012-11-23
116

            
117
  package Tiger;
118
  use Mojo::Base 'Cat';
119

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
120
  has friend  => sub { Cat->new };
copy gitweblite soruce code
root authored on 2012-11-23
121
  has stripes => 42;
122

            
123
  package main;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
124
  use Mojo::Base -strict;
copy gitweblite soruce code
root authored on 2012-11-23
125

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
126
  my $mew = Cat->new(name => 'Longcat');
127
  say $mew->mice;
128
  say $mew->mice(3)->birds(4)->mice;
copy gitweblite soruce code
root authored on 2012-11-23
129

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
130
  my $rawr = Tiger->new(stripes => 23, mice => 0);
131
  say $rawr->tap(sub { $_->friend->name('Tacgnol') })->mice;
copy gitweblite soruce code
root authored on 2012-11-23
132

            
133
=head1 DESCRIPTION
134

            
135
L<Mojo::Base> is a simple base class for L<Mojo> projects.
136

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
137
  # Automatically enables "strict", "warnings", "utf8" and Perl 5.10 features
copy gitweblite soruce code
root authored on 2012-11-23
138
  use Mojo::Base -strict;
139
  use Mojo::Base -base;
140
  use Mojo::Base 'SomeBaseClass';
141

            
142
All three forms save a lot of typing.
143

            
144
  # use Mojo::Base -strict;
145
  use strict;
146
  use warnings;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
147
  use utf8;
copy gitweblite soruce code
root authored on 2012-11-23
148
  use feature ':5.10';
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
149
  use IO::Handle ();
copy gitweblite soruce code
root authored on 2012-11-23
150

            
151
  # use Mojo::Base -base;
152
  use strict;
153
  use warnings;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
154
  use utf8;
copy gitweblite soruce code
root authored on 2012-11-23
155
  use feature ':5.10';
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
156
  use IO::Handle ();
copy gitweblite soruce code
root authored on 2012-11-23
157
  use Mojo::Base;
158
  push @ISA, 'Mojo::Base';
159
  sub has { Mojo::Base::attr(__PACKAGE__, @_) }
160

            
161
  # use Mojo::Base 'SomeBaseClass';
162
  use strict;
163
  use warnings;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
164
  use utf8;
copy gitweblite soruce code
root authored on 2012-11-23
165
  use feature ':5.10';
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
166
  use IO::Handle ();
copy gitweblite soruce code
root authored on 2012-11-23
167
  require SomeBaseClass;
168
  push @ISA, 'SomeBaseClass';
169
  use Mojo::Base;
170
  sub has { Mojo::Base::attr(__PACKAGE__, @_) }
171

            
172
=head1 FUNCTIONS
173

            
174
L<Mojo::Base> exports the following functions if imported with the C<-base>
175
flag or a base class.
176

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
177
=head2 has
copy gitweblite soruce code
root authored on 2012-11-23
178

            
179
  has 'name';
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
180
  has [qw(name1 name2 name3)];
copy gitweblite soruce code
root authored on 2012-11-23
181
  has name => 'foo';
182
  has name => sub {...};
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
183
  has [qw(name1 name2 name3)] => 'foo';
184
  has [qw(name1 name2 name3)] => sub {...};
copy gitweblite soruce code
root authored on 2012-11-23
185

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
186
Create attributes for hash-based objects, just like the L</"attr"> method.
copy gitweblite soruce code
root authored on 2012-11-23
187

            
188
=head1 METHODS
189

            
190
L<Mojo::Base> implements the following methods.
191

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
192
=head2 new
copy gitweblite soruce code
root authored on 2012-11-23
193

            
194
  my $object = BaseSubClass->new;
195
  my $object = BaseSubClass->new(name => 'value');
196
  my $object = BaseSubClass->new({name => 'value'});
197

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
198
This base class provides a basic constructor for hash-based objects. You can
199
pass it either a hash or a hash reference with attribute values.
copy gitweblite soruce code
root authored on 2012-11-23
200

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
201
=head2 attr
copy gitweblite soruce code
root authored on 2012-11-23
202

            
203
  $object->attr('name');
204
  BaseSubClass->attr('name');
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
205
  BaseSubClass->attr([qw(name1 name2 name3)]);
copy gitweblite soruce code
root authored on 2012-11-23
206
  BaseSubClass->attr(name => 'foo');
207
  BaseSubClass->attr(name => sub {...});
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
208
  BaseSubClass->attr([qw(name1 name2 name3)] => 'foo');
209
  BaseSubClass->attr([qw(name1 name2 name3)] => sub {...});
210

            
211
Create attribute accessor for hash-based objects, an array reference can be
212
used to create more than one at a time. Pass an optional second argument to
213
set a default value, it should be a constant or a callback. The callback will
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
214
be executed at accessor read time if there's no set value. Accessors can be
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
215
chained, that means they return their invocant when they are called with an
216
argument.
217

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
218
=head2 tap
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
219

            
220
  $object = $object->tap(sub {...});
copy gitweblite soruce code
root authored on 2012-11-23
221

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
222
K combinator, tap into a method chain to perform operations on an object
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
223
within the chain. The object will be the first argument passed to the callback
224
and is also available as C<$_>.
copy gitweblite soruce code
root authored on 2012-11-23
225

            
226
=head2 C<say>
227

            
228
Backported from perl-5.10.1
229

            
230
=head1 DEBUGGING
231

            
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
232
You can set the MOJO_BASE_DEBUG environment variable to get some advanced
copy gitweblite soruce code
root authored on 2012-11-23
233
diagnostics information printed to C<STDERR>.
234

            
235
  MOJO_BASE_DEBUG=1
236

            
237
=head1 SEE ALSO
238

            
239
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
240

            
241
=cut