anise 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,136 @@
1
+ require 'anise/annotation'
2
+
3
+ class Test_Annotation_0 < Test::Unit::TestCase
4
+ class X
5
+ include Anise::Annotation
6
+
7
+ attr :a
8
+
9
+ ann :@a, :valid => lambda{ |x| x.is_a?(Integer) }
10
+
11
+ ann :a, :class => Integer
12
+
13
+ def initialize(a)
14
+ @a = a
15
+ end
16
+
17
+ def validate
18
+ instance_variables.each do |iv|
19
+ if validator = self.class.ann(iv)[:valid]
20
+ value = instance_variable_get(iv)
21
+ unless validator.call(value)
22
+ raise "Invalid value #{value} for #{iv}"
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ def test_annotation_class
30
+ assert_equal(Integer, X.ann(:a, :class))
31
+ end
32
+
33
+ def test_annotation_validate
34
+ x = X.new(1)
35
+ assert_nothing_raised{ x.validate }
36
+ end
37
+ end
38
+
39
+ class Test_Annotation_1 < Test::Unit::TestCase
40
+ class X
41
+ include Anise::Annotation
42
+ def x1 ; end
43
+ ann :x1, :a=>1
44
+ ann :x1, :b=>2
45
+ end
46
+
47
+ def test_1_01
48
+ assert_equal( X.ann(:x1,:a), X.ann(:x1,:a) )
49
+ assert_equal( X.ann(:x1,:a).object_id, X.ann(:x1,:a).object_id )
50
+ end
51
+ def test_1_02
52
+ X.ann :x1, :a => 2
53
+ assert_equal( 2, X.ann(:x1,:a) )
54
+ end
55
+ end
56
+
57
+ class Test_Annotation_2 < Test::Unit::TestCase
58
+ class X
59
+ include Anise::Annotation
60
+ def x1 ; end
61
+ ann :x1, :a=>1
62
+ ann :x1, :b=>2
63
+ end
64
+ class Y < X ; end
65
+
66
+ def test_2_01
67
+ assert_equal( Y.ann(:x1,:a), Y.ann(:x1,:a) )
68
+ assert_equal( Y.ann(:x1,:a).object_id, Y.ann(:x1,:a).object_id )
69
+ end
70
+ def test_2_02
71
+ assert_equal( 1, Y.ann(:x1,:a) )
72
+ assert_equal( 2, Y.ann(:x1,:b) )
73
+ end
74
+ def test_2_03
75
+ Y.ann :x1,:a => 2
76
+ assert_equal( 2, Y.ann(:x1,:a) )
77
+ assert_equal( 2, Y.ann(:x1,:b) )
78
+ end
79
+ end
80
+
81
+ class Test_Annotation_3 < Test::Unit::TestCase
82
+ class X
83
+ include Anise::Annotation
84
+ ann :foo, Integer
85
+ end
86
+ class Y < X
87
+ ann :foo, String
88
+ end
89
+
90
+ def test_3_01
91
+ assert_equal( Integer, X.ann(:foo, :class) )
92
+ end
93
+ def test_3_02
94
+ assert_equal( String, Y.ann(:foo, :class) )
95
+ end
96
+ end
97
+
98
+ class Test_Annotation_4 < Test::Unit::TestCase
99
+ class X
100
+ include Anise::Annotation
101
+ ann :foo, :doc => "hello"
102
+ ann :foo, :bar => []
103
+ end
104
+ class Y < X
105
+ ann :foo, :class => String, :doc => "bye"
106
+ end
107
+
108
+ def test_4_01
109
+ assert_equal( "hello", X.ann(:foo,:doc) )
110
+ end
111
+ def test_4_02
112
+ assert_equal( X.ann(:foo), { :doc => "hello", :bar => [] } )
113
+ end
114
+ def test_4_03
115
+ X.ann(:foo,:bar) << "1"
116
+ assert_equal( ["1"], X.ann(:foo,:bar) )
117
+ end
118
+ def test_4_04
119
+ assert_equal( "bye", Y.ann(:foo,:doc) )
120
+ end
121
+ def test_4_05
122
+ #assert_equal( nil, Y.ann(:foo,:bar) )
123
+ assert_equal( ["1"], Y.ann(:foo,:bar) )
124
+ end
125
+ def test_4_06
126
+ Y.ann(:foo, :doc => "cap")
127
+ assert_equal( "cap", Y.ann(:foo, :doc) )
128
+ end
129
+ def test_4_07
130
+ Y.ann!(:foo,:bar) << "2"
131
+ assert_equal( ["1", "2"], Y.ann(:foo,:bar) )
132
+ assert_equal( ["1", "2"], Y.ann(:foo,:bar) )
133
+ assert_equal( ["1"], X.ann(:foo,:bar) )
134
+ end
135
+ end
136
+
@@ -0,0 +1,131 @@
1
+ require 'anise/annotation'
2
+
3
+ include Anise::Annotation
4
+
5
+ class Test_Annotation_Toplevel_0 < Test::Unit::TestCase
6
+ class X
7
+ attr :a
8
+ ann :a, :class => Integer
9
+ ann :@a, :valid => lambda{ |x| x.is_a?(Integer) }
10
+
11
+ def initialize(a)
12
+ @a = a
13
+ end
14
+
15
+ def validate
16
+ instance_variables.each do |iv|
17
+ if validator = self.class.ann(iv)[:valid]
18
+ value = instance_variable_get(iv)
19
+ unless validator.call(value)
20
+ raise "Invalid value #{value} for #{iv}"
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ def test_annotation_class
28
+ assert_equal(Integer, X.ann(:a, :class))
29
+ end
30
+
31
+ def test_annotation_validate
32
+ x = X.new(1)
33
+ assert_nothing_raised{ x.validate }
34
+ end
35
+
36
+ end
37
+
38
+ class Test_Annotation_Toplevel_1 < Test::Unit::TestCase
39
+ class X
40
+ def x1 ; end
41
+ ann :x1, :a=>1
42
+ ann :x1, :b=>2
43
+ end
44
+
45
+ def test_1_01
46
+ assert_equal( X.ann(:x1,:a), X.ann(:x1,:a) )
47
+ assert_equal( X.ann(:x1,:a).object_id, X.ann(:x1,:a).object_id )
48
+ end
49
+ def test_1_02
50
+ X.ann :x1, :a => 2
51
+ assert_equal( 2, X.ann(:x1,:a) )
52
+ end
53
+ end
54
+
55
+ class Test_Annotation_Toplevel_2 < Test::Unit::TestCase
56
+ class X
57
+ def x1 ; end
58
+ ann :x1, :a=>1
59
+ ann :x1, :b=>2
60
+ end
61
+ class Y < X ; end
62
+
63
+ def test_2_01
64
+ assert_equal( Y.ann(:x1,:a), Y.ann(:x1,:a) )
65
+ assert_equal( Y.ann(:x1,:a).object_id, Y.ann(:x1,:a).object_id )
66
+ end
67
+ def test_2_02
68
+ assert_equal( 1, Y.ann(:x1,:a) )
69
+ assert_equal( 2, Y.ann(:x1,:b) )
70
+ end
71
+ def test_2_03
72
+ Y.ann :x1,:a => 2
73
+ assert_equal( 2, Y.ann(:x1,:a) )
74
+ assert_equal( 2, Y.ann(:x1,:b) )
75
+ end
76
+ end
77
+
78
+ class Test_Annotation_Toplevel_3 < Test::Unit::TestCase
79
+ class X
80
+ ann :foo, Integer
81
+ end
82
+ class Y < X
83
+ ann :foo, String
84
+ end
85
+
86
+ def test_3_01
87
+ assert_equal( Integer, X.ann(:foo, :class) )
88
+ end
89
+ def test_3_02
90
+ assert_equal( String, Y.ann(:foo, :class) )
91
+ end
92
+ end
93
+
94
+ class Test_Annotation_Toplevel_4 < Test::Unit::TestCase
95
+ class X
96
+ ann :foo, :doc => "hello"
97
+ ann :foo, :bar => []
98
+ end
99
+ class Y < X
100
+ ann :foo, :class => String, :doc => "bye"
101
+ end
102
+
103
+ def test_4_01
104
+ assert_equal( "hello", X.ann(:foo,:doc) )
105
+ end
106
+ def test_4_02
107
+ assert_equal( X.ann(:foo), { :doc => "hello", :bar => [] } )
108
+ end
109
+ def test_4_03
110
+ X.ann(:foo,:bar) << "1"
111
+ assert_equal( ["1"], X.ann(:foo,:bar) )
112
+ end
113
+ def test_4_04
114
+ assert_equal( "bye", Y.ann(:foo,:doc) )
115
+ end
116
+ def test_4_05
117
+ #assert_equal( nil, Y.ann(:foo,:bar) )
118
+ assert_equal( ["1"], Y.ann(:foo,:bar) )
119
+ end
120
+ def test_4_06
121
+ Y.ann(:foo, :doc => "cap")
122
+ assert_equal( "cap", Y.ann(:foo, :doc) )
123
+ end
124
+ def test_4_07
125
+ Y.ann!(:foo,:bar) << "2"
126
+ assert_equal( ["1", "2"], Y.ann(:foo,:bar) )
127
+ assert_equal( ["1", "2"], Y.ann(:foo,:bar) )
128
+ assert_equal( ["1"], X.ann(:foo,:bar) )
129
+ end
130
+ end
131
+
@@ -0,0 +1,28 @@
1
+ require 'anise/annotator'
2
+
3
+ class Test_Annotator < Test::Unit::TestCase
4
+
5
+ class X
6
+ include Anise::Annotator
7
+
8
+ annotator :req
9
+
10
+ req 'r'
11
+
12
+ def a ; "a"; end
13
+
14
+ req 's'
15
+
16
+ attr :b
17
+ end
18
+
19
+ def test_annotated
20
+ assert_equal( {:req=>['r']}, X.ann(:a) )
21
+ end
22
+
23
+ def test_annotated
24
+ assert_equal( {:req=>['s']}, X.ann(:b) )
25
+ end
26
+
27
+ end
28
+
@@ -0,0 +1,30 @@
1
+ require 'anise/annotator'
2
+
3
+ include Anise::Annotator
4
+
5
+ class Test_Annotator_Toplevel < Test::Unit::TestCase
6
+
7
+ class X
8
+ annotator :req
9
+
10
+ req 'r'
11
+
12
+ def a ; "a"; end
13
+
14
+ req 's'
15
+
16
+ attr :b
17
+ end
18
+
19
+ def test_annotated
20
+ assert_equal( {:req=>['r']}, X.ann(:a) )
21
+ end
22
+
23
+ def test_annotated
24
+ assert_equal( {:req=>['s']}, X.ann(:b) )
25
+ end
26
+
27
+ end
28
+
29
+ #annotator :req # THIS DOES NOT WORK :(
30
+
@@ -1,16 +1,37 @@
1
- require 'anise/attributes'
1
+ require 'anise/attribute'
2
2
 
