NAME

Simo::Wrapper - Wrapper class to manipulate object.

VERSION

Version 0.0220

CAUTION

Simo::Wrapper is yet experimental stage.

Please wait until Simo::Wrapper will be stable.

SYNOPSIS

use Simo::Util 'o';
# new
my $book = o('Book')->new( title => 'Good day', price => 1000 );

# connect
my $dbh = o('DBI')->connect( 'dbi:SQLite:db_name=test_db', '', '' );

# new_and_validate
my $book = o('Book')->new_and_validate(
    title => 'a', sub{ length $_ < 30 },
    price => 1000, sub{ $_ > 0 && $_ < 50000 },
);

my $book = o('Book')->new_and_validate(
    { title => 'a', price => 'b' },
    { title=> sub{ length $_ < 30 }, price => sub{ $_ > 0 && $_ < 50000 } }
);

# set_values
o($book)->set_values( title => 'Good news', author => 'kimoto' );

# get_values
my ( $title, $author ) = o($book)->get_values( qw/ title author / );

# get_hashs
my $hash = o($book)->get_hash( qw/ title author / );

# run_method
o($book_list)->run_methods(
    find => [ 'author' => 'kimoto' ],
    sort => [ 'price', 'desc' ],
    'get_result'
);

# filter_values
my $result = o($book)->filter_values(
    sub{ uc $_ },
    qw/ title author /,
);

# encode_values and decode_values
o($book)->encode_values( 'utf8', qw/ title author / );
o($book)->decode_values( 'utf8', qw/ title author / );

# clone
my $book_copy = o($book)->clone;

# freeze and thaw
my $book_freezed = o($book)->freeze;
my $book = o->thaw( $book_freezed );

# new_from_xml and set_values_from_xml
my $book = o->new_from_xml( $xml_file );
o($book)->set_values_from_xml( $xml_file );

DESCRIPTION

Simo::Wrapper is the collection of methods to manipulate a object.

use a class not calling 'require' or 'use'

-> new

'new' automatically load the class, and call 'new' method.

create a object and validate values

-> new_and_validate

set or get multiple values

-> set_values, get_value, get_hash

call multiple methods

-> run_methods

convert multiple values

-> filter_values

encode or decode multiple values

-> encode_values, decode_values

clone,freeze or thaw the object

-> clone, freeze, thaw

= item create a object form xml

-> new_from_xml

set values from xml

-> set_values_from_xml

Simo::Wrapper is designed to be used from Simo::Util o function. See also Simo::Util

FUNCTION

Simo::Wrapper object is usually used from Simo::Util o function, so the following sample is explained using this function.

please write this at frist.

use Simo::Util qw( o );

new

'new' is a object constructor. Unlike normal 'new', this 'new' load class automatically and construct object.

my $book = o('Book')->new( title => 'Good day', price => 1000 );

You no longer call 'require' or 'use'.

connect

'connect' is the same as 'new'.

I prepare 'connect' method because classes like 'DBI' has 'connect' method as the object constructor.

my $dbh = o('DBI')->connect( 'dbi:SQLite:db_name=test_db', '', '' );

build

'build' is the same as 'new' except return Simo::Wrapper object.

validate

'validate' is the method for validating.

my $book = Book->new( title => 'Good time', price => 3000);
$book->validate(
    title => sub{ length $_ < 30 },
    prcie => sub{ $_ > 0 && $_ < 3000 }
);

If validator function return false value, 'validate' throw Simo::Error object.

'value_invalid' is set to 'type' field of Simo::Error object.

new_and_validate

'new_and_validate' construct object and validate object.

You can use 2 type of argument.

First: key-value-validator

my $book = o('Book')->new_and_validate(
    title => 'a', sub{ length $_ < 30 },
    price => 1000, sub{ $_ > 0 && $_ < 50000 },
    auhtor => 'Kimoto', sub{ 1 }
);

If you do not validate some field, you pass sub{ 1 } to validator.

Second: { key => valu }, { key => validator }

my $book = o('Book')->new_and_validate(
    { title => 'a', price => 'b' },
    { title=> sub{ length $_ < 30 }, price => sub{ $_ > 0 && $_ < 50000 } }
);

This method return constructed object.

define

'define' define class having some accessors.

o('Book')->define( qw/title author/ );

You can use Book class after this.

my $book = Book->new( title => 'Good news', author => 'Kimoto' );

get_values

'get_values' get the values.

my ( $title, $author ) = o($book)->get_values( qw/ title author / );

get_hash

'get_hash' get the hash of specified fields.

my $book = Book->new( title => 'Good cat', author => 'Kimoto', price => 3000 );
my $hash = o($book)->get_hash( qw/ title author / );

$hash is that

{
    title => 'Good cat',
    auhtor => 'Kimoto'
}

set_values

'set_values' set values of the object.

o($book)->set_values( title => 'Good news', author => 'kimoto' );

You can also pass hash reference

o($book)->set_values( { title => 'Good news', author => 'kimoto' } );

