[Jifty-commit] r6148 - in Net-Jifty/branches/classbuilder: .

Jifty commits jifty-commit at lists.jifty.org
Wed Dec 17 00:54:13 EST 2008


Author: sartak
Date: Wed Dec 17 00:54:13 2008
New Revision: 6148

Modified:
   Net-Jifty/branches/classbuilder/   (props changed)
   Net-Jifty/branches/classbuilder/lib/Net/Jifty/ClassBuilder.pm

Log:
 r77655 at onn:  sartak | 2008-12-17 00:54:09 -0500
 add_attribute


Modified: Net-Jifty/branches/classbuilder/lib/Net/Jifty/ClassBuilder.pm
==============================================================================
--- Net-Jifty/branches/classbuilder/lib/Net/Jifty/ClassBuilder.pm	(original)
+++ Net-Jifty/branches/classbuilder/lib/Net/Jifty/ClassBuilder.pm	Wed Dec 17 00:54:13 2008
@@ -21,10 +21,48 @@
         exists($args{auto_update}) ? (auto_update => $args{auto_update}) : (),
     );
 
+    my $model = $args{model_spec};
+    for my $attribute (keys %$model) {
+        next if $attribute eq 'id'; # built into Net::Jifty::Record
+
+        my $spec = $model->{$attribute};
+        $self->add_attribute($meta, $attribute, $spec);
+    }
+
     $meta->make_immutable;
     return $meta;
 }
 
+sub add_attribute {
+    my ($self, $meta, $name, $spec) = @_;
+
+    my %args;
+    $args{is} = $spec->{writable} ? 'rw'
+              : $spec->{readable} ? 'ro'
+                                  : '';
+    delete $args{is} if $args{is} eq '';
+
+    my $type_constraint = $self->create_type_constraint($spec);
+    $args{isa} = $type_constraint if defined($type_constraint);
+
+    $meta->add_attribute($name => %args);
+}
+
+my %builtin_type_constraint = (
+    text    => 'Str',
+    int     => 'Int',
+    integer => 'Int',
+    serial  => 'Int',
+);
+
+sub create_type_constraint {
+    my ($self, $spec) = @_;
+
+    return $builtin_type_constraint{ $spec->{type} }
+        if exists $builtin_type_constraint{ $spec->{type} };
+    return;
+}
+
 __PACKAGE__->meta->make_immutable;
 no Moose;
 


More information about the Jifty-commit mailing list