[Moscow.pm] refactoring
Akzhan Abdulin
akzhan.abdulin на gmail.com
Вт Дек 8 23:20:38 PST 2015
Storable меняем, где можем, на CBOR::XS или JSON::XS
09 дек 2015 г. 6:38 пользователь "Nikolay Mishin" <mi на ya.ru> написал:
>
> Кстати,насчет объектов в Perl
> вот говорят про Moose,а используют
> Kelp::Base;
>
> https://github.com/mishin/kelp/blob/master/lib/Kelp/Base.pm
>
> https://github.com/mishin/kelp/blob/master/t/base.t
> получается типа
>
>
> package Ftree::Name;
> use Kelp::Base;
> attr title => undef;
> attr prefix => undef;
> attr first_name => undef;
> attr mid_name => undef;
> attr last_name => undef;
> attr suffix => undef;
> attr nickname => undef;
>
> attr full_name => sub {
> my $self = shift;
> my @name_array = ( $self->last_name, $self->first_name, $self->mid_name );
> join( ' ', @name_array );
> };
>
> package main;
> use Test::More;
>
> my $o = Ftree::Name->new;
>
> isa_ok $o, 'Ftree::Name';
>
> can_ok $o, qw/title prefix first_name mid_name last_name suffix suffix/;
>
> $o->first_name('Николай');
> is $o->first_name, 'Николай', 'set name is ok';
> $o->last_name('Мишин');
> $o->mid_name('Алексеевич');
> is $o->full_name, 'Мишин Николай Алексеевич', 'fullname ok';
>
> done_testing;
>
> perl object_example.t
> ok 1 - An object of class 'Ftree::Name' isa 'Ftree::Name'
> ok 2 - Ftree::Name->can(...)
> ok 3 - set name is ok
> ok 4 - fullname ok
> 1..4
>
>
>
> так хорошо писать?
>
> https://github.com/mishin/presentation/blob/master/kelp/object_example.t
>
>
>
>
> как замену
> https://github.com/mishin/Ftree-cpan/blob/master/lib/Ftree/Person.pm
>
> да и Class::Std::Storable устарел меняем его на просто Storable
> <https://metacpan.org/pod/Storable>
>
>
> 15.10.2015, 13:52, "Михаил Шогин" <mshogin на gmail.com>:
>
> Это конечно не Perl, но все тоже самое можно написать и на Perl-e
>
> class NonePerson(BasePerson):
>
> def get_id():
> return None
>
>
> class Person(BasePerson):
>
> def get_father():
> return self.father or NonePerson()
>
> def get_mother():
> return self.mother or NonePerson()
>
> def get_date_of_birth():
> return self.birthday # datetime object
>
> def get_date_of_birth_as_a_very_custom_string():
> date = self.birthday.day or ""
>
> if self.birthday.month:
> date . = "/" + self.birthday.month
>
> if self.birthday.year:
> date . = "/" + self.birthday.year
>
> return date
>
>
> class PersonRowBuilder():
>
> def build(self, person):
> row = ()
> row.append(person.get_father().get_id())
> row.append(person.get_mather().get_id())
> row.append(person.get_email())
> row.append(person.get_homepage())
>
> return row
>
> ##################################
> def info():
> person = Person()
> birthday = person.get_date_of_birth_as_a_very_custom_string()
>
>
> def method_related_to_db_usage():
> builder = PersonRowBuilder()
> row = builder.build(Person())
>
>
> 15 октября 2015 г., 11:58 пользователь Vladimir Timofeev <
> vovkasm на gmail.com> написал:
>
> Гм...
>
> push @person_row, $person->get_father_id, $person->get_mother_id,
> $person->get_email, $person->get_homepage;
>
> my $date = $person->get_display_date_of_birth;
>
> package Person;
>
> sub get_father_id {
> my $self = shift;
> if (my $self->get_father) {
> return $self->get_father->get_id;
> }
> return;
> }
>
> ... и т.д.
>
> Это раз.
>
> Можно пойти дальше и объединить построение @person_row в один метод:
>
> push @person_row, $person->get_person_fields
>
> Хотя использование массива здесь выглядит очень подозрительно, но это
> уже к вопросу не относится.
>
> С датами, помимо выноса в отдельный метод кода форматирования, можно
> потом вынести код форматирования в класс даты. Потом можно переписать
> код форматирования даты, так, чтоб работал корректно во всех случаях.
> А то сейчас можно сделать "3/" к примеру (задан только месяц).
>
>
>
>
> 2015-10-15 5:43 GMT+03:00 Nikolay Mishin <mi на ya.ru>:
> > Hi Moscow.PM!
> >
> > Существует ли какой-либо сбособ сделать более читаемым такой код?:
> >
> > push @person_row, (defined $person->get_father()) ?
> $person->get_father()->get_id() : undef;
> > push @person_row, (defined $person->get_mother()) ?
> $person->get_mother()->get_id() : undef;
> > push @person_row, ($person->get_email(), $person->get_homepage());
> >
> > my $date = "";
> > if(defined $person->get_date_of_birth()) {
> > my $date_of_birth = $person->get_date_of_birth();
> > $date .= defined $date_of_birth->day ? $date_of_birth->day."/" :
> "";
> > $date .= defined $date_of_birth->month ? $date_of_birth->month."/"
> : "";
> > $date .= defined $date_of_birth->year ? $date_of_birth->year : "";
> > }
> >
> > источник
> https://github.com/mishin/Ftree-cpan/blob/master/lib/Ftree/Exporters/ExcelExporter.pm#L53
> >
> > --
> > С уважением
> > Николай Мишин
> >
> > --
> > Moscow.pm mailing list
> > moscow-pm на pm.org | http://moscow.pm.org
>
>
> --
> Vladimir Timofeev <vovkasm на gmail.com>
> --
> Moscow.pm mailing list
> moscow-pm на pm.org | http://moscow.pm.org
>
>
>
>
> --
> С уважением
> Михаил Шогин.
> ,--
> Moscow.pm mailing list
> moscow-pm на pm.org | http://moscow.pm.org
>
>
>
> --
> С уважением
> Николай Мишин
>
>
>
> --
> Moscow.pm mailing list
> moscow-pm на pm.org | http://moscow.pm.org
>
>
----------- следущая часть -----------
Вложение в формате HTML было извлечено…
URL: <http://mail.pm.org/pipermail/moscow-pm/attachments/20151209/63b44094/attachment-0001.html>
Подробная информация о списке рассылки Moscow-pm