3
3
  class Test_Attribute < Test::Unit::TestCase
4
+ class X
5
+ include Anise::Attribute
6
+ attr :q
7
+ attr :a, :x => 1
8
+ end
4
9
 
5
- include Anise::Annotatable::Attributes
10
+ def test_attr_a
11
+ assert_equal( {:x=>1}, X.ann(:a) )
12
+ end
13
+ end
6
14
 
7
- attr :q
15
+ class Test_Attribute_Using_Attr < Test::Unit::TestCase
16
+ class A
17
+ include Anise::Attribute
18
+ attr :x, :cast=>"to_s"
19
+ end
8
20
 
9
- attr :a, :x => 1
21
+ def test_01
22
+ assert_equal( "to_s", A.ann(:x,:cast) )
23
+ end
24
+ end
10
25
 
11
- def test_attr_a
12
- assert_equal( {:x=>1}, self.class.ann(:a) )
26
+ class Test_Attribute_Using_Attr_Accessor < Test::Unit::TestCase
27
+ class A
28
+ include Anise::Attribute
29
+ attr_accessor :x, :cast=>"to_s"
13
30
  end
14
31
 
32
+ def test_01
33
+ a = A.new
34
+ assert_equal( [:x], A.instance_attributes )
35
+ end
15
36
  end
