anise 0.2.2 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/MANIFEST CHANGED
@@ -1,6 +1,7 @@
1
1
  lib
2
2
  meta
3
3
  test
4
+ spec
4
5
  MANIFEST
5
6
  RELEASE
6
7
  README
@@ -27,5 +28,6 @@ test/test_annotator_toplevel.rb
27
28
  test/test_annotations.rb
28
29
  test/test_attribute_toplevel.rb
29
30
  test/test_anise.rb
31
+ test/test_annotations_module.rb
30
32
  test/test_anise_toplevel.rb
31
33
  test/test_annotations_toplevel.rb
data/RELEASE CHANGED
@@ -1,13 +1,13 @@
1
- = Anise 0.2.2 hits the streets.
1
+ = Anise 0.3.0
2
2
 
3
3
  Anise is a spin off the the Facets annotations.rb library.
4
4
  It includes the Annotations functionality, and adds
5
5
  a mixin, Annotator, that makes it easy to add new
6
6
  annotations on the fly.
7
7
 
8
- ### 0.2.0 // 2008-09-23
8
+ ### 0.3.0 // 2009-02-15
9
9
 
10
10
  1 Major Enhancments
11
11
 
12
- * #ann! will auto-prime any referenced annotation if it does not yet exist.
12
+ * Fixed some major bugs that prevented toplevel include from working.
13
13
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- anise 0.2.2 beta (2009-02-11)
1
+ anise 0.3.0 beta (2009-02-15)
@@ -71,7 +71,15 @@ module Anise
71
71
  module Annotation
72
72
 
73
73
  def self.append_features(base)
74
- base.extend self
74
+ if base == ::Object
75
+ append_features(::Module)
76
+ elsif base == ::Module
77
+ unless ::Module < Annotation
78
+ super
79
+ end
80
+ else
81
+ base.extend self
82
+ end
75
83
  end
76
84
 
77
85
  # Lookup an annotation. Unlike +annotations[ref]+
@@ -35,12 +35,30 @@ module Anise
35
35
  module Annotator
36
36
 
37
37
  def self.append_features(base)
38
- #if base == Object
39
- # Module.send(:include, self) # FIXME: Module ?
40
- #else
38
+ if base == Object
39
+ append_features(::Module)
40
+ elsif base == ::Module
41
+ unless Module < Annotator
42
+ ::Module.module_eval do
43
+ include Annotation
44
+ end
45
+ # can't include b/c it seem Module intercetps the call.
46
+ ::Module.module_eval do
47
+ def method_added(sym)
48
+ @pending_annotations ||= []
49
+ @pending_annotations.each do |name, args|
50
+ ann sym, name => args
51
+ end
52
+ @pending_annotations = []
53
+ #super if defined?(super)
54
+ end
55
+ end
56
+ super
57
+ end
58
+ else
41
59
  base.extend Annotation #unless base.is_a?(Annotation)
42
60
  base.extend self
43
- #end
61
+ end
44
62
  end
45
63
 
46
64
  def annotator(name)
@@ -27,21 +27,36 @@ module Anise
27
27
  module Attribute
28
28
 
29
29
  def self.append_features(base)
30
- base.extend Annotation
31
- base.extend Attribute
32
- base.module_eval do
30
+ if base == ::Object
31
+ append_features(::Module)
32
+ elsif base == ::Module
33
+ unless ::Module <= self
34
+ ::Module.module_eval do
35
+ include Annotation
36
+ super(::Module)
37
+ end
38
+ annotatable_attribute_method_for_module(:attr)
39
+ annotatable_attribute_method_for_module(:attr_reader)
40
+ annotatable_attribute_method_for_module(:attr_writer)
41
+ annotatable_attribute_method_for_module(:attr_accessor)
42
+ annotatable_attribute_method_for_module(:attr_setter) if defined?(attr_setter)
43
+ end
44
+ else
45
+ base.extend Annotation
46
+ base.extend Attribute
47
+ base = (class << base; self; end)
33
48
  #inheritor :instance_attributes, [], :|
