Showing 1 changed files with 50 additions and 70 deletions
+50 -70
lib/DBI/Custom.pm
... ...
@@ -1,95 +1,76 @@
1 1
 package DBI::Custom;
2 2
 use Object::Simple;
3 3
 
4
-# Import
5
-sub setup {
6
-    my ($class, $setup) = @_;
7
-    
8
-    # Import function to caller class;
9
-    $class->_import_functions_to($class);
10
-    
11
-    # Setup Caller class
12
-    $setup->();
13
-    
14
-    # Remove function from caller class;
15
-    $class->_remove_imported_functions($class);
4
+sub new {
5
+    my $self = shift->Object::Simple::new(@_);
6
+    my $class = ref $self;
7
+    return bless {%{$class->model}, %{$self}}, $class;
16 8
 }
17 9
 
18
-# Tempature import functions
19
-sub _temp_import_functions {
20
-    connect_info => sub {
21
-        my %options = @_;
22
-        my $class = caller;
23
-        $class->DBI::Custom::connect_info(%options);
24
-    },
25
-    create_table => sub {
26
-        my $table = shift;
27
-        my @row_infos = @_;
28
-        
29
-        my $class = caller;
30
-        $class->table_infos->{$table} = {};
31
-        
32
-        for (my $i = 0; $i < @columns; i++) {
33
-            my $column = $columns[$i];
34
-            
35
-            my $column_name = shift @$column;
36
-            my $column_type = shift @$column;
37
-            my %column_options = @$column;
38
-            
39
-            my $column_info
40
-              = $class->table_infos->{$table}{column}{$column_name} = {};
41
-            
42
-            $column_info->{pos}     = $i;
43
-            $column_info->{type}    = $column_type;
44
-            $column_info->{options} = \%column_options;
45
-        }
46
-    }
47
-}
10
+sub create_model {shift->Object::Simple::new(@_);
48 11
 
49
-# Import functions to caller class
50
-sub _import_functions_to {
51
-    my ($self, $class) = @_;
52
-    no strict 'refs';
53
-    foreach my $import_function (keys %{$self->_temp_import_functions}) {
54
-        *{"${class}::$import_function"}
55
-          = $self->_temp_import_functions->{$import_function};
56
-    }
57
-}
58
-
59
-# Remove functions from caller class
60
-sub _remove_imported_functions {
61
-    my ($self, $class) = @_;
62
-    no strict 'refs';
63
-    foreach my $import_function (keys %{$self->_temp_import_functions}) {
64
-        delete ${$class . '::'}{"$import_function"};    
65
-    }
12
+sub initialize_model {
13
+    my ($class, $callback) = @_;
14
+    
15
+    my $model = $class->create_model;
16
+    
17
+    $callback->($model);
18
+    
19
+    $class->model($model);
66 20
 }
67 21
 
68
-
69 22
 # Class attribute
70
-sub connect_info : ClassAttr { type => 'hash', default => sub { {} } }
71
-sub table_infos : ClassAttr { type => 'hash', default => sub { {} } }
23
+sub connect_info : Attr { type => 'hash' }
24
+sub table_infos  : Attr { type => 'hash' }
72 25
 
73 26
 sub column_info {
74
-    my ($class, $table, $column_name) = @_;
75
-    return $class->table_infos->{$table}{column}{$column_name};
27
+    my ($self, $table, $column_name, $column_info) = @_;
28
+    
29
+    if (@_ > 3) {
30
+        $self->table_infos->{$table}{column}{$column_name} = $column_info;
31
+        return $self;
32
+    }
33
+    return $self->table_infos->{$table}{column}{$column_name};
76 34
 }
77 35
 
78 36
 sub columns {
79
-    my ($class, $table) = @_;
37
+    my ($self, $table) = @_;
80 38
     
81 39
     return sort { 
82
-        $class->table_infos->{$table}{column}{$a}{pos} 
40
+        $self->table_infos->{$table}{column}{$a}{pos} 
83 41
         <=>
84
-        $class->table_infos->{$table}{column}{$b}{pos}
85
-    } keys %{$class->table_info->{$table}{column}}
42
+        $self->table_infos->{$table}{column}{$b}{pos}
43
+    } keys %{$self->table_info->{$table}{column}}
86 44
 }
87 45
 
88 46
 sub tables {
89
-    my $class = shift;
47
+    my $self = shift;
90 48
     return keys %{$self->table_info};
91 49
 }
92 50
 
51
+sub create_table {
52
+    my ($self, $table, @row_infos) = @_;
53
+    
54
+    $self->table_infos->{$table} = {};
55
+    
56
+    for (my $i = 0; $i < @columns; i++) {
57
+        my $column = $columns[$i];
58
+        
59
+        my $column_name = shift @$column;
60
+        my $column_type = shift @$column;
61
+        my %column_options = @$column;
62
+        
63
+        my $column_info = {};
64
+        
65
+        $column_info->{pos}     = $i;
66
+        $column_info->{type}    = $column_type;
67
+        $column_info->{options} = \%column_options;
68
+        
69
+        $self->column_info($table, $column_name, $column_info);
70
+    }
71
+}
72
+
73
+
93 74
 
94 75
 
95 76
 
... ...
@@ -104,7 +85,6 @@ sub insert {
104 85
 
105 86
 Object::Simple->build_class;
106 87
 
107
-
108 88
 =head1 NAME
109 89
 
110 90
 DBI::Custom - The great new DBI::Custom!