16
37
 
@@ -0,0 +1,64 @@
1
+ require 'anise/attribute'
2
+
3
+ include Anise::Attribute
4
+
5
+ class Test_Attribute_Toplevel < Test::Unit::TestCase
6
+ class X
7
+ attr :q
8
+ attr :a, :x => 1
9
+ end
10
+
11
+ def test_attr_a
12
+ assert_equal( {:x=>1}, X.ann(:a) )
13
+ end
14
+ end
15
+
16
+ class Test_Attribute_Toplevel_Using_Attr < Test::Unit::TestCase
17
+ class A
18
+ attr :x, :cast=>"to_s"
19
+ end
20
+
21
+ def test_01
22
+ assert_equal( "to_s", A.ann(:x,:cast) )
23
+ end
24
+ end
25
+
26
+ class Test_Attribute_Toplevel_Using_Attr_Accessor < Test::Unit::TestCase
27
+ class A
28
+ attr_accessor :x, :cast=>"to_s"
29
+ end
30
+
31
+ def test_01
32
+ a = A.new
33
+ assert_equal( [:x], A.instance_attributes )
34
+ end
35
+ end
36
+
37
+
38
+ =begin
39
+ require 'annotable/attributes'
40
+ require 'test/unit'
41
+
42
+ include Anise::Attributes
43
+
44
+ $self = self
45
+
46
+ attr :b, :y => 1
47
+
48
+ class Test_Attribute_Toplevel < Test::Unit::TestCase
49
+
50
+ attr :q
51
+
52
+ attr :a, :x => 1
53
+
54
+ def test_attr_a
55
+ assert_equal( {:x=>1}, self.class.ann(:a) )
56
+ end
57
+
58
+ def test_attr_b
59
+ assert_equal( {:y=>1}, $self.class.ann(:b) )
60
+ end
61
+
62
+ end
63
+ =end
64
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: anise
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - tigerops-community@rubyforge.org
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2008-10-17 00:00:00 -04:00
13
+ date: 2008-10-28 00:00:00 -04:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -37,17 +37,19 @@ extra_rdoc_files:
37
37
  - VERSION
