| 1 | package Net::ISC::DHCPd::OMAPI::Sugar; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Net::ISC::DHCPd::OMAPI::Sugar - Moose sugar for omapi classes | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | use Net::ISC::DHCPd::OMAPI::Sugar; # not use Moose | ||
| 10 | |||
| 11 | omapi_attr foo => ( isa => State, ... ); | ||
| 12 | |||
| 13 | =cut | ||
| 14 | |||
| 15 | use Moose; | ||
| 16 | use Net::ISC::DHCPd::Types ':all'; | ||
| 17 | use Moose::Exporter; | ||
| 18 | |||
| 19 | my @types = Net::ISC::DHCPd::Types->get_type_list; | ||
| 20 | |||
| 21 | =head1 FUNCTIONS | ||
| 22 | |||
| 23 | =head2 omapi_attr | ||
| 24 | |||
| 25 | omapi_attr $name => %attr; | ||
| 26 | |||
| 27 | C<%attr> is by default: | ||
| 28 | |||
| 29 | ( | ||
| 30 | is => "rw", | ||
| 31 | predicate => "has_$name", | ||
| 32 | traits => [qw/Net::ISC::DHCPd::OMAPI::Meta::Attribute/], | ||
| 33 | ) | ||
| 34 | |||
| 35 | It will also set "coerce => 1", when "isa" is one of L<Moose> | ||
| 36 | types from L<Net::ISC::DHCPd::Types>. | ||
| 37 | |||
| 38 | =cut | ||
| 39 | |||
| 40 | sub omapi_attr { | ||
| 41 | my $class = shift; | ||
| 42 | my $names = ref $_[0] eq 'ARRAY' ? shift : [shift]; | ||
| 43 | my %opts = @_; | ||
| 44 | my $to_raw; | ||
| 45 | |||
| 46 | if(my $type = $opts{'isa'}) { | ||
| 47 | if($type =~ /^Net::ISC::DHCPd::Types/ and $type->coercion) { | ||
| 48 | $type =~ s/Net::ISC::DHCPd::Types:://; | ||
| 49 | $opts{'coerce'} = 1; | ||
| 50 | $to_raw = Net::ISC::DHCPd::Types->can("from_$type"); | ||
| 51 | } | ||
| 52 | } | ||
| 53 | else { | ||
| 54 | confess '"isa" is required for omapi_attr()'; | ||
| 55 | } | ||
| 56 | |||
| 57 | for my $name (@$names) { | ||
| 58 | $class->meta->add_attribute($name => ( | ||
| 59 | is => 'rw', | ||
| 60 | predicate => "has_$name", | ||
| 61 | clearer => "clear_$name", | ||
| 62 | traits => [qw/Net::ISC::DHCPd::OMAPI::Meta::Attribute/], | ||
| 63 | %opts, | ||
| 64 | )); | ||
| 65 | $class->meta->add_method("raw_$name" => | ||
| 66 | $to_raw ? sub { shift->$to_raw($name) } : sub { shift->$name } | ||
| 67 | ); | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | Moose::Exporter->setup_import_methods( | ||
| 72 | with_caller => [qw/omapi_attr/], | ||
| 73 | as_is => \@types, | ||
| 74 | ); | ||
| 75 | |||
| 76 | =head1 COPYRIGHT & LICENSE | ||
| 77 | |||
| 78 | =head1 AUTHOR | ||
| 79 | |||
| 80 | See L<Net::ISC::DHCPd>. | ||
| 81 | |||
| 82 | =cut | ||
| 83 | __PACKAGE__->meta->make_immutable; | ||
| 84 | 1; |