new_from_objective_hash

'new_from_objective_hash' construct object from a objective hash.

my $book = o->new_from_objective_hash( $objective_hash );

You maybe hear the name of objective hash at first.

objective hash is the hash that contain the information of object accroding to the following rules.

1. '__CLASS' is class name.
2. '__CLASS_CONSTRUCTOR' is object constructor name. If this is ommited, 'new' is used as constructor name.

objective hash sample is

my $objective_hash = { 
    __CLASS => 'Book',
    __CLASS_CONSTRUCTOR => 'new',
    
    title => 'Good thing',
    
    author => {
        __CLASS => 'Person',
        
        name => 'Kimoto',
        age => 19,
        country => 'Japan'
    },
    
    price => 2600
};

'Person' object is automatically constructed and set to 'author' field.

After that, 'Book' object is constructed .

new_from_xml

'new_from_xml' construct object from a XML file.

my $book = o->new_from_xml( $xml_file );

XML file sample is

<?xml version="1.0" encoding='UTF-8' ?>
<root __CLASS="Book" >
  <title>Good man</title>
  
  <author __CLASS="Person">
    <name>Kimoto</name>
    <age>28</age>
    <country>Japan</country>
  </author>
</root>

You can use the xml using the form of objective hash. See also 'new_from_objective_hash'.

The xml parser of this method is 'XML::Simple'. See also XML::Simple

set_values_from_objective_hash

'set_values_from_objective_hash' set values from a objective hash.

o($book)->set_values_from_objective_hash( $objective_hash );

See also 'new_from_objective_hash'.

set_values_from_xml

'set_values_from_xml' set values loading from XML file.

o($book)->set_values_from_xml( $xml_file );

You can use the xml using the form of objective hash. See also 'new_from_objective_hash'.

The xml parser of this method is 'XML::Simple'. See also XML::Simple

run_methods

'run_methods' call multiple methods.

my $result = o($book_list)->run_methods(
    find => [ 'author' => 'kimoto' ],
    sort => [ 'price', 'desc' ],
    'get_result'
);

This method return the return value of last method ( this example, retrun value of 'get_result' )

call

'call' is aliase of 'run_methods'

filter_values

'filter_values' convert multiple values.

o($book)->filter_values( sub{ uc $_ }, qw/ title author / );

$book->title and $book->author is converted to upper case.

This method also filter the values of array ref.

$book->author( [ 'Kimoto', 'Matuda' ] );
o($book)->filter_values( sub{ uc $_ }, qw/ author / );

'Kimoto' and 'Matuda' is converted to upper case.

This method also filter the values of hash ref.

$book->info( { country => 'Japan', quality => 'Good' } );
o($book)->filter_values( sub{ uc $_ }, qw/ info / );

'Japan' and 'Good' is converted to upper case.

These 'filter_values' logic is used by 'encode_values' and 'decode_values'.

encode_values

'encode_values' encode multiple values.

o($book)->encode_values( 'utf8', qw/ title author / );

$book->title and $book->author is encoded.

This method also encode the values of array ref.

$book->author( [ 'Kimoto', 'Matuda' ] );
o($book)->encode_values( 'utf8', qw/ author / );

'Kimoto' and 'Matuda' is encoded.

This method also encode the values of hash ref.

$book->info( { country => 'Japan', quality => 'Good' } );
o($book)->encode_values( 'utf8', qw/ info / );

'Japan' and 'Good' is encoded.

decode_values

'decode_values' decode multipul values.

o($book)->decode_values( 'utf8', qw/ title author / );

$book->title and $book->author is decoded.

This method also decode the values of array ref.

$book->author( [ 'Kimoto', 'Matuda' ] );
o($book)->decode_values( 'utf8', qw/ author / );

'Kimoto' and 'Matuda' is decoded.

This method also decode the values of hash ref.

$book->info( { country => 'Japan', quality => 'Good' } );
o($book)->decode_values( 'utf8', qw/ info / );

'Japan' and 'Good' is decoded.

clone

'clone' copy the object deeply.

my $book_copy = o($book)->clone;

'clone' is the same as Storable::clone. See also Storable

freeze

'freeze' serialize the object.

my $book_freezed = o($book)->freeze;

'freeze' is the same as Storable::freeze. See also Storable

thaw

'thaw' resotre the freezed object.

my $book = o->thaw( $book_freezed );

'thaw' is the same as Storable::thaw. See also Storable

create

'create' is constructor of 'Simo::Wrapper'.

obj

'obj' is wrapped object.

my $book = o($book)->obj;

o($book)->obj is equel to $book.

AUTHOR

Yuki Kimoto, <kimoto.yuki at gmail.com>

BUGS

Please report any bugs or feature requests to bug-simo-wrapper at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Simo-Wrapper. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc Simo::Wrapper

You can also look for information at:

ACKNOWLEDGEMENTS

COPYRIGHT & LICENSE

Copyright 2009 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.