Class: Module

Inherits:
Object show all
Defined in:
lib/traits.rb,
lib/traits-0.10.0.rb

Instance Method Summary collapse

Instance Method Details

#__trait_define_class_reader_traits(*args, &block) ⇒ Object



349
350
351
352
353
# File 'lib/traits.rb', line 349

def __trait_define_class_reader_traits(*args, &block)
#--{{{
  __trait_define_singleton_reader_traits(*args, &block)
#--}}}
end

#__trait_define_class_traits(*args, &block) ⇒ Object



389
390
391
392
393
# File 'lib/traits.rb', line 389

def __trait_define_class_traits(*args, &block)
#--{{{
  __trait_define_singleton_traits(*args, &block)
#--}}}
end

#__trait_define_class_writer_traits(*args, &block) ⇒ Object



369
370
371
372
373
# File 'lib/traits.rb', line 369

def __trait_define_class_writer_traits(*args, &block)
#--{{{
  __trait_define_singleton_writer_traits(*args, &block)
#--}}}
end

#__trait_define_instance_reader_traits(*args, &block) ⇒ Object



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/traits.rb', line 272

def __trait_define_instance_reader_traits(*args, &block)
#--{{{
  if __trait_singleton?
    return(__trait_singleton_super.__trait_define_singleton_reader_traits(*args, &block))
  end
  argf = __trait_arg_filter args, &block
  defaults = __trait_instance_method_defaults
  list = __trait_instance_method_list 
  mhooks = __trait_instance_method_hooks
  argf['names_and_defaults'].each do |name, default|
    __trait_define_reader_trait name, default, defaults, list
  end
  argf['names_and_hooks'].each{|name, hooks| mhooks[name].update hooks}
  __trait_search_path.map{|ta| ta.__trait_instance_method_list['readers']}.flatten
#--}}}
end

#__trait_define_instance_traits(*args, &block) ⇒ Object



332
333
334
335
336
337
338
339
340
# File 'lib/traits.rb', line 332

def __trait_define_instance_traits(*args, &block)
#--{{{
  writers = __trait_define_instance_writer_traits(*args, &block)
  readers = __trait_define_instance_reader_traits(*args, &block)
  #[readers, writers]
  wr = writers.inject({}){|h,k| h.update k.delete('=') => k}
  readers.map{|r| [r, wr[r]]}
#--}}}
end

#__trait_define_instance_writer_traits(*args, &block) ⇒ Object



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/traits.rb', line 302

def __trait_define_instance_writer_traits(*args, &block)
#--{{{
  if __trait_singleton?
    return(__trait_singleton_super.__trait_define_singleton_writer_traits(*args, &block))
  end
  argf = __trait_arg_filter args, &block
  defaults = __trait_instance_method_defaults
  list = __trait_instance_method_list 
  mhooks = __trait_instance_method_hooks
  argf['names_and_defaults'].each do |name, default|
    __trait_define_writer_trait name, default, defaults, list
  end
  argf['names_and_hooks'].each{|name, hooks| mhooks[name].update hooks}
  __trait_search_path.map{|ta| ta.__trait_instance_method_list['writers']}.flatten
#--}}}
end

#__trait_define_reader_trait(name, default, defaults, list) ⇒ Object



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
# File 'lib/traits.rb', line 401

def __trait_define_reader_trait name, default, defaults, list
#--{{{
    getter = "#{ name }"
    setter = "#{ name }="
    query = "#{ name }?"
    toggle = "#{ name }!"

    defaults[getter] = default if default
    list['readers'] << getter 
    list['readers'].uniq!
    # list['readers'].sort!

    unless instance_methods.include? getter
      code = __trait_gen_reader_code name, 'public'
      __trait_module_eval code
    end
    unless instance_methods.include? setter
      code = __trait_gen_writer_code name, 'private'
      __trait_module_eval code
    end
    unless instance_methods.include? query
      code = __trait_gen_query_code name, 'public'
      __trait_module_eval code
    end
    unless instance_methods.include? toggle
      code = __trait_gen_toggle_code name, 'private'
      __trait_module_eval code
    end
