aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Reaction/InterfaceModel/Reflector/DBIC.pm
blob: 96bc262920d1f11ccbe8dc553cee32b39dad3487 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
package Reaction::InterfaceModel::Reflector::DBIC;

use aliased 'Reaction::InterfaceModel::Action::DBIC::ResultSet::Create';
use aliased 'Reaction::InterfaceModel::Action::DBIC::ResultSet::DeleteAll';
use aliased 'Reaction::InterfaceModel::Action::DBIC::Result::Update';
use aliased 'Reaction::InterfaceModel::Action::DBIC::Result::Delete';

use aliased 'Reaction::InterfaceModel::Collection::Virtual::ResultSet';
use aliased 'Reaction::InterfaceModel::Object';
use aliased 'Reaction::InterfaceModel::Action';
use Reaction::Class;
use Class::MOP;

use Catalyst::Utils;

use namespace::clean -except => [ qw(meta) ];

has make_classes_immutable => (isa => "Bool", is => "rw", required => 1, default => sub{ 1 });

#user defined actions and prototypes
has object_actions     => (isa => "HashRef", is => "rw", lazy_build => 1);
has collection_actions => (isa => "HashRef", is => "rw", lazy_build => 1);

#which actions to create by default
has default_object_actions     => (isa => "ArrayRef", is => "rw", lazy_build => 1);
has default_collection_actions => (isa => "ArrayRef", is => "rw", lazy_build => 1);