34
- annotatable_attribute_method(:attr_reader)
35
- annotatable_attribute_method(:attr_writer)
36
- annotatable_attribute_method(:attr_accessor)
37
- annotatable_attribute_method(:attr_setter) if defined?(attr_setter)
49
+ annotatable_attribute_method(base, :attr)
50
+ annotatable_attribute_method(base, :attr_reader)
51
+ annotatable_attribute_method(base, :attr_writer)
52
+ annotatable_attribute_method(base, :attr_accessor)
53
+ annotatable_attribute_method(base, :attr_setter) if defined?(attr_setter)
38
54
  end
39
55
  end
40
56
 
41
57
  #
42
- def annotatable_attribute_method(attr_method_name)
43
- (class << self; self; end).module_eval do
44
-
58
+ def self.annotatable_attribute_method(base, attr_method_name)
59
+ base.module_eval do
45
60
  define_method(attr_method_name) do |*args|
46
61
  args.flatten!
47
62
 
@@ -71,9 +86,45 @@ module Anise
71
86
  # return the names of the attributes created
72
87
  return args
73
88
  end
74
-
75
89
  end
90
+ end
91
+ end
92
+
93
+ #
94
+ def self.annotatable_attribute_method_for_module(attr_method_name)
95
+ ::Module.module_eval do
96
+ alias_method "__#{attr_method_name}", attr_method_name
97
+
98
+ define_method(attr_method_name) do |*args|
99
+
100
+ args.flatten!
101
+
102
+ harg={}; while args.last.is_a?(Hash)
103
+ harg.update(args.pop)
104
+ end
105
+
106
+ raise ArgumentError if args.empty? and harg.empty?
107
+
108
+ if args.empty? # hash mode
109
+ harg.each { |a,h| __send__(attr_method_name,a,h) }
110
+ else
111
+ klass = harg[:class] = args.pop if args.last.is_a?(Class)
112
+
113
+ __send__("__#{attr_method_name}", *args)
114
+
115
+ args.each{|a| ann(a.to_sym,harg)}
116
+
117
+ instance_attributes!.concat(args) #merge!
118
+
119
+ # Use this callback to customize for your needs.
120
+ if respond_to?(:attr_callback)
121
+ attr_callback(self, args, harg)
122
+ end
76
123
 
124
+ # return the names of the attributes created
125
+ return args
126
+ end
127
+ end
77
128
  end
78
129
  end
79
130
 
