NAME
Object::Simple - a simple class builder
VERSION
Version 2.1301
FEATURES
- 1. You can define accessors in a very simple way.
- 2. The "new()" method is already defined.
- 3. You can define various accessor option (default, type, chained, weak).
- 4. you can use a mixin system like Ruby's.
By using Object::Simple, you will be exempt from the bitter work of repeatedly writing the new() constructor and the accessors.
But now I recommend Object::Simple::Base than Object::Simple.
It is more Object Oriented, more simple and easier, and Mojo::Base compatible.
SYNOPSIS
# Class definition( Book.pm )
package Book;
use Object::Simple;
sub title : Attr {}
sub author : Attr {}
sub price : Attr {}
Object::Simple->build_class;
# End of module. Don't forget to call 'build_class' method
# Using class
use Book;
my $book = Book->new(title => 'a', author => 'b', price => 1000);
# Default
sub author : Attr { default => 'Good new' }
sub persons : Attr { default => sub {['Ken', 'Taro']} }
# Build
sub title : Attr { build => 'Good news' }
sub authors : Attr { build => sub{['Ken', 'Taro']} }
# Weak reference
sub parent : Attr { weak => 1 }
# Variable type
sub authors : Attr { type => 'array' }
sub country : Attr { type => 'hash' }
# Convert to object
sub url : Attr { convert => 'URI' }
sub url : Attr { convert => sub{ ref $_[0] ? $_[0] : URI->new($_[0]) } }
# Dereference return value
sub authors : Attr { type => 'array', deref => 1 }
sub country_id : Attr { type => 'hash', deref => 1 }
# Trigger when value is set
sub error : Attr { trigger => sub{
my $self = shift;
$self->state('error');
}}
sub state : Attr {}
# Inheritance
package Magazine;
use Object::Simple(base => 'Book');
# Mixin
package Book;
use Object::Simple(
mixins => [
'Object::Simple::Mixin::AttrNames',
'Object::Simple::Mixin::AttrOptions'
]
);
Methods
new
Object::Simple defines a 'new' method for the subclass, so you do not need to define 'new'. 'new' accepts a hash or a hash reference.
$book = Book->new(title => 'Good life', author => 'Ken', price => 200);
$book = Book->new({title => 'Good life', author => 'Ken', price => 200});
build_class
You must call build_class at the end of the package. The class will be completely constructed by this invocation.
Object::Simple->build_class;
The following processes is excuted.
1. Inherit base class
2. Include mixin classes
3. Create accessors
4. Create constructor
You can specify class name if you need.
Object::Simple->build_class($class);
call_super
Call method of super class.
$self->call_super('initialize');
You can call method of super class. but the method is not one of real super class. Method is searched by using the follwoing order.
+-------------+
3 | BaseClass | # real super class
+-------------+
|
+-------------+
2 | MixinClass1 |
+-------------+
|
+-------------+
1 | MixinClass2 |
+-------------+
|
+-------------+
| ThisClass |
+-------------+
If 'Mixin class2' has 'initialize' method, the method is called.
call_mixin
Call a method of mixined class
$self->call_mixin('MixinClass1', 'initialize');
You can call any method of mixin class, even if method is not imported to your class
mixin_methods
Get all same name methods of mixin classes
my $methods = $self->mixin_methods('initialize');
You can call all methods of mixined classes as the following way.
foreach my $method (@$methods) {
$self->$method();
}
resist_accessor_info
You can resist accessor informations.
Object::Simple->resist_accessor_info($class, $accessor_name,
$accessor_options, $accessor_type);
The following is arguments sample
Object::Simple->resist_accessor_info('Book', 'title', {default => 1}, 'Attr');
This is equal to
package Book;
sub title : Attr {default => 1}
This method only resist accessor infomation. If you want to build class, you must call 'build_class'
Object::Simple->build_class('Book');
Accessor options
Inheritance
# Inheritance
package Magazine;
use Object::Simple(base => 'Book');
Object::Simple do not support multiple inheritance because it is so complex.
Mixin
Object::Simple support mixin syntax
# Mixin
package Book;
use Object::Simple(
mixins => [
'Object::Simple::Mixin::AttrNames',
'Object::Simple::Mixin::AttrOptions'
]
);
Object::Simple mixin merge mixin class attribute.
# mixin class
package Some::Mixin;
use Object::Simple;
sub m2 : Attr {}
Object::Simple->build_class;
# using mixin class
package Some::Class;
use Object::Simple(mixins => ['Some::Mixin']);
sub m1 : Attr {}
Object::Simple->build_class;
Because Some::Mixin is mixined, Some::Class has two attribute m1 and m2.
Author
Yuki Kimoto, <kimoto.yuki at gmail.com>
Github http://github.com/yuki-kimoto/
I develop this module at http://github.com/yuki-kimoto/object-simple .
Please tell me bug if you find.
Copyright & license
Copyright 2008 Yuki Kimoto, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.