#builtin actions and prototypes
has builtin_object_actions     => (isa => "HashRef", is => "rw", lazy_build => 1);
has builtin_collection_actions => (isa => "HashRef", is => "rw", lazy_build => 1);
sub _build_object_actions { {} };
sub _build_collection_actions { {} };
sub _build_default_object_actions { [ qw/Update Delete/ ] };
sub _build_default_collection_actions { [ qw/Create DeleteAll/ ] };
sub _build_builtin_object_actions {
  {
    Update => { name => 'Update', base => Update },
    Delete => { name => 'Delete', base => Delete, attributes => [] },
  };
};
sub _build_builtin_collection_actions {
  {
    Create    => {name => 'Create',    base => Create    },
    DeleteAll => {name => 'DeleteAll', base => DeleteAll, attributes => [] }
  };
};
sub _all_object_actions {
 my $self = shift;
  return $self->merge_hashes
    ($self->builtin_object_actions, $self->object_actions);
};
sub _all_collection_actions {
  my $self = shift;
  return $self->merge_hashes
    ($self->builtin_collection_actions, $self->collection_actions);
};
sub dm_name_from_class_name {
  my($self, $class) = @_;
  confess("wrong arguments") unless $class;
  $class =~ s/::/_/g;
  $class = "_" . $self->_class_to_attribute_name($class) . "_store";
  return $class;
};
sub dm_name_from_source_name {
  my($self, $source) = @_;
  confess("wrong arguments") unless $source;
  $source =~ s/([a-z0-9])([A-Z])/${1}_${2}/g ;
  $source = "_" . $self->_class_to_attribute_name($source) . "_store";
  return $source;
};
sub class_name_from_source_name {
  my ($self, $model_class, $source_name) = @_;
  confess("wrong arguments") unless $model_class && $source_name;
  return join "::", $model_class, $source_name;
};
sub class_name_for_collection_of {
  my ($self, $object_class) = @_;
  confess("wrong arguments") unless $object_class;
  return "${object_class}::Collection";
};
sub merge_hashes {
  my($self, $left, $right) = @_;
  return Catalyst::Utils::merge_hashes($left, $right);
};
sub parse_reflect_rules {
  my ($self, $rules, $haystack) = @_;
  confess('$rules must be an array reference')    unless ref $rules    eq 'ARRAY';
  confess('$haystack must be an array reference') unless ref $haystack eq 'ARRAY';

  my $needles = {};
  my (@exclude, @include, $global_opts);
  if(@$rules == 2 && $rules->[0] eq '-exclude'){
    push(@exclude, (ref $rules->[1] eq 'ARRAY' ? @{$rules->[1]} : $rules->[1]));
  } else {
    for my $rule ( @$rules ){
      if (ref $rule eq 'ARRAY' && $rule->[0] eq '-exclude'){
        push(@exclude, (ref $rule->[1] eq 'ARRAY' ? @{$rule->[1]} : $rule->[1]));
      } elsif( ref $rule eq 'HASH' ){
        $global_opts = ref $global_opts eq 'HASH' ?
          $self->merge_hashes($global_opts, $rule) : $rule;
      } else {
        push(@include, $rule);
      }
    }
  }
  my $check_exclude = sub{
    for my $rule (@exclude){
      return 1 if(ref $rule eq 'Regexp' ? $_[0] =~ /$rule/ : $_[0] eq $rule);
    }
    return;
  };

  @$haystack = grep { !$check_exclude->($_) } @$haystack;
  $self->merge_reflect_rules(\@include, $needles, $haystack, $global_opts);
  return $needles;
};
sub merge_reflect_rules {
  my ($self, $rules, $needles, $haystack, $local_opts) = @_;
  for my $rule ( @$rules ){
    if(!ref $rule && ( grep {$rule eq $_} @$haystack ) ){
      $needles->{$rule} = defined $needles->{$rule} ?
        $self->merge_hashes($needles->{$rule}, $local_opts) : $local_opts;
    } elsif( ref $rule eq 'Regexp' ){
      for my $match ( grep { /$rule/ } @$haystack ){
        $needles->{$match} = defined $needles->{$match} ?
          $self->merge_hashes($needles->{$match}, $local_opts) : $local_opts;
      }
    } elsif( ref $rule eq 'ARRAY' ){
      my $opts;
      $opts = pop(@$rule) if @$rule > 1 and ref $rule->[$#$rule] eq 'HASH';
      $opts = $self->merge_hashes($local_opts, $opts) if defined $local_opts;
      $self->merge_reflect_rules($rule, $needles, $haystack, $opts);
    }
  }
};
sub reflect_schema {
  my ($self, %opts) = @_;
  my $base    = delete $opts{base} || Object;
  my $roles   = delete $opts{roles} || [];
  my $model   = delete $opts{model_class};
  my $schema  = delete $opts{schema_class};
  my $dm_name = delete $opts{domain_model_name};
  my $dm_args = delete $opts{domain_model_args} || {};
  $dm_name ||= $self->dm_name_from_class_name($schema);

  #load all necessary classes
  confess("model_class and schema_class are required parameters")
    unless($model && $schema);
  Class::MOP::load_class( $base );
  Class::MOP::load_class( $schema );
  my $meta = $self->_load_or_create(
    $model,
    superclasses => [$base],
    ( @$roles ? (roles => $roles) : ()),
  );

  # sources => undef,              #default to qr/./
  # sources => [],                 #default to nothing
  # sources => qr//,               #DWIM, treated as [qr//]
  # sources => [{...}]             #DWIM, treat as [qr/./, {...} ]
  # sources => [[-exclude => ...]] #DWIM, treat as [qr/./, [-exclude => ...]]
  my $haystack = [ $schema->sources ];

  my $rules = delete $opts{sources};
  if(!defined $rules){
    $rules = [qr/./];
  } elsif( ref $rules eq 'Regexp'){
    $rules = [ $rules ];
  } elsif( ref $rules eq 'ARRAY' && @$rules){
    #don't add a qr/./ rule if we have at least one match rule
    push(@$rules, qr/./) unless grep {(ref $_ eq 'ARRAY' && $_->[0] ne '-exclude')
                                        || !ref $_  || ref $_ eq 'Regexp'} @$rules;
  }

  my $sources = $self->parse_reflect_rules($rules, $haystack);

  my $make_immutable = $meta->is_immutable || $self->make_classes_immutable;
  $meta->make_mutable if $meta->is_immutable;

  $meta->add_domain_model
    ($dm_name, is => 'rw', isa => $schema, required => 1, %$dm_args);

  for my $source_name (keys %$sources){
    my $source_opts = $sources->{$source_name} || {};
    $self->reflect_source(
                          source_name  => $source_name,
                          parent_class => $model,
                          schema_class => $schema,
                          source_class => $schema->class($source_name),
                          parent_domain_model_name => $dm_name,
                          %$source_opts
                         );
  }

  $meta->make_immutable if $make_immutable;
  return $meta;
};
sub _compute_source_options {
  my ($self, %opts) = @_;
  my $schema       = delete $opts{schema_class};
  my $source_name  = delete $opts{source_name};
  my $source_class = delete $opts{source_class};
  my $parent       = delete $opts{parent_class};
  my $parent_dm    = delete $opts{parent_domain_model_name};

  #this is the part where I hate my life for promissing all sorts of DWIMery
  confess("parent_class and source_name or source_class are required parameters")
    unless($parent && ($source_name || $source_class));

OUTER: until( $schema && $source_name && $source_class && $parent_dm ){
    if( $schema && !$source_name){
      next OUTER if $source_name = $schema->source($source_class)->source_name;
    } elsif( $schema && !$source_class){
      next OUTER if $source_class = eval { $schema->class($source_name) };
    }

    my @haystack = $parent_dm ? $parent->meta->find_attribute_by_name($parent_dm) : ();

    #there's a lot of guessing going on, but it should work fine on most cases
  INNER: for my $needle (@haystack){
      my $isa = $needle->_isa_metadata;
      next INNER unless Class::MOP::load_class( $isa->_isa_metadata );
      next INNER unless $isa->isa('DBIx::Class::Schema');

      if( $source_name ){
        my $src_class = eval{ $isa->class($source_name) };
        next INNER unless $src_class;
        next INNER if($source_class && $source_class ne $src_class);
        $schema = $isa;
        $parent_dm = $needle->name;
        $source_class = $src_class;
        next OUTER;
      }
    }


    confess("Could not determine options automatically from: schema " .
            "'${schema}', source_name '${source_name}', source_class " .
            "'${source_class}', parent_domain_model_name '${parent_dm}'");
  }

  return {
          source_name  => $source_name,
          schema_class => $schema,
          source_class => $source_class,
          parent_class => $parent,
          parent_domain_model_name => $parent_dm,
         };
};
sub _class_to_attribute_name {
  my ( $self, $str ) = @_;
  confess("wrong arguments passed for _class_to_attribute_name") unless $str;
  return join('_', map lc, split(/::|(?<=[a-z0-9])(?=[A-Z])/, $str))
};
sub add_source {
  my ($self, %opts) = @_;

  my $model      = delete $opts{model_class};
  my $reader     = delete $opts{reader};
  my $source     = delete $opts{source_name};
  my $dm_name    = delete $opts{domain_model_name};
  my $collection = delete $opts{collection_class};
  my $name       = delete $opts{attribute_name} || $source;

  confess("model_class and source_name are required parameters")
    unless $model && $source;
  my $meta = $model->meta;

  unless( $collection ){
    my $object = $self->class_name_from_source_name($model, $source);
    $collection = $self->class_name_for_collection_of($object);
  }
  unless( $reader ){
    $reader = $source;
    $reader =~ s/([a-z0-9])([A-Z])/${1}_${2}/g ;
    $reader = $self->_class_to_attribute_name($reader) . "_collection";
  }
  unless( $dm_name ){
    my @haystack = $meta->domain_models;
    if( @haystack > 1 ){
      @haystack = grep { $_->_isa_metadata->isa('DBIx::Class::Schema') } @haystack;
    }
    if(@haystack == 1){
      $dm_name = $haystack[0]->name;
    } elsif(@haystack > 1){
      confess("Failed to automatically determine domain_model_name. More than one " .
              "possible match (".(join ", ", map{"'".$_->name."'"} @haystack).")");
    } else {
      confess("Failed to automatically determine domain_model_name. No matches.");
    }
  }

  my %attr_opts =
    (
     lazy           => 1,
     required       => 1,
     isa            => $collection,
     reader         => $reader,
     predicate      => "has_" . $self->_class_to_attribute_name($name) ,
     domain_model   => $dm_name,
     orig_attr_name => $source,
     default        => sub {
       $collection->new
         (
          _source_resultset => $_[0]->$dm_name->resultset($source),
          _parent           => $_[0],
         );
     },
    );

  my $make_immutable = $meta->is_immutable;
  $meta->make_mutable   if $make_immutable;
  my $attr = $meta->add_attribute($name, %attr_opts);
  $meta->make_immutable if $make_immutable;

  return $attr;
};
sub reflect_source {
  my ($self, %opts) = @_;
  my $collection  = delete $opts{collection} || {};
  %opts = %{ $self->merge_hashes(\%opts, $self->_compute_source_options(%opts)) };

  my $obj_meta = $self->reflect_source_object(%opts);
  my $col_meta = $self->reflect_source_collection
    (
     object_class => $obj_meta->name,
     source_class => $opts{source_class},
     schema => $opts{schema_class},
     %$collection
    );

  $self->add_source(
                    %opts,
                    model_class       => delete $opts{parent_class},
                    domain_model_name => delete $opts{parent_domain_model_name},
                    collection_class  => $col_meta->name,
                   );
};
sub reflect_source_collection {
  my ($self, %opts) = @_;
  my $base    = delete $opts{base} || ResultSet;
  my $roles   = delete $opts{roles} || [];
  my $class   = delete $opts{class};
  my $object  = delete $opts{object_class};
  my $source  = delete $opts{source_class};
  my $action_rules = delete $opts{actions};
  my $schema = delete $opts{schema};

  confess('object_class and source_class are required parameters')
    unless $object && $source;
  $class ||= $self->class_name_for_collection_of($object);

  Class::MOP::load_class( $base );
  Class::MOP::load_class( $object );

  my $meta = $self->_load_or_create(
    $class,
    superclasses => [$base],
    ( @$roles ? (roles => $roles) : ()),
  );

  my $make_immutable = $meta->is_immutable || $self->make_classes_immutable;;
  $meta->make_mutable if $meta->is_immutable;
  $meta->add_method(_build_member_type => sub{ $object } );
  #XXX as a default pass the domain model as a target_model until i come up with something
  #better through the coercion method
  my $def_act_args = sub {
    my $super = shift;
    return { (target_model => $_[0]->_source_resultset), %{ $super->(@_) } };
  };
  $meta->add_around_method_modifier('_default_action_args_for', $def_act_args);


  {
    my $all_actions = $self->_all_collection_actions;
    my $action_haystack = [keys %$all_actions];
    if(!defined $action_rules){
      $action_rules = $self->default_collection_actions;
    } elsif( (!ref $action_rules && $action_rules) || (ref $action_rules eq 'Regexp') ){
      $action_rules = [ $action_rules ];
    } elsif( ref $action_rules eq 'ARRAY' && @$action_rules){
      #don't add a qr/./ rule if we have at least one match rule
      push(@$action_rules, qr/./)
        unless grep {(ref $_ eq 'ARRAY' && $_->[0] ne '-exclude')
                       || !ref $_  || ref $_ eq 'Regexp'} @$action_rules;
    }

    # XXX this is kind of a dirty hack to support custom actions that are not
    # previously defined and still be able to use the parse_reflect_rules mechanism
    my @custom_actions = grep {!exists $all_actions->{$_}}
      map{ $_->[0] } grep {ref $_ eq 'ARRAY' && $_->[0] ne '-exclude'} @$action_rules;
    push(@$action_haystack, @custom_actions);
    my $actions = $self->parse_reflect_rules($action_rules, $action_haystack);
    for my $action (keys %$actions){
      my $action_opts = $self->merge_hashes
        ($all_actions->{$action} || {}, $actions->{$action} || {});

      #NOTE: If the name of the action is not specified in the prototype then use it's
      #hash key as the name. I think this is sane beahvior, but I've actually been thinking
      #of making Action prototypes their own separate objects
      $self->reflect_source_action(
                                   schema => $schema,
                                   name         => $action,
                                   object_class => $object,
                                   source_class => $source,
                                   %$action_opts,
                                  );

      # XXX i will move this to use the coercion method soon. this will be
      #  GoodEnough until then. I still need to think a little about the type coercion
      #  thing so i don't make a mess of it
      my $act_args = sub {   #override target model for this action
        my $super = shift;
        return { %{ $super->(@_) },
                 ($_[1] eq $action ? (target_model => $_[0]->_source_resultset) : () ) };
      };
      $meta->add_around_method_modifier('_default_action_args_for', $act_args);
    }
  }
  $meta->make_immutable if $make_immutable;
  return $meta;
};
sub reflect_source_object {
  my($self, %opts) = @_;
  %opts = %{ $self->merge_hashes(\%opts, $self->_compute_source_options(%opts)) };

  my $base = delete $opts{base} || Object;
  my $roles = delete $opts{roles} || [];
  my $class = delete $opts{class};
  my $dm_name = delete $opts{domain_model_name};
  my $dm_opts = delete $opts{domain_model_args} || {};

  my $source_name  = delete $opts{source_name};
  my $schema       = delete $opts{schema_class};
  my $source_class = delete $opts{source_class};
  my $parent       = delete $opts{parent_class};
  my $parent_dm    = delete $opts{parent_domain_model_name};

  my $action_rules = delete $opts{actions};
  my $attr_rules   = delete $opts{attributes};

  $class ||= $self->class_name_from_source_name($parent, $source_name);

  Class::MOP::load_class($parent);
  Class::MOP::load_class($schema) if $schema;
  Class::MOP::load_class($source_class);

  my $meta = $self->_load_or_create(
    $class,
    superclasses => [$base],
    ( @$roles ? (roles => $roles) : ()),
  );

  #create the domain model
  $dm_name ||= $self->dm_name_from_source_name($source_name);

  $dm_opts->{isa}        = $source_class;
  $dm_opts->{is}       ||= 'rw';
  $dm_opts->{required} ||= 1;

  my $make_immutable = $meta->is_immutable || $self->make_classes_immutable;;
  $meta->make_mutable if $meta->is_immutable;

  my $dm_attr   = $meta->add_domain_model($dm_name, %$dm_opts);
  my $dm_reader = $dm_attr->get_read_method;

  unless( $class->can('inflate_result') ){
    my $inflate_method = sub {
      my $class = shift; my ($src) = @_;
      $src = $src->resolve if $src->isa('DBIx::Class::ResultSourceHandle');
      $class->new($dm_name, $src->result_class->inflate_result(@_));
    };
    $meta->add_method('inflate_result', $inflate_method);
  }

  #XXX this is here to allow action prototypes to work with ListView
  # maybe Collections hsould have this kind of thing too to allow you to reconstruct them?
  #i like the possibility to be honest... as aset of key/value pairs they could be URId
  #XXX move to using 'handles' for this?
  $meta->add_method('__id', sub {shift->$dm_reader->id} )
    unless $class->can('__id');
  #XXX this one is for Action, ChooseOne and ChooseMany need this shit
  $meta->add_method('__ident_condition', sub {shift->$dm_reader->ident_condition} )
    unless $class->can('__ident_condition');

  #XXX this is just a disaster
  $meta->add_method('display_name', sub {shift->$dm_reader->display_name} )
    if( $source_class->can('display_name') && !$class->can('display_name'));

  #XXX as a default pass the domain model as a target_model until i come up with something
  #better through the coercion method
  my $def_act_args = sub {
    my $super = shift;
    confess "no dm reader: $dm_reader on $_[0]" unless $_[0]->can($dm_reader);
    return { (target_model => $_[0]->$dm_reader), %{ $super->(@_) } };
  };
  $meta->add_around_method_modifier('_default_action_args_for', $def_act_args);

  {
    # attributes => undef,              #default to qr/./
    # attributes => [],                 #default to nothing
    # attributes => qr//,               #DWIM, treated as [qr//]
    # attributes => [{...}]             #DWIM, treat as [qr/./, {...} ]
    # attributes => [[-exclude => ...]] #DWIM, treat as [qr/./, [-exclude => ...]]
    my $attr_haystack =
      [ map { $_->name } $source_class->meta->get_all_attributes ];

    if(!defined $attr_rules){
      $attr_rules = [qr/./];
    } elsif( (!ref $attr_rules && $attr_rules) || (ref $attr_rules eq 'Regexp') ){
      $attr_rules = [ $attr_rules ];
    } elsif( ref $attr_rules eq 'ARRAY' && @$attr_rules){
      #don't add a qr/./ rule if we have at least one match rule
      push(@$attr_rules, qr/./) unless
        grep {(ref $_ eq 'ARRAY' && $_->[0] ne '-exclude')
                || !ref $_  || ref $_ eq 'Regexp'} @$attr_rules;
    }

    my $attributes = $self->parse_reflect_rules($attr_rules, $attr_haystack);
    for my $attr_name (keys %$attributes){
      $self->reflect_source_object_attribute(
                                             schema => $schema,
                                             class             => $class,
                                             source_class      => $source_class,
                                             parent_class      => $parent,
                                             attribute_name    => $attr_name,
                                             domain_model_name => $dm_name,
                                             %{ $attributes->{$attr_name} || {}},
                                            );
    }
  }

  {
    my $all_actions = $self->_all_object_actions;
    my $action_haystack = [keys %$all_actions];
    if(!defined $action_rules){
      $action_rules = $self->default_object_actions;
    } elsif( (!ref $action_rules && $action_rules) || (ref $action_rules eq 'Regexp') ){
      $action_rules = [ $action_rules ];
    } elsif( ref $action_rules eq 'ARRAY' && @$action_rules){
      #don't add a qr/./ rule if we have at least one match rule
      push(@$action_rules, qr/./)
        unless grep {(ref $_ eq 'ARRAY' && $_->[0] ne '-exclude')
                       || !ref $_  || ref $_ eq 'Regexp'} @$action_rules;
    }

    # XXX this is kind of a dirty hack to support custom actions that are not
    # previously defined and still be able to use the parse_reflect_rules mechanism
    my @custom_actions = grep {!exists $all_actions->{$_}} map{ $_->[0] }
      grep {ref $_ eq 'ARRAY' && $_->[0] ne '-exclude'} @$action_rules;
    push(@$action_haystack, @custom_actions);
    my $actions = $self->parse_reflect_rules($action_rules, $action_haystack);
    for my $action (keys %$actions){
      my $action_opts = $self->merge_hashes
        ($all_actions->{$action} || {}, $actions->{$action} || {});

      #NOTE: If the name of the action is not specified in the prototype then use it's
      #hash key as the name. I think this is sane beahvior, but I've actually been thinking
      #of making Action prototypes their own separate objects
      $self->reflect_source_action(
                                   schema => $schema,
                                   name         => $action,
                                   object_class => $class,
                                   source_class => $source_class,
                                   %$action_opts,
                                  );

      # XXX i will move this to use the coercion method soon. this will be
      #  GoodEnough until then. I still need to think a little about the type coercion
      #  thing so i don't make a mess of it
      my $act_args = sub {   #override target model for this action
        my $super = shift;
        confess "no dm reader: $dm_reader on $_[0]" unless $_[0]->can($dm_reader);
        return { %{ $super->(@_) },
                 ($_[1] eq $action ? (target_model => $_[0]->$dm_reader) : () ) };
      };
      $meta->add_around_method_modifier('_default_action_args_for', $act_args);
    }
  }

  $meta->make_immutable if $make_immutable;
  return $meta;
};

# needs class, attribute_name domain_model_name
sub reflect_source_object_attribute {
  my ($self, %opts) = @_;
  unless( $opts{attribute_name} && $opts{class} && $opts{parent_class}
          && ( $opts{source_class} || $opts{domain_model_name} ) ){
    confess( "Error: class, parent_class, attribute_name, and either " .
             "domain_model_name or source_class are required parameters" );
  }

  my $meta =  $opts{class}->meta;
  my $attr_opts = $self->parameters_for_source_object_attribute(%opts);

  my $make_immutable = $meta->is_immutable;
  $meta->make_mutable if $meta->is_immutable;

  my $attr = $meta->add_attribute($opts{attribute_name}, %$attr_opts);

  $meta->make_immutable if $make_immutable;
  return $attr;
};

# needs class, attribute_name domain_model_name
sub parameters_for_source_object_attribute {
  my ($self, %opts) = @_;

  my $class        = delete $opts{class};
  my $attr_name    = delete $opts{attribute_name};
  my $dm_name      = delete $opts{domain_model_name};
  my $source_class = delete $opts{source_class};
  my $parent_class = delete $opts{parent_class};
  my $schema = $opts{schema};
  confess("parent_class is a required argument") unless $parent_class;
  confess("You must supply at least one of domain_model_name and source_class")
    unless $dm_name || $source_class;

  my $source = $schema->source($source_class);
  my $from_attr = $source_class->meta->find_attribute_by_name($attr_name);
  my $reader = $from_attr->get_read_method;
  die("Could not find reader for attribute '$attr_name' on $source_class")
    unless $reader;

  #default options. lazy build but no outsider method
  my %attr_opts = ( is => 'ro', lazy => 1, required => 1,
                    clearer   => "_clear_${attr_name}",
                    predicate => {
                        "has_${attr_name}" =>
                            sub { defined(shift->$dm_name->$reader) }
                    },
                    domain_model   => $dm_name,
                    orig_attr_name => $attr_name,
                  );
  $attr_opts{coerce} = 1 if $from_attr->should_coerce;

  #m2m / has_many
  my $m2m_meta;
  if(my $coderef = $source->result_class->can('_m2m_metadata')){
    $m2m_meta = $source->result_class->$coderef;
  }

  my $constraint_is_ArrayRef =
    $from_attr->type_constraint->name eq 'ArrayRef' ||
      $from_attr->type_constraint->is_subtype_of('ArrayRef');

  if( my $rel_info = $source->relationship_info($attr_name) ){
    my $rel_accessor = $rel_info->{attrs}->{accessor};
    my $rel_moniker  = $schema->source($rel_info->{class})->source_name;

    if($rel_accessor eq 'multi' && $constraint_is_ArrayRef) {
      #has_many
      my $sm = $self->class_name_from_source_name($parent_class, $rel_moniker);
      #type constraint is a collection, and default builds it
      my $isa = $attr_opts{isa} = $self->class_name_for_collection_of($sm);
      $attr_opts{default} = eval "sub {
        my \$rs = shift->${dm_name}->related_resultset('${attr_name}');
        return ${isa}->new(_source_resultset => \$rs);
      }";
    } elsif( $rel_accessor eq 'single' || $rel_accessor eq 'filter' ) {
      #belongs_to
      #type constraint is the foreign IM object, default inflates it
      my $isa = $attr_opts{isa} = $self->class_name_from_source_name($parent_class, $rel_moniker);
      $attr_opts{default} = eval "sub {
        if (defined(my \$o = shift->${dm_name}->${reader})) {
          return ${isa}->inflate_result(\$o->result_source, { \$o->get_columns });
        }
        return undef;
      }";
    }
  } elsif( $constraint_is_ArrayRef && $attr_name =~ m/^(.*)_list$/ ) {
    #m2m magic
    my $mm_name = $1;
    my $link_table = "links_to_${mm_name}_list";
    my ($hm_source, $far_side);
    eval { $hm_source = $source->related_source($link_table); }
      || confess "Can't find ${link_table} has_many for ${mm_name}_list";
    eval { $far_side = $hm_source->related_source($mm_name); }
      || confess "Can't find ${mm_name} belongs_to on ".$hm_source->result_class
        ." traversing many-many for ${mm_name}_list";

    my $sm = $self->class_name_from_source_name($parent_class,$far_side->source_name);
    my $isa = $attr_opts{isa} = $self->class_name_for_collection_of($sm);

    #proper collections will remove the result_class uglyness.
    $attr_opts{default} = eval "sub {
      my \$rs = shift->${dm_name}->related_resultset('${link_table}')->related_resultset('${mm_name}');
      return ${isa}->new(_source_resultset => \$rs);
    }";
  } elsif( $constraint_is_ArrayRef && defined $m2m_meta && exists $m2m_meta->{$attr_name} ){
    #m2m if using introspectable m2m component
    my $rel = $m2m_meta->{$attr_name}->{relation};
    my $far_rel   = $m2m_meta->{$attr_name}->{foreign_relation};
    my $far_source = $source->related_source($rel)->related_source($far_rel);
    my $sm = $self->class_name_from_source_name($parent_class, $far_source->source_name);
    my $isa = $attr_opts{isa} = $self->class_name_for_collection_of($sm);

    my $rs_meth = $m2m_meta->{$attr_name}->{rs_method};
    $attr_opts{default} = eval "sub {
      return ${isa}->new(_source_resultset => shift->${dm_name}->${rs_meth});
    }";
  } else {
    #no rel
    $attr_opts{isa} = $from_attr->_isa_metadata;
    my $default_code = "sub{ shift->${dm_name}->${reader} }";
    $attr_opts{default} = eval $default_code;
    die "Could not generate default for attribute, code '$default_code' did not compile with: $@" if $@;
  }
  return \%attr_opts;
};
sub reflect_source_action {
  my($self, %opts) = @_;
  my $name = delete $opts{name};
  my $base = delete $opts{base} || Action;
  my $roles = delete $opts{roles} || [];
  my $class = delete $opts{class};
  my $object = delete $opts{object_class};
  my $source = delete $opts{source_class};
  my $schema = delete $opts{schema};

  confess("name, object_class and source_class are required arguments")
    unless $source && $name && $object;

  my $attr_rules = delete $opts{attributes};
  $class ||= $object->_default_action_class_for($name);

  Class::MOP::load_class( $base   );
  Class::MOP::load_class( $object );
  Class::MOP::load_class( $source );

  #print STDERR "\n\t", ref $attr_rules eq 'ARRAY' ? @$attr_rules : $attr_rules,"\n";
  # attributes => undef,              #default to qr/./
  # attributes => [],                 #default to nothing
  # attributes => qr//,               #DWIM, treated as [qr//]
  # attributes => [{...}]             #DWIM, treat as [qr/./, {...} ]
  # attributes => [[-exclude => ...]] #DWIM, treat as [qr/./, [-exclude => ...]]
  my $attr_haystack = [ map { $_->name } $object->parameter_attributes ];
  if(!defined $attr_rules){
    $attr_rules = [qr/./];
  } elsif( (!ref $attr_rules && $attr_rules) || (ref $attr_rules eq 'Regexp') ){
    $attr_rules = [ $attr_rules ];
  } elsif( ref $attr_rules eq 'ARRAY' && @$attr_rules){
    #don't add a qr/./ rule if we have at least one match rule
    push(@$attr_rules, qr/./) unless
      grep {(ref $_ eq 'ARRAY' && $_->[0] ne '-exclude')
              || !ref $_  || ref $_ eq 'Regexp'} @$attr_rules;
  }

  #print STDERR "${name}\t${class}\t${base}\n";
  #print STDERR "\t${object}\t${source}\n";
  #print STDERR "\t",@$attr_rules,"\n";

  my $o_meta = $object->meta;
  my $s_meta = $source->meta;
  my $attributes = $self->parse_reflect_rules($attr_rules, $attr_haystack);

  #create the class
  my $meta = $self->_load_or_create(
    $class,
    superclasses => [$base],
    ( @$roles ? (roles => $roles) : ()),
  );
  my $make_immutable = $meta->is_immutable || $self->make_classes_immutable;
  $meta->make_mutable if $meta->is_immutable;

  for my $attr_name (keys %$attributes){
    my $attr_opts   = $attributes->{$attr_name} || {};
    my $o_attr      = $o_meta->find_attribute_by_name($attr_name);
    my $s_attr_name = $o_attr->orig_attr_name || $attr_name;
    my $s_attr      = $s_meta->find_attribute_by_name($s_attr_name);
    confess("Unable to find attribute for '${s_attr_name}' via '${source}'")
      unless defined $s_attr;
    next unless $s_attr->get_write_method
      && $s_attr->get_write_method !~ /^_/; #only rw attributes!

    my $attr_params = $self->parameters_for_source_object_action_attribute
      (
       schema => $schema,
       object_class   => $object,
       source_class   => $source,
       attribute_name => $attr_name
      );
    $meta->add_attribute( $attr_name => %$attr_params);
  }

  $meta->make_immutable if $make_immutable;
  return $meta;
};
sub parameters_for_source_object_action_attribute {
  my ($self, %opts) = @_;

  my $object       = delete $opts{object_class};
  my $attr_name    = delete $opts{attribute_name};
  my $source_class = delete $opts{source_class};
  my $schema = delete $opts{schema};
  my $source = $schema->source($source_class);
  confess("object_class and attribute_name are required parameters")
    unless $attr_name && $object;

  my $o_meta  = $object->meta;
  my $dm_name = $o_meta->find_attribute_by_name($attr_name)->domain_model;
  $source_class ||= $o_meta->find_attribute_by_name($dm_name)->_isa_metadata;
  my $from_attr = $source_class->meta->find_attribute_by_name($attr_name);

  #print STDERR "$attr_name is type: " . $from_attr->meta->name . "\n";

  confess("${attr_name} is not writeable and can not be reflected")
    unless $from_attr->get_write_method;

  my %attr_opts = (
                   is        => 'rw',
                   isa       => $from_attr->_isa_metadata,
                   required  => $from_attr->is_required,
                   ($from_attr->is_required
                     ? () : (clearer => "clear_${attr_name}")),
                   predicate => "has_${attr_name}",
                  );

  if ($attr_opts{required}) {
      if($from_attr->has_default) {
        $attr_opts{lazy} = 1;
        $attr_opts{default} = $from_attr->default;
      } else {
        $attr_opts{lazy_fail} = 1;
      }
  }


  my $m2m_meta;
  if(my $coderef = $source_class->result_class->can('_m2m_metadata')){
    $m2m_meta = $source_class->result_class->$coderef;
  }
  #test for relationships
  my $constraint_is_ArrayRef =
    $from_attr->type_constraint->name eq 'ArrayRef' ||
      $from_attr->type_constraint->is_subtype_of('ArrayRef');

  if (my $rel_info = $source->relationship_info($attr_name)) {
    my $rel_accessor = $rel_info->{attrs}->{accessor};

    if($rel_accessor eq 'multi' && $constraint_is_ArrayRef) {
      confess "${attr_name} is a rw has_many, this won't work.";
    } elsif( $rel_accessor eq 'single' || $rel_accessor eq 'filter') {
      $attr_opts{valid_values} = sub {
        shift->target_model->result_source->related_source($attr_name)->resultset;
      };
    }
  } elsif ( $constraint_is_ArrayRef && $attr_name =~ m/^(.*)_list$/) {
    my $mm_name = $1;
    my $link_table = "links_to_${mm_name}_list";
    $attr_opts{default} = sub { [] };
    $attr_opts{valid_values} = sub {
      shift->target_model->result_source->related_source($link_table)
        ->related_source($mm_name)->resultset;
    };
  } elsif( $constraint_is_ArrayRef && defined $m2m_meta && exists $m2m_meta->{$attr_name} ){
    #m2m if using introspectable m2m component
    my $rel = $m2m_meta->{$attr_name}->{relation};
    my $far_rel   = $m2m_meta->{$attr_name}->{foreign_relation};
    $attr_opts{default} = sub { [] };
    $attr_opts{valid_values} = sub {
      shift->target_model->result_source->related_source($rel)
        ->related_source($far_rel)->resultset;
    };
  }
  #use Data::Dumper;
  #print STDERR "\n" .$attr_name ." - ". $object . "\n";
  #print STDERR Dumper(\%attr_opts);
  return \%attr_opts;
};