#--}}}
end

#__trait_define_writer_trait(name, default, defaults, list) ⇒ Object



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
# File 'lib/traits.rb', line 432

def __trait_define_writer_trait name, default, defaults, list
#--{{{
    reader = "#{ name }"
    writer = "#{ name }="
    query = "#{ name }?"
    toggle = "#{ name }!"

    defaults[reader] = default if default
    list['writers'] << writer 
    list['writers'].uniq!
    # list['writers'].sort!

    unless instance_methods.include? reader
      code = __trait_gen_reader_code name, 'private'
      __trait_module_eval code
    end
    unless instance_methods.include? writer
      code = __trait_gen_writer_code name, 'public'
      __trait_module_eval code
    end
    unless instance_methods.include? query
      code = __trait_gen_query_code name, 'private'
      __trait_module_eval code
    end
    unless instance_methods.include? toggle
      code = __trait_gen_toggle_code name, 'public'
      __trait_module_eval code
    end
#--}}}
end

#__trait_gen_access_protection_code(name, access_protection = nil) ⇒ Object



860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
# File 'lib/traits.rb', line 860

def __trait_gen_access_protection_code name, access_protection = nil 
#--{{{
  access_protection ||= 'public' 
  access_protection_code =
    case access_protection
      when %r/private/
        "private '#{ name }'.intern"
      when %r/protected/
        "protected '#{ name }'.intern"
      else
        "public '#{ name }'.intern"
    end
  puts access_protection_code if Traits::DEBUG
  access_protection_code
#--}}}
end

#__trait_gen_query_code(name, access_protection = 'public') ⇒ Object



665
666
667
668
669
670
671
672
673
674
675
676
# File 'lib/traits.rb', line 665

def __trait_gen_query_code name, access_protection = 'public' 
#--{{{
  query_code = <<-code
    def #{ name }?
      send('#{ name }') ? true : false
    end
    # #{ access_protection } '#{ name }?'.intern
  code
  puts query_code if Traits::DEBUG
  query_code
#--}}}
end

#__trait_gen_reader_code(name, access_protection = 'public') ⇒ Object



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
# File 'lib/traits.rb', line 463

def __trait_gen_reader_code name, access_protection = 'public' 
#--{{{
  s = __trait_singleton?
  klass = s ? 'self' : 'self.class'
  defaults = s ? '__trait_singleton_method_defaults' : '__trait_instance_method_defaults'
  search_ancestors = s ? 'true' : 'false'

  getter_meth = "#{ name }"
  setter_meth = "#{ name }="
  query_meth = "#{ name }?"
  ivar = "@#{ name }"
  getter = "'#{ name }'"
  setter = "'#{ name }='"

  reader_code = <<-code
    def #{ name }(*a, &b)
      unless a.empty?
        send('#{ name }=', *a, &b)
      else
        #unless(defined?(@________#{ name }_set) and @________#{ name }_set)
        unless defined? @#{ name }
          #{ klass }::__trait_search_path.each do |obj|
              defaults = obj.#{ defaults }
              if defaults.has_key? '#{ name }'
                d = defaults['#{ name }']
                if d.respond_to? '__trait_default'
                  d = instance_eval &d
                end
                return(send('#{ name }=', d))
              end
          end
        end
        case self
          when Class
            if defined? @#{ name }
              @#{ name }
            else
              __trait_search_path.each do |obj|
                if self != obj and obj.respond_to? '#{ name }'
                  return(obj.send('#{ name }', *a,  &b))
                end
              end
              return nil
            end
          else
            defined? @#{ name } and @#{ name }
        end
      end
    end
    #{ access_protection } '#{ name }'.intern
  code
  puts reader_code if Traits::DEBUG
  reader_code
#--}}}
end

#__trait_gen_toggle_code(name, access_protection = 'public') ⇒ Object



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
# File 'lib/traits.rb', line 678