38
38
  - COPYING
39
39
  files:
40
- - MANIFEST
40
+ - lib
41
+ - meta
41
42
  - test
42
- - test/test_toplevel_attribute.rb
43
- - test/test_attribute.rb
44
- - test/test_annotatable.rb
45
- - test/suite.rb
46
- - test/test_toplevel_annotatable.rb
47
43
  - RELEASE
48
44
  - README
49
45
  - HISTORY
50
- - meta
46
+ - VERSION
47
+ - COPYING
48
+ - lib/anise
49
+ - lib/anise.rb
50
+ - lib/anise/attribute.rb
51
+ - lib/anise/annotation.rb
52
+ - lib/anise/annotator.rb
51
53
  - meta/require
52
54
  - meta/created
53
55
  - meta/homepage
@@ -56,14 +58,16 @@ files:
56
58
  - meta/license
57
59
  - meta/authors
58
60
  - meta/contact
59
- - lib
60
- - lib/anise
61
- - lib/anise/attributes.rb
62
- - lib/anise/annotations.rb
63
- - lib/anise/annotatable.rb
64
- - lib/anise.rb
65
- - VERSION
66
- - COPYING
61
+ - test/test_attribute.rb
62
+ - test/test_annotator.rb
63
+ - test/suite.rb
64
+ - test/test_annotator_toplevel.rb
65
+ - test/test_annotations.rb
66
+ - test/test_attribute_toplevel.rb
67
+ - test/test_anise.rb
68
+ - test/test_anise_toplevel.rb
69
+ - test/test_annotations_toplevel.rb
70
+ - MANIFEST
67
71
  has_rdoc: true
68
72
  homepage: http://
69
73
  post_install_message:
@@ -95,8 +99,11 @@ signing_key:
95
99
  specification_version: 2
96
100
  summary: Annable is an annotations systems for the Ruby programming lanaguage.
97
101
  test_files:
98
- - test/test_toplevel_attribute.rb
99
102
  - test/test_attribute.rb