sub _load_or_create {
  my ($self, $class, %options) = @_;

  if( $self->_maybe_load_class($class) ){
    return $class->meta;
  }
  my $base;
  if( exists $options{superclasses} ){
    ($base) = @{ $options{superclasses} };
  } else {
    $base = 'Reaction::InterfaceModel::Action';
  }
  return $base->meta->create($class, %options);
}

sub _maybe_load_class {
  my ($self, $class) = @_;
  my $file = $class . '.pm';
  $file =~ s{::}{/}g;
  my $ret = eval { Class::MOP::load_class($class) };
  if ($INC{$file} && $@) {
    confess "Error loading ${class}: $@";
  }
  return $ret;
}

__PACKAGE__->meta->make_immutable;


1;

#--------#---------#---------#---------#---------#---------#---------#---------#
__END__;

=head1 NAME

Reaction::InterfaceModel::Reflector::DBIC -
Automatically Generate InterfaceModels from DBIx::Class models

=head1 DESCRIPTION

The InterfaceModel reflectors are classes that are meant to aid you in easily
generating Reaction::InterfaceModel classes that represent their underlying
DBIx::Class domain models by introspecting your L<DBIx::Class::ResultSource>s
and creating a collection of L<Reaction::InterfaceModel::Object> and
L<Reaction::InterfaceModel::Collection> classes for you to use.