def __trait_gen_toggle_code name, access_protection = 'public' 
#--{{{
  s = __trait_singleton?
  klass = s ? 'self' : 'self.class'
  hooks = s ? '__trait_singleton_method_hooks' : '__trait_instance_method_hooks'

  writer_code = <<-code
    def #{ name }!
      hooks = {}
      hook_types = %w( pre munge cast type ducktype validate post toggle )

      #{ klass }::__trait_search_path.each do |obj|

        break if hooks.values_at(*hook_types).compact.size == hook_types.size

        hook_types.each{|ht| hooks[ht] ||= obj.#{ hooks }['#{ name }'][ht]}

      end

      pre_hook, munge_hook, cast_hook, type_hook, ducktype_hook, validate_hook, post_hook, toggle_hook = 
        hooks.values_at(*hook_types) 

      toggle =
        if defined? @________#{ name }_toggle
          @________#{ name }_toggle
        else
          if toggle_hook
            case toggle_hook
              when Proc
                t = Object.new
                sc = class << t 
                  self
                end
                sc.module_eval{
                  define_method 'shift' do
                    toggle_hook.call
                  end
                  define_method 'push' do
                  end
                }
                t
              when Enumerable
                toggle_hook.to_a
              when NilClass, TrueClass 
                @________#{ name }_toggle = [true, false] 
              else
                if toggle_hook.respond_to?('shift') and toggle_hook.respond_to?('push')
                  toggle_hook
                else
                  raise "bad toggle <\#{ toggle_hook.inspect }>"
                end
            end
          else
            @________#{ name }_toggle = [true, false] 
          end
        end

      value = toggle.shift
      toggle.push value

      if pre_hook
        [ pre_hook ].flatten.each do |pre|
          if Proc === pre 
            case pre.arity
              when 0
                __trait_evaluate &pre
              when 1
                __trait_evaluate value, &pre
              else
                __trait_evaluate "#{ name }", value, &pre
            end
          else
            case method("\#{ pre }").arity
              when 0
                send "\#{ pre }"
              when 1
                send "\#{ pre }", value
              else
                send "\#{ pre }", "#{ name }", value
            end
          end
        end
      end

      if cast_hook
        [ cast_hook ].flatten.each do |cast|
          value =
            if Proc === cast
              __trait_evaluate value, &cast
            else
              send cast, value
            end
        end
      end

      if munge_hook
        [ munge_hook ].flatten.each do |munge|
          value =
            if Proc === munge
              __trait_evaluate value, &munge
            else
              value.send munge
            end
        end
      end

      if type_hook
        [ type_hook ].flatten.each do |type|
          is_valid = 
            if Proc === type
              __trait_evaluate(value, &type) === value
            else
              type === value
            end
          raise ArgumentError, 
            "validation of <\#{ value.inspect }> failed!" unless is_valid
        end
      end

      if ducktype_hook
        [ ducktype_hook ].flatten.each do |ducktype|
          is_valid = 
            if Proc === ducktype
              value.respond_to? __trait_evaluate(value, &ducktype)
            else
              value.respond_to? ducktype
            end
          raise ArgumentError, 
            "validation of <\#{ value.inspect }> failed!" unless is_valid
        end
      end

      if validate_hook
        [ validate_hook ].flatten.each do |validate|
          is_valid = 
            if Proc === validate
              __trait_evaluate value, &validate
            else
              send "\#{ validate }", value
            end
          raise ArgumentError, 
            "validation of <\#{ value.inspect }> failed!" unless is_valid
        end
      end

      @#{ name } = value 

      #@________#{ name }_set = true 

      if post_hook
        [ post_hook ].flatten.each do |post|
          if Proc === post 
            case post.arity
              when 0
                __trait_evaluate &post
              when 1
                __trait_evaluate value, &post
              else
                __trait_evaluate "#{ name }", value, &post
            end
          else
            case method("\#{ post }").arity
              when 0
                send "\#{ post }"
              when 1
                send "\#{ post }", value
              else
                send "\#{ post }", "#{ name }", value
            end
          end
        end
      end

      @#{ name }
    end
    #{ access_protection } '#{ name }='.intern
  code
  puts writer_code if Traits::DEBUG
  writer_code
#--}}}
end

#__trait_gen_writer_code(name, access_protection = 'public') ⇒ Object



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
# File 'lib/traits.rb', line 519

def __trait_gen_writer_code name, access_protection = 'public' 
#--{{{
  s = __trait_singleton?
  klass = s ? 'self' : 'self.class'
  hooks = s ? '__trait_singleton_method_hooks' : '__trait_instance_method_hooks'

  writer_code = <<-code
    def #{ name }=(head, *tail)
      value = tail.empty? ? head : [head] + tail

      hooks = {}
      hook_types = %w( pre munge cast type ducktype validate post )

      #{ klass }::__trait_search_path.each do |obj|

        break if hooks.values_at(*hook_types).compact.size == hook_types.size

        hook_types.each{|ht| hooks[ht] ||= obj.#{ hooks }['#{ name }'][ht]}

      end

      pre_hook, munge_hook, cast_hook, type_hook, ducktype_hook, validate_hook, post_hook = 
        hooks.values_at(*hook_types) 

      if pre_hook
        [ pre_hook ].flatten.each do |pre|
          if Proc === pre 
            case pre.arity
              when 0
                __trait_evaluate &pre
              when 1
                __trait_evaluate value, &pre
              else
                __trait_evaluate "#{ name }", value, &pre
            end
          else
            case method("\#{ pre }").arity
              when 0
                send "\#{ pre }"
              when 1
                send "\#{ pre }", value
              else
                send "\#{ pre }", "#{ name }", value
            end
          end
        end
      end

      if cast_hook
        [ cast_hook ].flatten.each do |cast|
          value =
            if Proc === cast
              __trait_evaluate value, &cast
            else
              send cast, value
            end
        end
      end

      if munge_hook
        [ munge_hook ].flatten.each do |munge|
          value =
            if Proc === munge
              __trait_evaluate value, &munge
            else
              value.send munge
            end
        end
      end

      if type_hook
        [ type_hook ].flatten.each do |type|
          is_valid = 
            if Proc === type
              __trait_evaluate(value, &type) === value
            else
              type === value
            end
          raise ArgumentError, 
            "validation of <\#{ value.inspect }> failed!" unless is_valid
        end
      end

      if ducktype_hook
        [ ducktype_hook ].flatten.each do |ducktype|
          is_valid = 
            if Proc === ducktype
              value.respond_to? __trait_evaluate(value, &ducktype)
            else
              value.respond_to? ducktype
            end
          raise ArgumentError, 
            "validation of <\#{ value.inspect }> failed!" unless is_valid
        end
      end

      if validate_hook
        [ validate_hook ].flatten.each do |validate|
          is_valid = 
            if Proc === validate
              __trait_evaluate value, &validate
            else
              send "\#{ validate }", value
            end
          raise ArgumentError, 
            "validation of <\#{ value.inspect }> failed!" unless is_valid
        end
      end

      @#{ name } = value 

      #@________#{ name }_set = true 

      if post_hook
        [ post_hook ].flatten.each do |post|
          if Proc === post 
            case post.arity
              when 0
                __trait_evaluate &post
              when 1
                __trait_evaluate value, &post
              else
                __trait_evaluate "#{ name }", value, &post
            end
          else
            case method("\#{ post }").arity
              when 0
                send "\#{ post }"
              when 1
                send "\#{ post }", value
              else
                send "\#{ post }", "#{ name }", value
            end
          end
        end
      end

      @#{ name }
    end
    #{ access_protection } '#{ name }='.intern
  code
  puts writer_code if Traits::DEBUG
  writer_code
#--}}}
end

#__trait_module_eval(*a, &b) ⇒ Object

–{{{



261
262
263
264
265
266
267
268
269
270
# File 'lib/traits.rb', line 261

def __trait_module_eval(*a, &b)
#--{{{
  begin
    module_eval(*a, &b)
  rescue Exception => e
    STDERR.puts([a, b].inspect) if Traits::DEBUG
    raise
  end
#--}}}
end