100
- - test/test_annotatable.rb
101
- - test/suite.rb
102
- - test/test_toplevel_annotatable.rb
103
+ - test/test_annotator.rb
104
+ - test/test_annotator_toplevel.rb
105
+ - test/test_annotations.rb
106
+ - test/test_attribute_toplevel.rb
107
+ - test/test_anise.rb
108
+ - test/test_anise_toplevel.rb
109
+ - test/test_annotations_toplevel.rb
@@ -1,60 +0,0 @@
1
- module Anise
2
- require 'anise/annotations.rb'
3
-
4
- # = Annotatable
5
- #
6
- # Annotatable allows for the create of dynamic
7
- # method annotations which attach to the next
8
- # method defined.
9
- #
10
- # class X
11
- # include Anise::Annotatable
12
- #
13
- # annotation :doc
14
- #
15
- # doc "See waht I mean?"
16
- # def see
17
- # # ...
18
- # end
19
- # end
20
- #
21
- # X.ann(:see, :doc) #=> "See what I mean?"
22
- #
23
- # TODO: This must work when included in a module or
24
- # class and also when included at the toplevel.
25
- #
26
- module Annotatable
27
-
28
- def self.append_features(base)
29
- if base == Object
30
- Module.send(:include, self) # FIXME: Module ?
31
- else
32
- base.extend self
33
- end
34
- end
35
-
36
- def pending_annotations
37
- @pending_annotations ||= []
38
- end
39
-
40
- def annotation(name)
41
- (class << self; self; end).instance_eval do
42
- define_method(name) do |*args|
43
- pending_annotations << [name, args]
44
- end
45
- end
46
- end
47
-
48
- def method_added(sym)
49
- pending_annotations.each do |name, args|
50
- ann sym, name => args
51
- end
52
- @pending_annotations = []
53
- end
54
-
55
- end
56
-
57
- end
58
-
59
- # Copyright (c) 2005, 2008 TigerOps
60
-
@@ -1,114 +0,0 @@
1
- require 'facets/hash/rekey' # FIXME: remove dependency
2
- require 'facets/hash/op' # FIXME: remove dependency
3
-
4
- # = Annotations
5
- #
6
- # Annotations allows you to annontate objects, including methods with arbitrary
7
- # "metadata". These annotations don't do anything in themselves. They are
8
- # merely data. But you can put them to use. For instance an attribute
9
- # validator might check for an annotation called :valid and test against it.
10
- #
11
- # == Synopsis
12
- #
13
- # class X
14
- # attr :a
15
- # ann :@a, :valid => lambda{ |x| x.is_a?(Integer) }
16
- #
17
- # def validate
18
- # instance_variables.each do |iv|
19
- # if validator = self.class.ann(iv)[:valid]
20
- # value = instance_variable_get(iv)
21
- # unless validator.call(vale)
22
- # raise "Invalid value #{value} for #{iv}"
23
- # end
24
- # end
25
- # end
26
- # end
27
- #
28
- # end
29
- #
30
- #--
31
- # By using a global veriable rather the definining a class instance variable
32
- # for each class/module, it is possible to quicky scan all annotations for the
33
- # entire system. To do the same without this would require scanning through
34
- # the ObjectSpace. (Still which is better?)
35
- #
36
- #$annotations = Hash.new { |h,k| h[k] = {} }
37
- #++
38
- #
39
- # TODO: The ann(x).name notation is kind of nice. Would like to add that
40
- # back-in if reasonable. Basically this require heritage to be an
41
- # OpenObject rather than just a hash.
42
- #
43
- #--
44
- # History Note
45
- #
46
- # * 2006-11-07 trans Created this ultra-concise version of annotations.
47
- #++
48
-
49
- class ::Module
50
-
51
- def annotations
52
- #$annotations[self]
53
- @annotations ||= {}
54
- end
55
-
56
- def heritage(ref)
57
- ref = ref.to_sym
58
- ancestors.inject({}) { |memo, ancestor|
59
- ancestor.annotations[ref] ||= {}
60
- ancestor.annotations[ref] + memo
61
- }
62
- end
63
-
64
- # Set or read annotations.
65
-
66
- def ann( ref, keys_or_class=nil, keys=nil )
67
- return heritage(ref) unless keys_or_class or keys
68
-
69
- if Class === keys_or_class
70
- keys ||= {}
71
- keys[:class] = keys_or_class
72
- else
73
- keys = keys_or_class
74
- end
75
-
76
- if Hash === keys
77
- ref = ref.to_sym
78
- annotations[ref] ||= {}
79
- annotations[ref].update(keys.rekey)
80
- else
81
- key = keys.to_sym
82
- heritage(ref)[key]
83
- end
84
- end
85
-
86
- # To change an annotation's value in place for a given class or module
87
- # it first must be duplicated, otherwise the change may effect annotations
88
- # in the class or module's ancestors.
89
-
90
- def ann!( ref, keys_or_class=nil, keys=nil )
91
- #return heritage(ref) unless keys_or_class or keys
92
- return annotations[ref] unless keys_or_class or keys
93
-
94
- if Class === keys_or_class
95
- keys ||= {}
96
- keys[:class] = keys_or_class
97
- else
98
- keys = keys_or_class
99
- end
100
-
101
- if Hash === keys
102
- ref = ref.to_sym
103
- annotations[ref] ||= {}
104
- annotations[ref].update(keys.rekey)
105
- else
106
- key = keys.to_sym
107
- annotations[ref][key] = heritage(ref)[key].dup
108
- end
109
- end
110
-
111
- end
112
-
113
- # Copyright (c) 2005, 2008 TigerOps
114
-