The default base class of all Object classes will be
 L<Reaction::InterfaceModel::Object> and the default Collection type will be
L<Reaction::InterfaceModel::Collection::Virtual::ResultSet>.

Additionally, the reflector can create InterfaceModel actions that interact
with the supplied L<Reaction::UI::Controller::Collection::CRUD>, allowing you
to easily set up a highly customizable CRUD interface in minimal time.

At this time, supported collection actions consist of:

=over 4

=item B<> L<Reaction::INterfaceModel::Action::DBIC::ResultSet::Create>

Creates a new item in the collection and underlying ResultSet.

=item B<> L<Reaction::INterfaceModel::Action::DBIC::ResultSet::DeleteAll>

Deletes all the items in a collection and it's underlying resultset using
C<delete_all>

=back

And supported object actions are :

=over 4

=item B<Update> - via L<Reaction::INterfaceModel::Action::DBIC::Result::Update>

Updates an existing object.

=item B<Delete> - via L<Reaction::INterfaceModel::Action::DBIC::Result::Delete>

Deletes an existing object.

=back

=head1 SYNOPSIS

    package MyApp::IM::TestModel;
    use base 'Reaction::InterfaceModel::Object';
    use Reaction::Class;
    use Reaction::InterfaceModel::Reflector::DBIC;
    my $reflector = Reaction::InterfaceModel::Reflector::DBIC->new;

    #Reflect everything
    $reflector->reflect_schema
      (
       model_class  => __PACKAGE__,
       schema_class => 'MyApp::Schema',
      );

