anise 0.6.0 → 0.8.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.
@@ -0,0 +1,218 @@
1
+ module Anise
2
+
3
+ module Annotative
4
+
5
+ # The {Annotative::Attributes} mixin modifies the attr_* methods to allow easy
6
+ # addition of annotations for attributes. It modifies the built in
7
+ # attribute methods (attr, attr_reader, attr_writer and attr_accessor),
8
+ # and any other custom `attr_*` methods, to allow annotations to be
9
+ # added to them directly rather than requiring a separate annotating
10
+ # statement.
11
+ #
12
+ # class X
13
+ # extend Anise::Annotative::Attributes
14
+ #
15
+ # attr :a, :valid => lambda{ |x| x.is_a?(Integer) }
16
+ # end
17
+ #
18
+ # See {Annotation} module for more information.
19
+ #
20
+ # One thing to know: a `private` or `protected` line on its own does
21
+ # not apply to the attributes that follow it. Ruby keeps that setting
22
+ # for the class body alone, where a method that wraps `attr` can't see
23
+ # it. Name the attributes instead:
24
+ #
25
+ # class X
26
+ # extend Anise::Annotative::Attributes
27
+ #
28
+ # attr :a, :doc => "one way"
29
+ # private :a
30
+ #
31
+ # private attr(:b, :doc => "another")
32
+ # end
33
+ #
34
+ # @todo Currently annotated attributes alwasy use the standard
35
+ # annotator (:ann). In the future we might make this customizable.
36
+ #
37
+ module Attributes
38
+
39
+ include Annotations
40
+
41
+ #
42
+ # When included into a class or module, {Annotation} is also
43
+ # included and {Attribute::Aid} extends the class/module.
44
+ #
45
+ # @param base [Class, Module]
46
+ # The class or module to get features.
47
+ #
48
+ def self.extended(base)
49
+ base_class = (class << base; self; end)
50
+ attribute_methods(base).each do |attr_method|
51
+ define_annotated_attribute(base_class, attr_method)
52
+ end
53
+ end
54
+
55
+ #
56
+ # The attribute methods of a class or module: all those whose names
57
+ # start with `attr_`. A class can give its own list by defining an
58
+ # `attribute_methods` class method before it is extended.
59
+ #
60
+ def self.attribute_methods(base)
61
+ return base.attribute_methods if base.respond_to?(:attribute_methods)
62
+ list = base.public_methods(true)
63
+ list += base.protected_methods(true)
64
+ list += base.private_methods(true)
65
+ list.select{ |m| m.to_s.start_with?('attr_') && m != :attr_callback }.uniq
66
+ end
67
+
68
+ #
69
+ # Define an annotated attribute method, given an existing
70
+ # non-annotated attribute method. The new method goes in a module
71
+ # prepended to +base+, a singleton class, so that it can call the
72
+ # existing one whether that is inherited or the class's own.
73
+ #
74
+ def self.define_annotated_attribute(base, attr_method_name)
75
+ wrapper = Module.new do
76
+ define_method(attr_method_name) do |*args|
77
+ args.flatten!
78
+
79
+ harg={}; while args.last.is_a?(Hash)
80
+ harg.update(args.pop)
81
+ end
82
+
83
+ raise ArgumentError if args.empty? and harg.empty?
84
+
85
+ if args.empty? # hash mode
86
+ harg.each { |a,h| __send__(attr_method_name,a,h) }
87
+ else
88
+ harg[:class] = args.pop if args.last.is_a?(Class)
89
+
90
+ made = super(*args)
91
+
92
+ args.each{|a| ann(a.to_sym,harg)}
93
+
94
+ if harg.key?(:default) && DEFAULTING.include?(attr_method_name.to_sym)
95
+ args.each{ |a| Attributes.define_default_reader(self, a.to_sym) }
96
+ end
97
+
98
+ instance_attributes!.concat(args) #merge!
99
+
100
+ # Use this callback to customize for your needs.
101
+ if respond_to?(:attr_callback)
102
+ attr_callback(self, args, harg)
103
+ end
104
+
105
+ # Return the names of the methods made, as Ruby's own attr
106
+ # methods do, so that `private attr_accessor(...)` covers
107
+ # the writers too. Failing that, the attributes' names.
108
+ if Array === made && !made.empty? && made.all?{ |m| Symbol === m }
109
+ return made
110
+ else
111
+ return args.map{ |a| a.to_sym }
112
+ end
113
+ end
114
+ end
115
+ end
116
+ base.prepend(wrapper)
117
+ end
118
+
119
+ # The attribute methods whose readers honor a :default annotation.
120
+ DEFAULTING = [:attr, :attr_reader, :attr_accessor]
121
+
122
+ #
123
+ # Redefine an attribute's reader to give its :default annotation
124
+ # while the attribute has not been set. Once set, to anything, nil
125
+ # included, the reader gives what was set.
126
+ #
127
+ # The default is looked up when it is first needed, so a subclass
128
+ # can change it with `ann :name, :default=>value`. Each instance
129
+ # gets its own copy (a +dup+), so a default such as `[]` is not
130
+ # shared. A Proc is called instead, in the instance, and what it
131
+ # returns is the default.
132
+ #
133
+ def self.define_default_reader(base, name)
134
+ ivar = "@#{name}"
135
+ hide = base.private_method_defined?(name) ? :private :
136
+ base.protected_method_defined?(name) ? :protected : nil
137
+
138
+ base.module_eval do
139
+ remove_method(name) if method_defined?(name, false) || private_method_defined?(name, false)
140
+
141
+ define_method(name) do
142
+ unless instance_variable_defined?(ivar)
143
+ from = self.class.respond_to?(:ann) ? self.class : base
144
+ value = from.ann(name, :default)
145
+ value = Proc === value ? instance_exec(&value) : value.dup
146
+ instance_variable_set(ivar, value)
147
+ end
148
+ instance_variable_get(ivar)
149
+ end
150
+
151
+ __send__(hide, name) if hide
152
+ end
153
+ end
154
+
155
+ #
156
+ # Instance attributes, including inherited attributes.
157
+ #
158
+ def instance_attributes
159
+ a = []
160
+ ancestors.each do |anc|
161
+ next unless Attributes === anc
162
+ if x = anc.instance_attributes!
163
+ a |= x
164
+ end
165
+ end
166
+ return a
167
+ end
168
+
169
+ #
170
+ # Local instance attributes.
171
+ #
172
+ def instance_attributes!
173
+ @instance_attributes ||= []
174
+ end
175
+
176
+ #
177
+ # Return list of attributes that have a :class annotation.
178
+ #
179
+ # class MyClass
180
+ # attr_accessor :test
181
+ # attr_accessor :name, String, :doc => 'Hello'
182
+ # attr_accessor :age, Integer
183
+ # end
184
+ #
185
+ # MyClass.instance_attributes # => [:test, :name, :age]
186
+ # MyClass.classified_attributes # => [:name, :age]
187
+ #
188
+ def classified_attributes
189
+ instance_attributes.find_all do |a|
190
+ self.ann(a, :class)
191
+ end
192
+ end
193
+
194
+ #
195
+ # This adjusts #attr so that it can take annotations, and still
196
+ # takes the old boolean argument for a writer.
197
+ #
198
+ def attr(*args)
199
+ args.flatten!
200
+ case args.last
201
+ when TrueClass
202
+ args.pop
203
+ attr_accessor(*args)
204
+ when FalseClass, NilClass
205
+ args.pop
206
+ attr_reader(*args)
207
+ else
208
+ attr_reader(*args)
209
+ end
210
+ end
211
+
212
+ end
213
+
214
+ end
215
+
216
+ end
217
+
218
+ # Copyright (c) 2006,2011 Thomas Sawyer. All rights reserved. (BSD-2-Clause License)
@@ -0,0 +1,129 @@
1
+ module Anise
2
+
3
+ module Annotative
4
+
5
+ # The Annotator::Method module allows for the creation of annotations
6
+ # which attach to the next method defined.
7
+ #
8
+ # This idiom of annotation-before-definition was popularized by Rake's
9
+ # `desc`/`task` pair. This module can be used to add similar capabilites
10
+ # to any class or module.
11
+ #
12
+ # class X
13
+ # extend Anise::Annotative::Methods
14
+ #
15
+ # def self.doc(string)
16
+ # method_annotation(:doc => string)
17
+ # end
18
+ #
19
+ # doc "See what I mean?"
20
+ #
21
+ # def see
22
+ # puts "Yes, I see!"
23
+ # end
24
+ # end
25
+ #
26
+ # X.ann(:see, :doc) #=> "See what I mean?"
27
+ #
28
+ # One can get a bit more control over the creation of annotations
29
+ # by using a block. In this case it is up the code to actually
30
+ # create the annotation.
31
+ #
32
+ # def self.doc(string)
33
+ # method_annotation do |meth|
34
+ # ann meth, :doc => string
35
+ # end
36
+ # end
37
+ #
38
+ # Note that the library uses the #method_added callback, so be sure to
39
+ # respect good practices of calling +super+ if you need to override
40
+ # this method.
41
+ #
42
+ module Methods
43
+
44
+ include Annotations
45
+
46
+ #
47
+ # The annotations waiting for the next method to be defined.
48
+ #
49
+ def pending_annotations
50
+ @_pending_annotations ||= []
51
+ end
52
+
53
+ #
54
+ # Define a method annotation.
55
+ #
56
+ # @example
57
+ # method_annotator :doc
58
+ #
59
+ # @param name [Symbol]
60
+ # Name of annotation.
61
+ #
62
+ def method_annotator(name, &block)
63
+ (class << self; self; end).module_eval do
64
+ define_method(name) do |*args|
65
+ anns = { name => (args.size > 1 ? args : args.first) }
66
+ pending_annotations << [anns, block]
67
+ end
68
+ end
69
+ end
70
+
71
+ #
72
+ #
73
+ #
74
+ def annotator(name, &block)
75
+ if name.to_s.start_with?('@')
76
+ if defined?(super)
77
+ super(name, &block)
78
+ else
79
+ raise ArgumentError, "not a valid method name -- #{name}"
80
+ end
81
+ else
82
+ method_annotator(name, &block)
83
+ end
84
+ end
85
+
86
+ #
87
+ # Setup a pending method annotation.
88
+ #
89
+ # @param [Hash] annotations
90
+ # The annotation settings.
91
+ #
92
+ def method_annotation(*args, &block)
93
+ anns = (Hash === args.last ? args.pop : {})
94
+ pending_annotations << [anns, block]
95
+ end
96
+
97
+ #
98
+ # When a method is added, run all pending annotations.
99
+ #
100
+ # @param [Symbol] sym
101
+ # The name of the method added.
102
+ #
103
+ def method_added(sym)
104
+ pending = pending_annotations
105
+ @_pending_annotations = []
106
+ pending.each do |anns, block|
107
+ if block
108
+ block.call(sym)
109
+ else
110
+ anns.each do |name, value|
111
+ if name.to_s.index('/')
112
+ name, ns = name.to_s.split('/')
113
+ else
114
+ ns = :ann
115
+ end
116
+ ann("#{sym}/#{ns}", name=>value)
117
+ end
118
+ end
119
+ end
120
+ super if defined?(super)
121
+ end
122
+
123
+ end
124
+
125
+ end
126
+
127
+ end
128
+
129
+ # Copyright (c) 2006 Rubyworks. All rights reserved. (BSD-2-Clause License)
@@ -0,0 +1,100 @@
1
+ module Anise
2
+
3
+ module Annotative
4
+
5
+ # I bet you never imagined Ruby could suport `@style` annotations.
6
+ # Well, I am here to tell you otherwise.
7
+ #
8
+ # The {VariableAnnotator} module allows class instance variable to be
9
+ # used method annotations which attach to the next defined method.
10
+ #
11
+ # class X
12
+ # extend Anise::Annotative::Variables
13
+ #
14
+ # variable_annotator :@doc
15
+ # variable_annotator :@returns
16
+ #
17
+ # @doc = "See what I mean?"
18
+ # @returns = NilClass
19
+ #
20
+ # def see
21
+ # puts "Yes, I see!"
22
+ # end
23
+ # end
24
+ #
25
+ # X.ann(:see, :@doc) #=> "See what I mean?"
26
+ #
27
+ # This library uses the #method_added callback, so be sure to respect
28
+ # good practices of calling +super+ if you need to override this method.
29
+ #
30
+ # **IMPORTANT!!!** This library is an interesting expirement, but it remains
31
+ # to be determined if it makes sense for general use.
32
+ #
33
+ module Variables
34
+
35
+ include Annotations
36
+
37
+ #
38
+ # Open method annotations.
39
+ #
40
+ # @example
41
+ # variable_annotator :@doc
42
+ #
43
+ # @param ns [Symbol]
44
+ # Annotator to use. Default is `:ann`.
45
+ #
46
+ def variable_annotator(iv, &block)
47
+ # TODO: should none iv raise an error instead?
48
+ iv = "@#{iv}".to_sym if iv.to_s !~ /^@/
49
+
50
+ # TODO: use an annotation to record the annotators
51
+ #ann(:variable_annotator, iv=>block)
52
+
53
+ @_variable_annotations ||= {}
54
+ @_variable_annotations[iv] = block
55
+ end
56
+
57
+ #
58
+ def annotator(iv, &block)
59
+ if not iv.to_s.start_with?('@')
60
+ if defined?(super)
61
+ super(iv, &block)
62
+ else
63
+ raise ArgumentError, "not a valid instance variable -- #{iv}"
64
+ end
65
+ else
66
+ variable_annotator(iv, &block)
67
+ end
68
+ end
69
+
70
+ #
71
+ # When a method is added, run all pending annotations.
72
+ #
73
+ def method_added(sym)
74
+ @_variable_annotations ||= {}
75
+ @_variable_annotations.each do |iv, block|
76
+ if iv.to_s.index('/')
77
+ iv, ns = iv.to_s.split('/')
78
+ else
79
+ ns = :ann
80
+ end
81
+ # Only a variable that has been set since the last method
82
+ # annotates this one.
83
+ next unless instance_variable_defined?(iv)
84
+ value = remove_instance_variable(iv)
85
+ if block
86
+ block.call(sym, value)
87
+ else
88
+ ann("#{sym}/#{ns}", iv=>value)
89
+ end
90
+ end
91
+ super(sym) if defined?(super)
92
+ end
93
+
94
+ end
95
+
96
+ end
97
+
98
+ end
99
+
100
+ # Copyright (c) 2006 Rubyworks. All rights reserved. (BSD-2-Clause License)
@@ -0,0 +1,7 @@
1
+ module Anise
2
+
3
+ module Annotative
4
+
5
+ end
6
+
7
+ end
@@ -0,0 +1,32 @@
1
+ # Optional core extensions, loaded with `require 'anise/core_ext'`. They
2
+ # let a reference and its namespace be written with a slash:
3
+ #
4
+ # ann :foo/:doc, :text => "..."
5
+ #
6
+ # Without them, write it as a symbol or string: `ann :'foo/doc', ...`.
7
+
8
+ class Symbol
9
+ #
10
+ # Create new combination symbol with slash.
11
+ #
12
+ # @example
13
+ # :foo/:bar #=> :'foo/bar'
14
+ #
15
+ def /(other)
16
+ "#{self}/#{other}".to_sym
17
+ end unless method_defined?(:/)
18
+ end
19
+
20
+ class String
21
+ #
22
+ # Create new combination string with slash.
23
+ #
24
+ # @example
25
+ # 'foo'/'bar' #=> 'foo/bar'
26
+ #
27
+ def /(other)
28
+ "#{self}/#{other}"
29
+ end unless method_defined?(:/)
30
+ end
31
+
32
+ # Copyright (c) 2006 Rubyworks. All rights reserved. (BSD-2-Clause License)
@@ -0,0 +1,6 @@
1
+ require 'anise'
2
+
3
+ class Module
4
+ include Anise::Annotations
5
+ end
6
+
@@ -0,0 +1,4 @@
1
+ module Anise
2
+ # The version of this library.
3
+ VERSION = '0.8.0'
4
+ end
data/lib/anise.rb CHANGED
@@ -1,62 +1,9 @@
1
- require 'anise/annotation'
2
- require 'anise/attribute'
3
- require 'anise/annotator'
4
-
5
- # = Anise
6
- #
7
- # Dynamic Annotations for Ruby.
8
- #
9
- # require 'anise'
10
- #
11
- # class X
12
- # include Anise
13
- #
14
- # # Provides annotations
15
- # ann :foo, :class=>String
16
- #
17
- # # Provides annotated attributes
18
- # attr :bar, Integer, :max=>10
19
- #
20
- # # Provides method annotators.
21
- # annotator :doc
22
- # doc "foo is cool"
23
- # def foo
24
- # # ...
25
- # end
26
- # end
27
- #
28
1
  module Anise
29
- extend self
30
-
31
- def included(base)
32
- #super(base)
33
- base.send(:include, Attribute)
34
- base.send(:include, Annotator)
35
- #base.extend Anise #ClassMethods
36
- end
37
-
38
- #module ClassMethods
39
- # def append_features(base)
40
- # super(base)
41
- # Attribute.append_features(base)
42
- # Annotator.append_features(base)
43
- # base.extend ClassMethods
44
- # end
45
- #end
46
-
47
- #
48
- def self.metadata
49
- @metadata ||= (
50
- require 'yaml'
51
- YAML.load(File.new(File.dirname(__FILE__) + '/anise.yml'))
52
- )
53
- end
54
-
55
- #
56
- def self.const_missing(name)
57
- metadata[name.to_s.downcase] || super(name)
58
- end
59
-
60
- VERSION = metadata['version'] # becuase Ruby 1.8~ gets in the way :(
2
+ require 'anise/version'
3
+ require 'anise/annotations'
4
+ require 'anise/annotations/store'
5
+ require 'anise/annotative'
6
+ require 'anise/annotative/methods'
7
+ require 'anise/annotative/attributes'
8
+ require 'anise/annotative/variables'
61
9
  end
62
-