@@ -0,0 +1,132 @@
1
+ require 'anise/annotation'
2
+
3
+ class Module
4
+ include Anise::Annotation
5
+ end
6
+
7
+ class Test_Annotation_Toplevel_0 < Test::Unit::TestCase
8
+ class X
9
+ attr :a
10
+ ann :a, :class => Integer
11
+ ann :@a, :valid => lambda{ |x| x.is_a?(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_Toplevel_1 < Test::Unit::TestCase
40
+ class X
41
+ def x1 ; end
42
+ ann :x1, :a=>1
43
+ ann :x1, :b=>2
44
+ end
45
+
46
+ def test_1_01
47
+ assert_equal( X.ann(:x1,:a), X.ann(:x1,:a) )
48
+ assert_equal( X.ann(:x1,:a).object_id, X.ann(:x1,:a).object_id )
49
+ end
50
+ def test_1_02
51
+ X.ann :x1, :a => 2
52
+ assert_equal( 2, X.ann(:x1,:a) )
53
+ end
54
+ end
55
+
56
+ class Test_Annotation_Toplevel_2 < Test::Unit::TestCase
57
+ class X
58
+ def x1 ; end
59
+ ann :x1, :a=>1
60
+ ann :x1, :b=>2
61
+ end
62
+ class Y < X ; end
63
+
64
+ def test_2_01
65
+ assert_equal( Y.ann(:x1,:a), Y.ann(:x1,:a) )
66
+ assert_equal( Y.ann(:x1,:a).object_id, Y.ann(:x1,:a).object_id )
67
+ end
68
+ def test_2_02
69
+ assert_equal( 1, Y.ann(:x1,:a) )
70
+ assert_equal( 2, Y.ann(:x1,:b) )
71
+ end
72
+ def test_2_03
73
+ Y.ann :x1,:a => 2
74
+ assert_equal( 2, Y.ann(:x1,:a) )
75
+ assert_equal( 2, Y.ann(:x1,:b) )
76
+ end
77
+ end
78
+
79
+ class Test_Annotation_Toplevel_3 < Test::Unit::TestCase
80
+ class X
81
+ ann :foo, Integer
82
+ end
83
+ class Y < X
84
+ ann :foo, String
85
+ end
86
+
87
+ def test_3_01
88
+ assert_equal( Integer, X.ann(:foo, :class) )
89
+ end
90
+ def test_3_02
91
+ assert_equal( String, Y.ann(:foo, :class) )
92
+ end
93
+ end
94
+
95
+ class Test_Annotation_Toplevel_4 < Test::Unit::TestCase
96
+ class X
97
+ ann :foo, :doc => "hello"
98
+ ann :foo, :bar => []
99
+ end
100
+ class Y < X
101
+ ann :foo, :class => String, :doc => "bye"
102
+ end
103
+
104
+ def test_4_01
105
+ assert_equal( "hello", X.ann(:foo,:doc) )
106
+ end
107
+ def test_4_02
108
+ assert_equal( X.ann(:foo), { :doc => "hello", :bar => [] } )
109
+ end
110
+ def test_4_03
111
+ X.ann(:foo,:bar) << "1"
112
+ assert_equal( ["1"], X.ann(:foo,:bar) )
113
+ end
114
+ def test_4_04
115
+ assert_equal( "bye", Y.ann(:foo,:doc) )
116
+ end
117
+ def test_4_05
118
+ #assert_equal( nil, Y.ann(:foo,:bar) )
119
+ assert_equal( ["1"], Y.ann(:foo,:bar) )
120
+ end
121
+ def test_4_06
122
+ Y.ann(:foo, :doc => "cap")
123
+ assert_equal( "cap", Y.ann(:foo, :doc) )
124
+ end
125
+ def test_4_07
126
+ Y.ann!(:foo,:bar) << "2"
127
+ assert_equal( ["1", "2"], Y.ann(:foo,:bar) )
128
+ assert_equal( ["1", "2"], Y.ann(:foo,:bar) )
129
+ assert_equal( ["1"], X.ann(:foo,:bar) )
130
+ end
131
+ end
132
+
@@ -1,3 +1,4 @@
1
+ require 'anise/annotation'
1
2
  require 'anise/attribute'
2
3
 
3
4
  include Anise::Attribute
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.2.2
4
+ version: 0.3.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: 2009-02-11 00:00:00 -05:00
13
+ date: 2009-02-15 00:00:00 -05:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -40,6 +40,7 @@ files:
40
40
  - lib
41
41
  - meta
42
42
  - test
43
+ - spec
43
44
  - MANIFEST
44
45
  - RELEASE
45
46
  - README
@@ -66,6 +67,7 @@ files:
66
67
  - test/test_annotations.rb
67
68
  - test/test_attribute_toplevel.rb
68
69
  - test/test_anise.rb
70
+ - test/test_annotations_module.rb
69
71
  - test/test_anise_toplevel.rb
70
72
  - test/test_annotations_toplevel.rb
71
73
  has_rdoc: true
@@ -105,5 +107,6 @@ test_files:
105
107
  - test/test_annotations.rb
106
108
  - test/test_attribute_toplevel.rb
107
109
  - test/test_anise.rb
110
+ - test/test_annotations_module.rb
108
111
  - test/test_anise_toplevel.rb
109
112
  - test/test_annotations_toplevel.rb