=head2 Selectively including and excluding sources

    #reflect everything except for the FooBar and FooBaz classes
    $reflector->reflect_schema
      (
       model_class  => __PACKAGE__,
       schema_class => 'MyApp::Schema',
       sources => [-exclude => [qw/FooBar FooBaz/] ],
       # you could also do:
       sources => [-exclude => qr/(?:FooBar|FooBaz)/,
       # or even
       sources => [-exclude => [qr/FooBar/, qr/FooBaz/],
      );

    #reflect only the Foo family of sources
    $reflector->reflect_schema
      (
       model_class  => __PACKAGE__,
       schema_class => 'MyApp::Schema',
       sources => qr/^Foo/,
      );

=head2 Selectively including and excluding fields in sources

    #Reflect Foo and Baz in their entirety and exclude the field 'avatar' in the Bar ResultSource
    $reflector->reflect_schema
      (
       model_class  => __PACKAGE__,
       schema_class => 'MyApp::Schema',
       sources => [qw/Foo Baz/,
                   [ Bar => {attributes => [[-exclude => 'avatar']] } ],
                   # or exclude by regex
                   [ Bar => {attributes => [-exclude => qr/avatar/] } ],
                   # or simply do not include it...
                   [ Bar => {attributes => [qw/id name description/] } ],
                  ],
      );

=head1 ATTRIBUTES

=head2 make_classes_immutable

=head2 object_actions

=head2 collection_actions

=head2 default_object_actions

=head2 default_collection_actions

=head2 builtin_object_actions

=head2 builtin_collection_actions

=head1 METHODS

=head2 new

=head2 _all_object_actions

=head2 _all_collection_actions

=head2 dm_name_from_class_name

=head2 dm_name_from_source_name

=head2 class_name_from_source_name

=head2 class_name_for_collection_of

=head2 merge_hashes

=head2 parse_reflect_rules

=head2 merge_reflect_rules

=head2 reflect_schema

=head2 _compute_source_options

=head2 add_source

=head2 reflect_source

=head2 reflect_source_collection

=head2 reflect_source_object

=head2 reflect_source_object_attribute

=head2 parameters_for_source_object_attribute

=head2 reflect_source_action

=head2 parameters_for_source_object_action_attribute

=head1 TODO

Allow the reflector to dump the generated code out as files, eliminating the need to
reflect on startup every time. This will likely take quite a bit of work though. The
main work is already in place, but the grunt work is still left. At the moment there
is no closures that can't be dumped out as code with a little bit of work.

=head1 AUTHORS

See L<Reaction::Class> for authors.

=head1 LICENSE

See L<Reaction::Class> for the license.

=cut