classx 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/ChangeLog +354 -0
- data/README +5 -0
- data/Rakefile +2 -2
- data/bench/attribute_get.rb +73 -0
- data/bench/attribute_set.rb +53 -19
- data/bench/define_attribute.rb +226 -0
- data/bench/initialize.rb +25 -7
- data/doc/output/coverage/index.html +74 -128
- data/doc/output/coverage/lib-classx-attribute_rb.html +291 -190
- data/doc/output/coverage/lib-classx-attributes_rb.html +167 -101
- data/doc/output/coverage/lib-classx-bracketable_rb.html +671 -0
- data/doc/output/coverage/lib-classx-class_attributes_rb.html +775 -0
- data/doc/output/coverage/lib-classx-commandable_rb.html +727 -0
- data/doc/output/coverage/{-Library-Ruby-Gems-gems-diff-lcs-1_1_2-lib-diff-lcs-block_rb.html → lib-classx-declare_rb.html} +60 -62
- data/doc/output/coverage/lib-classx-validate_rb.html +43 -41
- data/doc/output/coverage/lib-classx_rb.html +208 -78
- data/example/commandable.rb +1 -0
- data/lib/classx.rb +146 -16
- data/lib/classx/attribute.rb +244 -143
- data/lib/classx/attributes.rb +93 -27
- data/lib/classx/bracketable.rb +61 -0
- data/lib/classx/class_attributes.rb +165 -0
- data/lib/classx/commandable.rb +55 -4
- data/lib/classx/declare.rb +40 -3
- data/lib/classx/role/logger.rb +17 -13
- data/lib/classx/validate.rb +26 -24
- data/spec/classx/handles_spec.rb +21 -6
- data/spec/classx/serialize_spec.rb +57 -0
- data/spec/classx/{with_coerce.rb → with_coerce_spec.rb} +0 -0
- data/spec/classx/with_extend_spec.rb +58 -0
- data/spec/classx/with_include_spec.rb +58 -0
- data/spec/classx/with_validate_spec.rb +363 -0
- data/spec/classx/without_anyoption_spec.rb +1 -1
- data/spec/classx/writable_option_spec.rb +29 -4
- data/spec/classx_bracketable_spec.rb +160 -0
- data/spec/classx_class_attributes/default_option_spec.rb +121 -0
- data/spec/classx_class_attributes/dsl_accessor_spec.rb +88 -0
- data/spec/classx_class_attributes/handles_spec.rb +77 -0
- data/spec/classx_class_attributes/with_coerce_spec.rb +119 -0
- data/spec/classx_class_attributes/with_extend_spec.rb +64 -0
- data/spec/classx_class_attributes/with_include_spec.rb +64 -0
- data/spec/classx_class_attributes/with_multiple_class_spec.rb +60 -0
- data/spec/classx_class_attributes/with_validate_spec.rb +248 -0
- data/spec/classx_class_attributes/without_accessor_spec.rb +19 -0
- data/spec/classx_class_attributes/without_anyoption_spec.rb +21 -0
- data/spec/classx_class_attributes/writable_option_spec.rb +100 -0
- data/spec/classx_declare_spec.rb +117 -0
- data/spec/classx_validate_spec.rb +1 -3
- data/tasks/basic_tasks.rake +1 -1
- metadata +36 -26
- data/doc/output/coverage/-Library-Ruby-Gems-gems-diff-lcs-1_1_2-lib-diff-lcs-callbacks_rb.html +0 -932
- data/doc/output/coverage/-Library-Ruby-Gems-gems-diff-lcs-1_1_2-lib-diff-lcs-change_rb.html +0 -779
- data/doc/output/coverage/-Library-Ruby-Gems-gems-diff-lcs-1_1_2-lib-diff-lcs-hunk_rb.html +0 -867
- data/doc/output/coverage/-Library-Ruby-Gems-gems-diff-lcs-1_1_2-lib-diff-lcs_rb.html +0 -1715
- data/doc/output/coverage/-Library-Ruby-Gems-gems-rcov-0_8_1_2_0-lib-rcov_rb.html +0 -1598
- data/spec/classx/with_extend.rb +0 -27
- data/spec/classx/with_include.rb +0 -27
@@ -1,5 +1,5 @@
|
|
1
1
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
2
|
-
<html lang='en' xml:lang='en' xmlns='http://www.w3.org/1999/xhtml'><head><title
|
2
|
+
<html lang='en' xml:lang='en' xmlns='http://www.w3.org/1999/xhtml'><head><title>lib/classx/declare.rb - C0 code coverage information</title>
|
3
3
|
<style type='text/css'>body { background-color: rgb(240, 240, 245); }</style>
|
4
4
|
<style type='text/css'>span.cross-ref-title {
|
5
5
|
font-size: 140%;
|
@@ -553,7 +553,7 @@ span.run100 {
|
|
553
553
|
</style>
|
554
554
|
</head>
|
555
555
|
<body><h3>C0 code coverage information</h3>
|
556
|
-
<p>Generated on Sat
|
556
|
+
<p>Generated on Sat Sep 13 00:31:29 +0900 2008 with <a href='http://eigenclass.org/hiki/rcov'>rcov 0.8.1.2</a>
|
557
557
|
</p>
|
558
558
|
<hr/>
|
559
559
|
<pre><span class='marked0'>Code reported as executed by Ruby looks like this...
|
@@ -569,26 +569,26 @@ span.run100 {
|
|
569
569
|
<td class='heading'>Code coverage</td>
|
570
570
|
</tr>
|
571
571
|
</thead>
|
572
|
-
<tbody><tr class='light'><td><a href='
|
572
|
+
<tbody><tr class='light'><td><a href='lib-classx-declare_rb.html'>lib/classx/declare.rb</a>
|
573
573
|
</td>
|
574
|
-
<td class='lines_total'><tt>
|
574
|
+
<td class='lines_total'><tt>49</tt>
|
575
575
|
</td>
|
576
|
-
<td class='lines_code'><tt>
|
576
|
+
<td class='lines_code'><tt>26</tt>
|
577
577
|
</td>
|
578
|
-
<td><table cellspacing='0' cellpadding='0' align='right'><tr><td><tt class='coverage_total'>
|
578
|
+
<td><table cellspacing='0' cellpadding='0' align='right'><tr><td><tt class='coverage_total'>93.9%</tt>
|
579
579
|
</td>
|
580
|
-
<td><table cellspacing='0' class='percent_graph' cellpadding='0' width='100'><tr><td class='covered' width='
|
581
|
-
<td class='uncovered' width='
|
580
|
+
<td><table cellspacing='0' class='percent_graph' cellpadding='0' width='100'><tr><td class='covered' width='94'/>
|
581
|
+
<td class='uncovered' width='6'/>
|
582
582
|
</tr>
|
583
583
|
</table>
|
584
584
|
</td>
|
585
585
|
</tr>
|
586
586
|
</table>
|
587
587
|
</td>
|
588
|
-
<td><table cellspacing='0' cellpadding='0' align='right'><tr><td><tt class='coverage_code'>
|
588
|
+
<td><table cellspacing='0' cellpadding='0' align='right'><tr><td><tt class='coverage_code'>88.5%</tt>
|
589
589
|
</td>
|
590
|
-
<td><table cellspacing='0' class='percent_graph' cellpadding='0' width='100'><tr><td class='covered' width='
|
591
|
-
<td class='uncovered' width='
|
590
|
+
<td><table cellspacing='0' class='percent_graph' cellpadding='0' width='100'><tr><td class='covered' width='88'/>
|
591
|
+
<td class='uncovered' width='12'/>
|
592
592
|
</tr>
|
593
593
|
</table>
|
594
594
|
</td>
|
@@ -598,57 +598,55 @@ span.run100 {
|
|
598
598
|
</tr>
|
599
599
|
</tbody>
|
600
600
|
</table>
|
601
|
-
<pre><span class="
|
602
|
-
</span><span class="
|
603
|
-
</span><span class="
|
604
|
-
</span><span class="
|
605
|
-
</span><span class="
|
606
|
-
</span><span class="
|
607
|
-
</span><span class="
|
608
|
-
</span><span class="
|
609
|
-
</span><span class="
|
610
|
-
</span><span class="
|
611
|
-
</span><span class="
|
612
|
-
</span><span class="
|
613
|
-
</span><span class="
|
614
|
-
</span><span class="
|
615
|
-
</span><span class="
|
616
|
-
</span><span class="
|
617
|
-
</span><span class="
|
618
|
-
</span><span class="
|
619
|
-
</span><span class="
|
620
|
-
</span><span class="
|
621
|
-
</span><span class="
|
622
|
-
</span><span class="
|
623
|
-
</span><span class="
|
624
|
-
</span><span class="
|
625
|
-
</span><span class="
|
626
|
-
</span><span class="
|
627
|
-
</span><span class="
|
628
|
-
</span><span class="
|
629
|
-
</span><span class="
|
630
|
-
</span><span class="
|
631
|
-
</span><span class="
|
632
|
-
</span><span class="
|
633
|
-
</span><span class="
|
634
|
-
</span><span class="
|
635
|
-
</span><span class="
|
636
|
-
</span><span class="
|
637
|
-
</span><span class="
|
638
|
-
</span><span class="
|
639
|
-
</span><span class="
|
640
|
-
</span><span class="
|
641
|
-
</span><span class="
|
642
|
-
</span><span class="
|
643
|
-
</span><span class="
|
644
|
-
</span><span class="
|
645
|
-
</span><span class="
|
646
|
-
</span><span class="
|
647
|
-
</span><span class="
|
648
|
-
</span><span class="
|
649
|
-
</span><span class="
|
650
|
-
</span><span class="uncovered1"><a name="line50"></a>50 end
|
651
|
-
</span><span class="uncovered0"><a name="line51"></a>51 end
|
601
|
+
<pre><span class="marked1"><a name="line1"></a> 1 module ClassX
|
602
|
+
</span><span class="inferred0"><a name="line2"></a> 2 # you can define classx base class using DSL
|
603
|
+
</span><span class="inferred1"><a name="line3"></a> 3 #
|
604
|
+
</span><span class="inferred0"><a name="line4"></a> 4 # require 'classx'
|
605
|
+
</span><span class="inferred1"><a name="line5"></a> 5 # require 'classx/declare'
|
606
|
+
</span><span class="inferred0"><a name="line6"></a> 6 # include ClassX::Declare
|
607
|
+
</span><span class="inferred1"><a name="line7"></a> 7 #
|
608
|
+
</span><span class="inferred0"><a name="line8"></a> 8 # classx :Klass do
|
609
|
+
</span><span class="inferred1"><a name="line9"></a> 9 # has :x
|
610
|
+
</span><span class="inferred0"><a name="line10"></a>10 # end
|
611
|
+
</span><span class="inferred1"><a name="line11"></a>11 #
|
612
|
+
</span><span class="inferred0"><a name="line12"></a>12 # Klass.new(:x => 10)
|
613
|
+
</span><span class="inferred1"><a name="line13"></a>13 #
|
614
|
+
</span><span class="inferred0"><a name="line14"></a>14 # or you can define nested.
|
615
|
+
</span><span class="inferred1"><a name="line15"></a>15 #
|
616
|
+
</span><span class="inferred0"><a name="line16"></a>16 # classx :Klass do
|
617
|
+
</span><span class="inferred1"><a name="line17"></a>17 # classx :Klass2 do
|
618
|
+
</span><span class="inferred0"><a name="line18"></a>18 # has :x
|
619
|
+
</span><span class="inferred1"><a name="line19"></a>19 # end
|
620
|
+
</span><span class="inferred0"><a name="line20"></a>20 # end
|
621
|
+
</span><span class="inferred1"><a name="line21"></a>21 #
|
622
|
+
</span><span class="inferred0"><a name="line22"></a>22 # #=> define Klass::Klass2
|
623
|
+
</span><span class="inferred1"><a name="line23"></a>23 #
|
624
|
+
</span><span class="marked0"><a name="line24"></a>24 module Declare
|
625
|
+
</span><span class="marked1"><a name="line25"></a>25 def classx name, options=[] , ctx=_guess_parent_namespace(), &block
|
626
|
+
</span><span class="marked0"><a name="line26"></a>26 options.push(:Declare)
|
627
|
+
</span><span class="marked1"><a name="line27"></a>27 klass = Class.new
|
628
|
+
</span><span class="marked0"><a name="line28"></a>28 klass.class_eval do
|
629
|
+
</span><span class="marked1"><a name="line29"></a>29 options.each do |mod|
|
630
|
+
</span><span class="marked0"><a name="line30"></a>30 __send__ ::ClassX::MODULE_USAGE_MAP_OF[mod], ::ClassX.const_get(mod)
|
631
|
+
</span><span class="inferred1"><a name="line31"></a>31 end
|
632
|
+
</span><span class="marked0"><a name="line32"></a>32 include(ClassX)
|
633
|
+
</span><span class="inferred1"><a name="line33"></a>33 end
|
634
|
+
</span><span class="marked0"><a name="line34"></a>34 klass.class_eval(&block)
|
635
|
+
</span><span class="marked1"><a name="line35"></a>35 ctx.module_eval do
|
636
|
+
</span><span class="marked0"><a name="line36"></a>36 const_set(name.to_s.capitalize, klass)
|
637
|
+
</span><span class="inferred1"><a name="line37"></a>37 end
|
638
|
+
</span><span class="inferred0"><a name="line38"></a>38 end
|
639
|
+
</span><span class="inferred1"><a name="line39"></a>39
|
640
|
+
</span><span class="marked0"><a name="line40"></a>40 private
|
641
|
+
</span><span class="marked1"><a name="line41"></a>41 def _guess_parent_namespace klass=self
|
642
|
+
</span><span class="marked0"><a name="line42"></a>42 if self.respond_to? :module_eval
|
643
|
+
</span><span class="marked1"><a name="line43"></a>43 klass
|
644
|
+
</span><span class="uncovered0"><a name="line44"></a>44 else
|
645
|
+
</span><span class="uncovered1"><a name="line45"></a>45 klass.class
|
646
|
+
</span><span class="uncovered0"><a name="line46"></a>46 end
|
647
|
+
</span><span class="marked1"><a name="line47"></a>47 end
|
648
|
+
</span><span class="inferred0"><a name="line48"></a>48 end
|
649
|
+
</span><span class="inferred1"><a name="line49"></a>49 end
|
652
650
|
</span></pre><hr/>
|
653
651
|
<p>Generated using the <a href='http://eigenclass.org/hiki.rb?rcov'>rcov code coverage analysis tool for Ruby</a>
|
654
652
|
version 0.8.1.2.</p>
|
@@ -553,7 +553,7 @@ span.run100 {
|
|
553
553
|
</style>
|
554
554
|
</head>
|
555
555
|
<body><h3>C0 code coverage information</h3>
|
556
|
-
<p>Generated on Sat
|
556
|
+
<p>Generated on Sat Sep 13 00:31:29 +0900 2008 with <a href='http://eigenclass.org/hiki/rcov'>rcov 0.8.1.2</a>
|
557
557
|
</p>
|
558
558
|
<hr/>
|
559
559
|
<pre><span class='marked0'>Code reported as executed by Ruby looks like this...
|
@@ -571,9 +571,9 @@ span.run100 {
|
|
571
571
|
</thead>
|
572
572
|
<tbody><tr class='light'><td><a href='lib-classx-validate_rb.html'>lib/classx/validate.rb</a>
|
573
573
|
</td>
|
574
|
-
<td class='lines_total'><tt>
|
574
|
+
<td class='lines_total'><tt>40</tt>
|
575
575
|
</td>
|
576
|
-
<td class='lines_code'><tt>
|
576
|
+
<td class='lines_code'><tt>17</tt>
|
577
577
|
</td>
|
578
578
|
<td><table cellspacing='0' cellpadding='0' align='right'><tr><td><tt class='coverage_total'>100.0%</tt>
|
579
579
|
</td>
|
@@ -598,44 +598,46 @@ span.run100 {
|
|
598
598
|
</tr>
|
599
599
|
</tbody>
|
600
600
|
</table>
|
601
|
-
<pre><span class="
|
602
|
-
</span><span class="
|
603
|
-
</span><span class="
|
604
|
-
</span><span class="
|
605
|
-
</span><span class="
|
606
|
-
</span><span class="
|
607
|
-
</span><span class="
|
608
|
-
</span><span class="
|
609
|
-
</span><span class="
|
610
|
-
</span><span class="
|
611
|
-
</span><span class="
|
612
|
-
</span><span class="
|
613
|
-
</span><span class="
|
614
|
-
</span><span class="
|
615
|
-
</span><span class="
|
616
|
-
</span><span class="
|
617
|
-
</span><span class="
|
618
|
-
</span><span class="
|
619
|
-
</span><span class="
|
620
|
-
</span><span class="
|
621
|
-
</span><span class="
|
622
|
-
</span><span class="
|
623
|
-
</span><span class="
|
624
|
-
</span><span class="
|
625
|
-
</span><span class="
|
626
|
-
</span><span class="
|
627
|
-
</span><span class="
|
628
|
-
</span><span class="
|
629
|
-
</span><span class="
|
630
|
-
</span><span class="
|
631
|
-
</span><span class="
|
632
|
-
</span><span class="
|
633
|
-
</span><span class="
|
634
|
-
</span><span class="
|
635
|
-
</span><span class="
|
636
|
-
</span><span class="
|
637
|
-
</span><span class="
|
638
|
-
</span><span class="
|
601
|
+
<pre><span class="inferred0"><a name="line1"></a> 1 #
|
602
|
+
</span><span class="inferred1"><a name="line2"></a> 2 # for validatation Hash parameter declaretively.
|
603
|
+
</span><span class="inferred0"><a name="line3"></a> 3 #
|
604
|
+
</span><span class="inferred1"><a name="line4"></a> 4 # require 'classx/validate'
|
605
|
+
</span><span class="inferred0"><a name="line5"></a> 5 #
|
606
|
+
</span><span class="inferred1"><a name="line6"></a> 6 # class YourClass
|
607
|
+
</span><span class="inferred0"><a name="line7"></a> 7 #
|
608
|
+
</span><span class="inferred1"><a name="line8"></a> 8 # def run params
|
609
|
+
</span><span class="inferred0"><a name="line9"></a> 9 # validated_prams = Class::Validate.validate params do
|
610
|
+
</span><span class="inferred1"><a name="line10"></a>10 # has :x
|
611
|
+
</span><span class="inferred0"><a name="line11"></a>11 # has :y, :default => "hoge", :kind_of => Hash
|
612
|
+
</span><span class="inferred1"><a name="line12"></a>12 # end
|
613
|
+
</span><span class="inferred0"><a name="line13"></a>13 #
|
614
|
+
</span><span class="inferred1"><a name="line14"></a>14 # # do something with params
|
615
|
+
</span><span class="inferred0"><a name="line15"></a>15 #
|
616
|
+
</span><span class="inferred1"><a name="line16"></a>16 # end
|
617
|
+
</span><span class="inferred0"><a name="line17"></a>17 # end
|
618
|
+
</span><span class="inferred1"><a name="line18"></a>18 #
|
619
|
+
</span><span class="inferred0"><a name="line19"></a>19 # YourClass.new.run(:x => 10) # raise ClassX::AttrRequiredError
|
620
|
+
</span><span class="inferred1"><a name="line20"></a>20 #
|
621
|
+
</span><span class="marked0"><a name="line21"></a>21 module ClassX::Validate
|
622
|
+
</span><span class="marked1"><a name="line22"></a>22 private
|
623
|
+
</span><span class="marked0"><a name="line23"></a>23 def validate hash, &block #:doc:
|
624
|
+
</span><span class="inferred1"><a name="line24"></a>24 # FIXME: it's experimental feature for caching validate class.
|
625
|
+
</span><span class="inferred0"><a name="line25"></a>25 # it can use class variable because it use caller[0] as key.
|
626
|
+
</span><span class="marked1"><a name="line26"></a>26 @@__validate_cached ||= {}
|
627
|
+
</span><span class="marked0"><a name="line27"></a>27 uniq_key = caller[0]
|
628
|
+
</span><span class="marked1"><a name="line28"></a>28 unless @@__validate_cached[uniq_key]
|
629
|
+
</span><span class="marked0"><a name="line29"></a>29 @@__validate_cached[uniq_key] = Class.new
|
630
|
+
</span><span class="marked1"><a name="line30"></a>30 @@__validate_cached[uniq_key].class_eval do
|
631
|
+
</span><span class="marked0"><a name="line31"></a>31 include ::ClassX
|
632
|
+
</span><span class="marked1"><a name="line32"></a>32 include ::ClassX::Bracketable
|
633
|
+
</span><span class="inferred0"><a name="line33"></a>33 end
|
634
|
+
</span><span class="marked1"><a name="line34"></a>34 @@__validate_cached[uniq_key].class_eval(&block)
|
635
|
+
</span><span class="inferred0"><a name="line35"></a>35 end
|
636
|
+
</span><span class="marked1"><a name="line36"></a>36 @@__validate_cached[uniq_key].new hash
|
637
|
+
</span><span class="inferred0"><a name="line37"></a>37 end
|
638
|
+
</span><span class="inferred1"><a name="line38"></a>38
|
639
|
+
</span><span class="marked0"><a name="line39"></a>39 module_function :validate
|
640
|
+
</span><span class="inferred1"><a name="line40"></a>40 end
|
639
641
|
</span></pre><hr/>
|
640
642
|
<p>Generated using the <a href='http://eigenclass.org/hiki.rb?rcov'>rcov code coverage analysis tool for Ruby</a>
|
641
643
|
version 0.8.1.2.</p>
|
@@ -553,7 +553,7 @@ span.run100 {
|
|
553
553
|
</style>
|
554
554
|
</head>
|
555
555
|
<body><h3>C0 code coverage information</h3>
|
556
|
-
<p>Generated on Sat
|
556
|
+
<p>Generated on Sat Sep 13 00:31:29 +0900 2008 with <a href='http://eigenclass.org/hiki/rcov'>rcov 0.8.1.2</a>
|
557
557
|
</p>
|
558
558
|
<hr/>
|
559
559
|
<pre><span class='marked0'>Code reported as executed by Ruby looks like this...
|
@@ -571,9 +571,9 @@ span.run100 {
|
|
571
571
|
</thead>
|
572
572
|
<tbody><tr class='light'><td><a href='lib-classx_rb.html'>lib/classx.rb</a>
|
573
573
|
</td>
|
574
|
-
<td class='lines_total'><tt>
|
574
|
+
<td class='lines_total'><tt>205</tt>
|
575
575
|
</td>
|
576
|
-
<td class='lines_code'><tt>
|
576
|
+
<td class='lines_code'><tt>130</tt>
|
577
577
|
</td>
|
578
578
|
<td><table cellspacing='0' cellpadding='0' align='right'><tr><td><tt class='coverage_total'>100.0%</tt>
|
579
579
|
</td>
|
@@ -598,81 +598,211 @@ span.run100 {
|
|
598
598
|
</tr>
|
599
599
|
</tbody>
|
600
600
|
</table>
|
601
|
-
<pre><span class="marked1"><a name="line1"></a>
|
602
|
-
</span><span class="marked0"><a name="line2"></a>
|
603
|
-
</span><span class="inferred1"><a name="line3"></a>
|
604
|
-
</span><span class="
|
605
|
-
</span><span class="
|
606
|
-
</span><span class="
|
607
|
-
</span><span class="
|
608
|
-
</span><span class="
|
609
|
-
</span><span class="
|
610
|
-
</span><span class="
|
611
|
-
</span><span class="inferred1"><a name="line11"></a>11
|
612
|
-
</span><span class="inferred0"><a name="line12"></a>12
|
613
|
-
</span><span class="
|
614
|
-
</span><span class="
|
615
|
-
</span><span class="
|
616
|
-
</span><span class="
|
617
|
-
</span><span class="
|
618
|
-
</span><span class="
|
619
|
-
</span><span class="inferred1"><a name="line19"></a>19
|
620
|
-
</span><span class="
|
621
|
-
</span><span class="marked1"><a name="line21"></a>21
|
622
|
-
</span><span class="marked0"><a name="line22"></a>22
|
623
|
-
</span><span class="
|
624
|
-
</span><span class="marked0"><a name="line24"></a>24
|
625
|
-
</span><span class="marked1"><a name="line25"></a>25
|
626
|
-
</span><span class="
|
627
|
-
</span><span class="marked1"><a name="line27"></a>27
|
628
|
-
</span><span class="marked0"><a name="line28"></a>28
|
629
|
-
</span><span class="
|
630
|
-
</span><span class="inferred0"><a name="line30"></a>30
|
631
|
-
</span><span class="inferred1"><a name="line31"></a>31
|
632
|
-
</span><span class="marked0"><a name="line32"></a>32
|
633
|
-
</span><span class="
|
634
|
-
</span><span class="
|
635
|
-
</span><span class="inferred1"><a name="line35"></a>35
|
636
|
-
</span><span class="
|
637
|
-
</span><span class="inferred1"><a name="line37"></a>37
|
638
|
-
</span><span class="
|
639
|
-
</span><span class="
|
640
|
-
</span><span class="
|
641
|
-
</span><span class="marked1"><a name="line41"></a>41
|
642
|
-
</span><span class="
|
643
|
-
</span><span class="
|
644
|
-
</span><span class="
|
645
|
-
</span><span class="marked1"><a name="line45"></a>45
|
646
|
-
</span><span class="marked0"><a name="line46"></a>46
|
647
|
-
</span><span class="
|
648
|
-
</span><span class="inferred0"><a name="line48"></a>48
|
649
|
-
</span><span class="inferred1"><a name="line49"></a>49
|
650
|
-
</span><span class="
|
651
|
-
</span><span class="inferred1"><a name="line51"></a>51
|
652
|
-
</span><span class="
|
653
|
-
</span><span class="marked1"><a name="line53"></a>53
|
654
|
-
</span><span class="
|
655
|
-
</span><span class="marked1"><a name="line55"></a>55
|
656
|
-
</span><span class="marked0"><a name="line56"></a>56
|
657
|
-
</span><span class="
|
658
|
-
</span><span class="inferred0"><a name="line58"></a>58
|
659
|
-
</span><span class="inferred1"><a name="line59"></a>59
|
660
|
-
</span><span class="
|
661
|
-
</span><span class="marked1"><a name="line61"></a>61 hash
|
662
|
-
</span><span class="
|
663
|
-
</span><span class="inferred1"><a name="line63"></a>63
|
664
|
-
</span><span class="
|
665
|
-
</span><span class="
|
666
|
-
</span><span class="marked0"><a name="line66"></a>66
|
667
|
-
</span><span class="
|
668
|
-
</span><span class="marked0"><a name="line68"></a>68
|
669
|
-
</span><span class="
|
670
|
-
</span><span class="inferred0"><a name="line70"></a>70
|
671
|
-
</span><span class="inferred1"><a name="line71"></a>71
|
672
|
-
</span><span class="marked0"><a name="line72"></a>72
|
673
|
-
</span><span class="marked1"><a name="line73"></a>73
|
674
|
-
</span><span class="
|
675
|
-
</span><span class="inferred1"><a name="line75"></a>75
|
601
|
+
<pre><span class="marked1"><a name="line1"></a> 1 require 'classx/attribute'
|
602
|
+
</span><span class="marked0"><a name="line2"></a> 2 require 'classx/attributes'
|
603
|
+
</span><span class="inferred1"><a name="line3"></a> 3
|
604
|
+
</span><span class="inferred0"><a name="line4"></a> 4 # usage
|
605
|
+
</span><span class="inferred1"><a name="line5"></a> 5 #
|
606
|
+
</span><span class="inferred0"><a name="line6"></a> 6 # require 'classx'
|
607
|
+
</span><span class="inferred1"><a name="line7"></a> 7 # class Point
|
608
|
+
</span><span class="inferred0"><a name="line8"></a> 8 # include ClassX
|
609
|
+
</span><span class="inferred1"><a name="line9"></a> 9 #
|
610
|
+
</span><span class="inferred0"><a name="line10"></a> 10 # has :x, :kind_of => Fixnum
|
611
|
+
</span><span class="inferred1"><a name="line11"></a> 11 # has :y, :kind_of => Fixnum
|
612
|
+
</span><span class="inferred0"><a name="line12"></a> 12 # end
|
613
|
+
</span><span class="inferred1"><a name="line13"></a> 13 #
|
614
|
+
</span><span class="inferred0"><a name="line14"></a> 14 # class Point3D < Point
|
615
|
+
</span><span class="inferred1"><a name="line15"></a> 15 # has :z, :writable => true, :kind_of => Fixnum, :optional => true
|
616
|
+
</span><span class="inferred0"><a name="line16"></a> 16 # end
|
617
|
+
</span><span class="inferred1"><a name="line17"></a> 17 #
|
618
|
+
</span><span class="inferred0"><a name="line18"></a> 18 # Point.new(:x => 30, :y => 40) #=> <# Point @x=30, @y=40 >
|
619
|
+
</span><span class="inferred1"><a name="line19"></a> 19 # point3d = Point3D.new(:x => 30, :y => 40, :z => 50) #=> <# Point3D @x=30, @y=40, @z=50 >
|
620
|
+
</span><span class="inferred0"><a name="line20"></a> 20 # point3d.z = 60.0 # raise ClassX::InvalidAttrArgument
|
621
|
+
</span><span class="marked1"><a name="line21"></a> 21 module ClassX
|
622
|
+
</span><span class="marked0"><a name="line22"></a> 22 autoload :ClassAttributes, 'classx/class_attributes'
|
623
|
+
</span><span class="marked1"><a name="line23"></a> 23 autoload :CAttrs, 'classx/class_attributes'
|
624
|
+
</span><span class="marked0"><a name="line24"></a> 24 autoload :Validate, 'classx/validate'
|
625
|
+
</span><span class="marked1"><a name="line25"></a> 25 autoload :Commandable, 'classx/commandable'
|
626
|
+
</span><span class="marked0"><a name="line26"></a> 26 autoload :Declare, 'classx/declare'
|
627
|
+
</span><span class="marked1"><a name="line27"></a> 27 autoload :Bracketable, 'classx/bracketable'
|
628
|
+
</span><span class="marked0"><a name="line28"></a> 28 module Role
|
629
|
+
</span><span class="marked1"><a name="line29"></a> 29 autoload :Logger, 'classx/role/logger'
|
630
|
+
</span><span class="inferred0"><a name="line30"></a> 30 end
|
631
|
+
</span><span class="inferred1"><a name="line31"></a> 31
|
632
|
+
</span><span class="marked0"><a name="line32"></a> 32 MODULE_USAGE_MAP_OF = {
|
633
|
+
</span><span class="inferred1"><a name="line33"></a> 33 :ClassAttributes => :extend,
|
634
|
+
</span><span class="inferred0"><a name="line34"></a> 34 :CAttrs => :extend,
|
635
|
+
</span><span class="inferred1"><a name="line35"></a> 35 :Commandable => :extend,
|
636
|
+
</span><span class="inferred0"><a name="line36"></a> 36 :Declare => :extend,
|
637
|
+
</span><span class="inferred1"><a name="line37"></a> 37 :Bracketable => :include,
|
638
|
+
</span><span class="inferred0"><a name="line38"></a> 38 :Validate => :include,
|
639
|
+
</span><span class="inferred1"><a name="line39"></a> 39 }
|
640
|
+
</span><span class="inferred0"><a name="line40"></a> 40
|
641
|
+
</span><span class="marked1"><a name="line41"></a> 41 class InstanceException < Exception; end
|
642
|
+
</span><span class="marked0"><a name="line42"></a> 42 class AttrRequiredError < InstanceException; end
|
643
|
+
</span><span class="marked1"><a name="line43"></a> 43 class InvalidAttrArgument < InstanceException; end
|
644
|
+
</span><span class="marked0"><a name="line44"></a> 44 class LazyOptionShouldHaveDefault < Exception; end
|
645
|
+
</span><span class="marked1"><a name="line45"></a> 45 class OptionalAttrShouldBeWritable < Exception; end
|
646
|
+
</span><span class="marked0"><a name="line46"></a> 46 class RequiredAttrShouldNotHaveDefault < Exception; end
|
647
|
+
</span><span class="inferred1"><a name="line47"></a> 47
|
648
|
+
</span><span class="inferred0"><a name="line48"></a> 48 # *args is Hash in default.
|
649
|
+
</span><span class="inferred1"><a name="line49"></a> 49 # Hash should have attribute's key and valid value for attribute.
|
650
|
+
</span><span class="inferred0"><a name="line50"></a> 50 # This method checking required value is setted and taking value is valid to attribute.
|
651
|
+
</span><span class="inferred1"><a name="line51"></a> 51 #
|
652
|
+
</span><span class="marked0"><a name="line52"></a> 52 def initialize *args
|
653
|
+
</span><span class="marked1"><a name="line53"></a> 53 hash = before_init(*args)
|
654
|
+
</span><span class="inferred0"><a name="line54"></a> 54
|
655
|
+
</span><span class="marked1"><a name="line55"></a> 55 unless hash && hash.kind_of?(Hash)
|
656
|
+
</span><span class="marked0"><a name="line56"></a> 56 raise ArgumentError, "#{hash.inspect} was wrong as arguments. please specify kind of Hash instance"
|
657
|
+
</span><span class="inferred1"><a name="line57"></a> 57 end
|
658
|
+
</span><span class="inferred0"><a name="line58"></a> 58
|
659
|
+
</span><span class="inferred1"><a name="line59"></a> 59 # allow String or Symbol for key
|
660
|
+
</span><span class="marked0"><a name="line60"></a> 60 tmp_hash = {}
|
661
|
+
</span><span class="marked1"><a name="line61"></a> 61 hash.each do |key,val|
|
662
|
+
</span><span class="marked0"><a name="line62"></a> 62 tmp_hash[key.to_s] = val
|
663
|
+
</span><span class="inferred1"><a name="line63"></a> 63 end
|
664
|
+
</span><span class="marked0"><a name="line64"></a> 64 hash = tmp_hash
|
665
|
+
</span><span class="inferred1"><a name="line65"></a> 65
|
666
|
+
</span><span class="marked0"><a name="line66"></a> 66 hash.each do |key, val|
|
667
|
+
</span><span class="marked1"><a name="line67"></a> 67 if attribute_of[key]
|
668
|
+
</span><span class="marked0"><a name="line68"></a> 68 attribute_of[key].set val
|
669
|
+
</span><span class="inferred1"><a name="line69"></a> 69 end
|
670
|
+
</span><span class="inferred0"><a name="line70"></a> 70 end
|
671
|
+
</span><span class="inferred1"><a name="line71"></a> 71
|
672
|
+
</span><span class="marked0"><a name="line72"></a> 72 attribute_of.each do |key, val|
|
673
|
+
</span><span class="marked1"><a name="line73"></a> 73 next if val.class.lazy?
|
674
|
+
</span><span class="marked0"><a name="line74"></a> 74 raise AttrRequiredError, "param: :#{key} is required to #{hash.inspect}" if !val.class.optional? && !val.get
|
675
|
+
</span><span class="inferred1"><a name="line75"></a> 75 end
|
676
|
+
</span><span class="inferred0"><a name="line76"></a> 76
|
677
|
+
</span><span class="marked1"><a name="line77"></a> 77 after_init
|
678
|
+
</span><span class="inferred0"><a name="line78"></a> 78 end
|
679
|
+
</span><span class="inferred1"><a name="line79"></a> 79
|
680
|
+
</span><span class="inferred0"><a name="line80"></a> 80 # return Hash of attribute's name as a key and attribute's meta class instance as a value.
|
681
|
+
</span><span class="inferred1"><a name="line81"></a> 81 # for example,
|
682
|
+
</span><span class="inferred0"><a name="line82"></a> 82 #
|
683
|
+
</span><span class="inferred1"><a name="line83"></a> 83 # class YourClass
|
684
|
+
</span><span class="inferred0"><a name="line84"></a> 84 # include ClassX
|
685
|
+
</span><span class="inferred1"><a name="line85"></a> 85 # has :x
|
686
|
+
</span><span class="inferred0"><a name="line86"></a> 86 # end
|
687
|
+
</span><span class="inferred1"><a name="line87"></a> 87 #
|
688
|
+
</span><span class="inferred0"><a name="line88"></a> 88 # obj = YourClass.new(:x => 10)
|
689
|
+
</span><span class="inferred1"><a name="line89"></a> 89 # obj.attribute_of #=> { "x" => <#<ClassX::Attribute> parent=<# YourClass> ... > }
|
690
|
+
</span><span class="inferred0"><a name="line90"></a> 90 #
|
691
|
+
</span><span class="marked1"><a name="line91"></a> 91 def attribute_of
|
692
|
+
</span><span class="marked0"><a name="line92"></a> 92 unless instance_variable_defined?('@__attribute_of') && @__attribute_of
|
693
|
+
</span><span class="marked1"><a name="line93"></a> 93 @__attribute_of = {}
|
694
|
+
</span><span class="marked0"><a name="line94"></a> 94 if self.class.attribute_of
|
695
|
+
</span><span class="marked1"><a name="line95"></a> 95 self.class.attribute_of.keys.each do |key|
|
696
|
+
</span><span class="marked0"><a name="line96"></a> 96 @__attribute_of[key] = __send__ "attribute_of:#{key}"
|
697
|
+
</span><span class="inferred1"><a name="line97"></a> 97 end
|
698
|
+
</span><span class="inferred0"><a name="line98"></a> 98 end
|
699
|
+
</span><span class="inferred1"><a name="line99"></a> 99 end
|
700
|
+
</span><span class="inferred0"><a name="line100"></a>100
|
701
|
+
</span><span class="marked1"><a name="line101"></a>101 @__attribute_of
|
702
|
+
</span><span class="inferred0"><a name="line102"></a>102 end
|
703
|
+
</span><span class="inferred1"><a name="line103"></a>103
|
704
|
+
</span><span class="inferred0"><a name="line104"></a>104 # processing initialize argument to hash
|
705
|
+
</span><span class="inferred1"><a name="line105"></a>105 # you can override this method for not taking initializer your classx based class.
|
706
|
+
</span><span class="marked0"><a name="line106"></a>106 def before_init *args
|
707
|
+
</span><span class="marked1"><a name="line107"></a>107 raise ArgumentError if args.size > 1
|
708
|
+
</span><span class="inferred0"><a name="line108"></a>108
|
709
|
+
</span><span class="marked1"><a name="line109"></a>109 hash = args.first
|
710
|
+
</span><span class="marked0"><a name="line110"></a>110 hash.nil? ? {} : hash
|
711
|
+
</span><span class="inferred1"><a name="line111"></a>111 end
|
712
|
+
</span><span class="inferred0"><a name="line112"></a>112
|
713
|
+
</span><span class="marked1"><a name="line113"></a>113 alias process_init_args before_init
|
714
|
+
</span><span class="inferred0"><a name="line114"></a>114
|
715
|
+
</span><span class="inferred1"><a name="line115"></a>115 # automatically called this method on last of #initialize.
|
716
|
+
</span><span class="inferred0"><a name="line116"></a>116 # you can override this method.
|
717
|
+
</span><span class="marked1"><a name="line117"></a>117 def after_init
|
718
|
+
</span><span class="marked0"><a name="line118"></a>118 end
|
719
|
+
</span><span class="inferred1"><a name="line119"></a>119
|
720
|
+
</span><span class="inferred0"><a name="line120"></a>120 # shared implementation for comparing classx based object.
|
721
|
+
</span><span class="marked1"><a name="line121"></a>121 def == other
|
722
|
+
</span><span class="marked0"><a name="line122"></a>122 return false unless other.kind_of? self.class
|
723
|
+
</span><span class="marked1"><a name="line123"></a>123 attribute_of.all? do |key, val|
|
724
|
+
</span><span class="marked0"><a name="line124"></a>124 val.get == other.__send__(key)
|
725
|
+
</span><span class="inferred1"><a name="line125"></a>125 end
|
726
|
+
</span><span class="inferred0"><a name="line126"></a>126 end
|
727
|
+
</span><span class="inferred1"><a name="line127"></a>127
|
728
|
+
</span><span class="marked0"><a name="line128"></a>128 UNSERIALIZE_INSTANCE_VARIABLES = ["@__attribute_of", "@__attribute_data_of"]
|
729
|
+
</span><span class="inferred1"><a name="line129"></a>129
|
730
|
+
</span><span class="inferred0"><a name="line130"></a>130 # convert attribute key and value to Hash.
|
731
|
+
</span><span class="marked1"><a name="line131"></a>131 def to_hash
|
732
|
+
</span><span class="marked0"><a name="line132"></a>132 result = {}
|
733
|
+
</span><span class="inferred1"><a name="line133"></a>133
|
734
|
+
</span><span class="marked0"><a name="line134"></a>134 attribute_of.each do |key, val|
|
735
|
+
</span><span class="marked1"><a name="line135"></a>135 result[key] = val.get
|
736
|
+
</span><span class="inferred0"><a name="line136"></a>136 end
|
737
|
+
</span><span class="inferred1"><a name="line137"></a>137
|
738
|
+
</span><span class="marked0"><a name="line138"></a>138 result
|
739
|
+
</span><span class="inferred1"><a name="line139"></a>139 end
|
740
|
+
</span><span class="inferred0"><a name="line140"></a>140
|
741
|
+
</span><span class="inferred1"><a name="line141"></a>141 # dupping classx based class.
|
742
|
+
</span><span class="marked0"><a name="line142"></a>142 def dup
|
743
|
+
</span><span class="marked1"><a name="line143"></a>143 self.class.new(to_hash)
|
744
|
+
</span><span class="marked0"><a name="line144"></a>144 end
|
745
|
+
</span><span class="inferred1"><a name="line145"></a>145
|
746
|
+
</span><span class="inferred0"><a name="line146"></a>146 # for Marshal.dump
|
747
|
+
</span><span class="marked1"><a name="line147"></a>147 def marshal_dump
|
748
|
+
</span><span class="marked0"><a name="line148"></a>148 dump_of = {}
|
749
|
+
</span><span class="marked1"><a name="line149"></a>149 dump_of[:attribute_of] = to_hash
|
750
|
+
</span><span class="marked0"><a name="line150"></a>150 dump_of[:instance_variable_of] = {}
|
751
|
+
</span><span class="marked1"><a name="line151"></a>151 ( instance_variables.map {|ival| ival.to_s } - UNSERIALIZE_INSTANCE_VARIABLES ).each do |ival|
|
752
|
+
</span><span class="marked0"><a name="line152"></a>152 dump_of[:instance_variable_of][ival] = instance_variable_get(ival)
|
753
|
+
</span><span class="inferred1"><a name="line153"></a>153 end
|
754
|
+
</span><span class="inferred0"><a name="line154"></a>154
|
755
|
+
</span><span class="marked1"><a name="line155"></a>155 dump_of
|
756
|
+
</span><span class="inferred0"><a name="line156"></a>156 end
|
757
|
+
</span><span class="inferred1"><a name="line157"></a>157
|
758
|
+
</span><span class="inferred0"><a name="line158"></a>158 # for Marshal.load
|
759
|
+
</span><span class="marked1"><a name="line159"></a>159 def marshal_load val
|
760
|
+
</span><span class="marked0"><a name="line160"></a>160 self.attribute_of.each do |k, v|
|
761
|
+
</span><span class="marked1"><a name="line161"></a>161 v.set(val[:attribute_of][k])
|
762
|
+
</span><span class="inferred0"><a name="line162"></a>162 end
|
763
|
+
</span><span class="marked1"><a name="line163"></a>163 val[:instance_variable_of].each do |key, val|
|
764
|
+
</span><span class="marked0"><a name="line164"></a>164 instance_variable_set(key, val)
|
765
|
+
</span><span class="inferred1"><a name="line165"></a>165 end
|
766
|
+
</span><span class="inferred0"><a name="line166"></a>166 end
|
767
|
+
</span><span class="inferred1"><a name="line167"></a>167
|
768
|
+
</span><span class="inferred0"><a name="line168"></a>168 # for YAML.dump
|
769
|
+
</span><span class="marked1"><a name="line169"></a>169 def to_yaml opts={}
|
770
|
+
</span><span class="marked0"><a name="line170"></a>170 require 'yaml'
|
771
|
+
</span><span class="marked1"><a name="line171"></a>171 YAML.quick_emit(self, opts) do |out|
|
772
|
+
</span><span class="marked0"><a name="line172"></a>172 out.map( taguri, to_yaml_style ) do |map|
|
773
|
+
</span><span class="marked1"><a name="line173"></a>173 attribute_of = {}
|
774
|
+
</span><span class="marked0"><a name="line174"></a>174 to_hash.each do |key, val|
|
775
|
+
</span><span class="marked1"><a name="line175"></a>175 attribute_of[key] = val
|
776
|
+
</span><span class="inferred0"><a name="line176"></a>176 end
|
777
|
+
</span><span class="marked1"><a name="line177"></a>177 map.add(:attribute_of, attribute_of)
|
778
|
+
</span><span class="inferred0"><a name="line178"></a>178
|
779
|
+
</span><span class="marked1"><a name="line179"></a>179 instance_variable_of = {}
|
780
|
+
</span><span class="marked0"><a name="line180"></a>180 ( instance_variables.map {|ival| ival.to_s } - UNSERIALIZE_INSTANCE_VARIABLES ).each do |ival|
|
781
|
+
</span><span class="marked1"><a name="line181"></a>181 instance_variable_of[ival] = instance_variable_get(ival)
|
782
|
+
</span><span class="inferred0"><a name="line182"></a>182 end
|
783
|
+
</span><span class="marked1"><a name="line183"></a>183 map.add(:instance_variable_of, instance_variable_of)
|
784
|
+
</span><span class="inferred0"><a name="line184"></a>184 end
|
785
|
+
</span><span class="inferred1"><a name="line185"></a>185 end
|
786
|
+
</span><span class="inferred0"><a name="line186"></a>186 end
|
787
|
+
</span><span class="inferred1"><a name="line187"></a>187
|
788
|
+
</span><span class="inferred0"><a name="line188"></a>188 # for YAML.load
|
789
|
+
</span><span class="marked1"><a name="line189"></a>189 def yaml_initialize tag, val
|
790
|
+
</span><span class="marked0"><a name="line190"></a>190 self.attribute_of.each do |k, v|
|
791
|
+
</span><span class="marked1"><a name="line191"></a>191 v.set(val[:attribute_of][k])
|
792
|
+
</span><span class="inferred0"><a name="line192"></a>192 end
|
793
|
+
</span><span class="inferred1"><a name="line193"></a>193
|
794
|
+
</span><span class="marked0"><a name="line194"></a>194 val[:instance_variable_of].each do |k, v|
|
795
|
+
</span><span class="marked1"><a name="line195"></a>195 instance_variable_set(k, v)
|
796
|
+
</span><span class="inferred0"><a name="line196"></a>196 end
|
797
|
+
</span><span class="inferred1"><a name="line197"></a>197 end
|
798
|
+
</span><span class="inferred0"><a name="line198"></a>198
|
799
|
+
</span><span class="marked1"><a name="line199"></a>199 private
|
800
|
+
</span><span class="inferred0"><a name="line200"></a>200
|
801
|
+
</span><span class="marked1"><a name="line201"></a>201 def self.included klass
|
802
|
+
</span><span class="marked0"><a name="line202"></a>202 klass.extend(Attributes)
|
803
|
+
</span><span class="marked1"><a name="line203"></a>203 end
|
804
|
+
</span><span class="inferred0"><a name="line204"></a>204
|
805
|
+
</span><span class="inferred1"><a name="line205"></a>205 end
|
676
806
|
</span></pre><hr/>
|
677
807
|
<p>Generated using the <a href='http://eigenclass.org/hiki.rb?rcov'>rcov code coverage analysis tool for Ruby</a>
|
678
808
|
version 0.8.1.2.</p>
|