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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 36b7c57c5e9eeb34fc277593564d18c64c87adbf5f50760897c43985a98d6f9c
4
+ data.tar.gz: 22edd171873a3e862a3e8746fa68dead29bfa5ce751d7abe181eb02385f445bc
5
+ SHA512:
6
+ metadata.gz: f18dc3f28919b74f000486524e4cd4b011cad365b6b3ae527262d81e5bb53add998974faa86f4399174680fbe0b2132b2a6ec25b567b738b1653abebc9958ded
7
+ data.tar.gz: 4e8e667079c0234571ba03f61ea2dca2259e0ab73914eb5f6c7532e69e47f1593abdd8fff2282a96d4c140355281879c6999e40c49926fa9f2ece661ee411f74
data/HISTORY.md ADDED
@@ -0,0 +1,131 @@
1
+ # RELEASE HISTORY
2
+
3
+ ## 0.8.0 / 2026-11-29
4
+
5
+ Anise is brought up to date, for Ruby 3.1 and up.
6
+
7
+ Changes:
8
+
9
+ * An attribute's `:default` annotation is now its default value:
10
+ `attr :list, :default=>[]`. Each object gets its own copy; a Proc is
11
+ called in the object for the value. (#1)
12
+ * `Symbol#/` and `String#/` are no longer added by `require 'anise'`.
13
+ To write `ann :ref/:ns`, add `require 'anise/core_ext'`; without it,
14
+ write the reference as `'ref/ns'`.
15
+ * `Module#attribute_methods` is no longer added. A class that defines an
16
+ `attribute_methods` class method of its own still has it used.
17
+ * `Anise::VERSION` is a plain constant. `Anise.metadata` and the
18
+ `anise.yml` file behind it are gone.
19
+ * The annotations waiting for the next method are kept by each class,
20
+ with `pending_annotations`, in place of one table shared by all.
21
+ * The unit tests run on Spectrum, and now assert what they describe.
22
+ * The QED demonstrations run on QED 3, in CI, and have a new one on
23
+ namespaces.
24
+
25
+ * The annotated `attr` methods return the names of the methods they
26
+ made, as Ruby's own do, so `private attr_accessor(:a, :doc=>"x")`
27
+ makes the writer private as well. They returned the attributes' names.
28
+
29
+ Documented, not changed: a `private` line on its own does not apply to
30
+ annotated attributes. See the README.
31
+
32
+ Fixes:
33
+
34
+ * `instance_attributes` and `classified_attributes` were always empty.
35
+ * An `attr_` method defined by the class itself, as opposed to one it
36
+ inherited, could not be annotated: it raised NoMethodError.
37
+ * `attr_callback` was itself treated as an attribute method.
38
+ * A variable annotator annotated every method defined after it, with
39
+ nil, where it should annotate only the next method after the variable
40
+ is set. The variable is now removed once used.
41
+ * `Annotative::Variables#annotator` raised NameError.
42
+
43
+
44
+ ## 0.7.0 / 2012-03-26
45
+
46
+ This release has some major API changes. Most significantly a number
47
+ of modules have been renamed. The `Method` module has been renamed to
48
+ `Annotative::Methods`. Likewise the `Attribute` module has been ranamed
49
+ to `Annotative::Attributes`, and so on. These have been renamed so that
50
+ including `Anise` in the toplevel will not cause conflicts with any
51
+ other modules or classes an application or library might be using.
52
+ In addition these modules now must use `extend` rather then `include`
53
+ to be mixed into a class or module, since they conatin only class methods.
54
+
55
+ Changes:
56
+
57
+ * Rename Annotations module to Annotations::Store.
58
+ * Rename Annotation module to Annotations.
59
+ * Rename Method module to Annotative::Methods.
60
+ * Rename Attribute module to Annotative::Attributes.
61
+ * Add #method_annotation for use in custom class method.
62
+ * Discourage #method_annotator in favor of #method_annotation.
63
+
64
+
65
+ ## 0.6.0 / 2011-05-16
66
+
67
+ This release fixes an bug in which append_features cant be called b/c
68
+ it is a private method. This release also renames `ClassMethods`
69
+ modules to `Aid`.
70
+
71
+ Changes:
72
+
73
+ * Fixed private method call to #append_features.
74
+ * Rename ClassMethods to Aid.
75
+
76
+
77
+ ## 0.5.0 / 2011-04-30
78
+
79
+ The primary changes in this release are behind the scenes implementation
80
+ improvements. The most significant of which is the simplification of
81
+ the #append_features code. In addition, annotators have been enhanced
82
+ to assign a single argument if one, and an array of arguments if there
83
+ are more than one. They can also override the callback altogether.
84
+
85
+ Changes:
86
+
87
+ * Simplified #append_features code.
88
+ * Use ClassMethod submodules.
89
+ * Annotators differentiate one vs. multiple arguments.
90
+ * Annotators can override method_added callback.
91
+
92
+
93
+ ## 0.4.0 / 2009-05-28
94
+
95
+ This version adds a callback method called #annotation_added
96
+ --a striaght-forward callback method patterned after Ruby's
97
+ other built-in callback methods. The callback should be enough
98
+ to allow for the creation of "active" annotations.
99
+
100
+ Changes:
101
+
102
+ * Added annotation_added callback.
103
+
104
+
105
+ ## 0.2.1 / 2008-10-31
106
+
107
+ Project reorganization release --mostly some file names have changed.
108
+
109
+ Changes:
110
+
111
+ * Renamed some lib files.
112
+
113
+
114
+ ## 0.2.0 / 2008-10-28
115
+
116
+ By making Annotations a module, it can not be used in only the clases
117
+ it is needed.
118
+
119
+ Changes:
120
+
121
+ * Annotations is a module rather than a core extenstion to Module.
122
+
123
+
124
+ ## 0.1.1 / 2008-10-17
125
+
126
+ Ahoy, mate! This is the first release of Anise.
127
+
128
+ Changes:
129
+
130
+ * initial release
131
+
data/LICENSE.txt ADDED
@@ -0,0 +1,23 @@
1
+ (BSD-2-Clause License)
2
+
3
+ Redistribution and use in source and binary forms, with or without
4
+ modification, are permitted provided that the following conditions are met:
5
+
6
+ * Redistributions of source code must retain the above copyright notice,
7
+ this list of conditions and the following disclaimer.
8
+
9
+ * Redistributions in binary form must reproduce the above copyright notice,
10
+ this list of conditions and the following disclaimer in the documentation
11
+ and/or other materials provided with the distribution.
12
+
13
+ THIS SOFTWARE IS PROVIDED BY Thomas Sawyer ``AS IS'' AND ANY EXPRESS
14
+ OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15
+ OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
16
+ NO EVENT SHALL Thomas Sawyer OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
17
+ INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
18
+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
20
+ OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
22
+ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
+
data/README.md ADDED
@@ -0,0 +1,195 @@
1
+ # Anise
2
+
3
+ [Source Code](https://github.com/rubyworks/anise) /
4
+ [Documentation](https://rubydoc.info/gems/anise) /
5
+ [Report Issue](https://github.com/rubyworks/anise/issues)    
6
+ [![CI](https://github.com/rubyworks/anise/actions/workflows/ci.yml/badge.svg)](https://github.com/rubyworks/anise/actions/workflows/ci.yml)
7
+
8
+
9
+ ## *Dynamic Annotations for Ruby*
10
+
11
+ Anise is an Annotation System for the Ruby programming language.
12
+ Unlike most other annotations systems it is not a comment-based or
13
+ macro-based system that sits over-and-above the rest of the code.
14
+ Rather, Anise is a dynamic annotations system operating at runtime.
15
+
16
+ The following example briefly demonstrates all three major features. To use
17
+ any of them first require the `anise` library.
18
+
19
+ require 'anise'
20
+
21
+ General annotations are provided by the `Anise::Annotations` module.
22
+
23
+ class X
24
+ extend Anise::Annotations
25
+
26
+ ann :grape, :class=>String
27
+ end
28
+
29
+ X.ann(:grape, :class) #=> String
30
+
31
+ Annotated attributes can be easily added to a class via the `Annotative::Attributes`
32
+ module.
33
+
34
+ class X
35
+ extend Anise::Annotative::Attributes
36
+
37
+ attr :baz, Integer, :max => 10
38
+ end
39
+
40
+ X.ann(:baz) #=> {:class=>Integer, :max=>10}
41
+
42
+ Method annotations can be had via the `Annotative::Methods` module.
43
+
44
+ class X
45
+ extend Anise::Annotative::Methods
46
+
47
+ def self.doc(string)
48
+ method_annotation(:doc=>string)
49
+ end
50
+
51
+ doc "This is an entry."
52
+
53
+ def bar
54
+ # ...
55
+ end
56
+ end
57
+
58
+ X.ann(:bar) #=> {:doc=>"This is an entry."}
59
+
60
+ An attribute's `:default` annotation is its value until it is set. Each
61
+ object gets its own copy, and a Proc is called in the object for the value.
62
+
63
+ class X
64
+ extend Anise::Annotative::Attributes
65
+
66
+ attr_accessor :list, :default => []
67
+ attr_accessor :size, :default => proc{ list.size }
68
+ end
69
+
70
+ X.new.list #=> []
71
+
72
+ One thing to know: in a class that extends `Annotative::Attributes`, a
73
+ `private` or `protected` line on its own does not apply to the attributes
74
+ after it. Ruby keeps that setting for the class body alone, where a method
75
+ that wraps `attr` cannot see it. Name the attributes instead:
76
+
77
+ class X
78
+ extend Anise::Annotative::Attributes
79
+
80
+ attr :a, :doc => "one way"
81
+ private :a
82
+
83
+ private attr(:b, :doc => "another")
84
+ end
85
+
86
+ Methods defined with `def` are not affected.
87
+
88
+ Any of these modules can be used in conjunction. Since both `Annotative::Methods`
89
+ and `Annotative::Attributes` preclude `Annotations` all three can be used by simply
90
+ using the later two.
91
+
92
+ class X
93
+ extend Anise::Annotative::Attributes
94
+ extend Anise::Annotative::Methods
95
+
96
+ ...
97
+ end
98
+
99
+ Note also that the `Anise` module is clean and contains only modules and classes
100
+ with detailed names starting with "Annotat-", so it can be included in your own
101
+ application's namespace, and its modules reached from there.
102
+
103
+ module MyApp
104
+ include Anise
105
+
106
+ class Foo
107
+ extend MyApp::Annotative::Attributes
108
+
109
+ ...
110
+ end
111
+ end
112
+
113
+
114
+ ## More
115
+
116
+ An annotation's reference can be anything, not only a method's name, and
117
+ annotations pass from a class or module to those that inherit from it.
118
+
119
+ class X
120
+ extend Anise::Annotations
121
+
122
+ ann :@count, :valid => lambda{ |x| x.is_a?(Integer) }
123
+ end
124
+
125
+ class Y < X
126
+ end
127
+
128
+ Y.ann(:@count, :valid) #=> the lambda
129
+
130
+ Annotations are kept in namespaces. The standard one is `ann`. Another is
131
+ named after a slash, which is how to write an annotator of your own:
132
+
133
+ class X
134
+ def self.doc(ref, *keys)
135
+ ann "#{ref}/doc", *keys
136
+ end
137
+
138
+ doc :grape, :text => "A fruit."
139
+ end
140
+
141
+ X.doc(:grape, :text) #=> "A fruit."
142
+
143
+ With `require 'anise/core_ext'`, the reference and namespace can also be
144
+ written `:grape/:doc`. That adds a `/` method to Symbol and String, so it
145
+ is left for you to ask for.
146
+
147
+ To have `ann` in every class and module without extending each one, use
148
+ `require 'anise/universal'`.
149
+
150
+ A class is told of each annotation by its `annotation_added(ref, namespace)`
151
+ class method, if it defines one.
152
+
153
+
154
+ ## Installation
155
+
156
+ It needs Ruby 3.1 or newer.
157
+
158
+ $ gem install anise
159
+
160
+
161
+ ## Documentation
162
+
163
+ The [demonstrations](https://github.com/rubyworks/anise/tree/master/demo)
164
+ in `demo/` walk through each feature. They are [QED](https://github.com/rubyworks/qed)
165
+ documents, so every example in them is run and checked.
166
+
167
+ The [API documentation](https://rubydoc.info/gems/anise) is on rubydoc.info.
168
+
169
+
170
+ ## Development
171
+
172
+ Anise is on [GitHub](https://github.com/rubyworks/anise). Problems,
173
+ questions and requests go in the
174
+ [issue tracker](https://github.com/rubyworks/anise/issues); to contribute,
175
+ fork the repository and send a pull request.
176
+
177
+ Anise has demonstrations, run with [QED](https://github.com/rubyworks/qed),
178
+ and unit tests, written with [Spectrum](https://github.com/rubyworks/spectrum)
179
+ on [Rubytest](https://github.com/rubyworks/rubytest):
180
+
181
+ $ bundle install
182
+ $ bundle exec rake demo
183
+ $ bundle exec rake test
184
+
185
+
186
+ ## Copyrights
187
+
188
+ Copyright (c) 2008 Rubyworks. All rights reserved.
189
+
190
+ This program is distributed under the terms of the
191
+ [BSD-2-Clause](https://spdx.org/licenses/BSD-2-Clause) license.
192
+
193
+ See LICENSE.txt for details.
194
+
195
+ This project was created on *2008-02-21*.
@@ -0,0 +1,136 @@
1
+ module Anise
2
+
3
+ module Annotations
4
+
5
+ # The {Annotations::Store} class tracks annotations on a per-class bases.
6
+ #
7
+ class Store
8
+
9
+ # Setup new Annotations instance.
10
+ #
11
+ # @param space [Class,Module]
12
+ # Class or Module to have annotations.
13
+ #
14
+ def initialize(space)
15
+ @space = space
16
+ @table = Hash.new { |h,k| h[k]={} }
17
+ end
18
+
19
+ # Ancestors of spaceal class/module.
20
+ def ancestors
21
+ @space.ancestors
22
+ end
23
+
24
+ # Annotations local to spaceal class/module.
25
+ def local
26
+ @table
27
+ end
28
+
29
+ # Access to local table.
30
+ def [](ref, ns=:ann)
31
+ @table[ns][ref]
32
+ end
33
+
34
+ # Lookup an annotation. Unlike `self[ref]`
35
+ # this provides a complete annotation _heritage_,
36
+ # pulling annotations of the same reference name
37
+ # from ancestor classes and modules.
38
+ #
39
+ # Unlike the other annotation methods, this method takes
40
+ # the `ref` argument before the `ns` argument. This is
41
+ # it allow `ns` to default to the common annotator `ann`.
42
+ #
43
+ # @param ref [Object] Annotation reference key.
44
+ #
45
+ # @param ns [Symbol] Annotation namespace.
46
+ #
47
+ def lookup(ref=nil, ns=:ann)
48
+ return @table if ref.nil?
49
+
50
+ ref, ns = ref.to_sym, (ns || :ann).to_sym
51
+
52
+ ann = {}
53
+ ancestors.reverse_each do |anc|
54
+ next unless anc.is_a?(Annotations)
55
+ if h = anc.annotations.local[ns][ref]
56
+ ann.merge!(h)
57
+ end
58
+ end
59
+ return ann
60
+ end
61
+
62
+ # Set or read annotations.
63
+ #
64
+ # IMPORTANT! Do not use this for in-place modifications.
65
+ # Use #annotate! instead.
66
+ #
67
+ # @pararm ns [Symbol] Annotation namespace.
68
+ #
69
+ # @param ref [Object] Annotation reference key.
70
+ #
71
+ # @since 0.7.0
72
+ def annotate(ns, ref, keys_or_class, keys=nil)
73
+ if Class === keys_or_class
74
+ keys ||= {}
75
+ keys[:class] = keys_or_class
76
+ else
77
+ keys = keys_or_class
78
+ end
79
+
80
+ if Hash === keys
81
+ update(ns, ref, keys)
82
+ else
83
+ key = keys.to_sym
84
+ lookup(ref, ns)[key]
85
+ end
86
+ end
87
+
88
+ # To change an annotation's value in place for a given class or module
89
+ # it first must be duplicated, otherwise the change may effect annotations
90
+ # in the class or module's ancestors.
91
+ #
92
+ # @pararm ns [Symbol] Annotation namespace.
93
+ #
94
+ # @param ref [Object] Annotation reference key.
95
+ #
96
+ # @since 0.7.0
97
+ def annotate!(ns, ref, keys_or_class, keys=nil)
98
+ if Class === keys_or_class
99
+ keys ||= {}
100
+ keys[:class] = keys_or_class
101
+ else
102
+ keys = keys_or_class
103
+ end
104
+
105
+ if Hash === keys
106
+ update(ns, ref, keys)
107
+ else
108
+ key = keys.to_sym
109
+ @table[ns][ref] ||= {}
110
+ begin
111
+ @table[ns][ref][key] = lookup(ref, ns)[key].dup
112
+ rescue TypeError
113
+ @table[ns][ref][key] = lookup(ref, ns)[key]
114
+ end
115
+ end
116
+ end
117
+
118
+ # Update annotations for a given namespace and reference.
119
+ def update(ns, ref, hash)
120
+ ref = ref.to_sym
121
+
122
+ @table[ns][ref] ||= {}
123
+
124
+ hash.each do |k,v|
125
+ @table[ns][ref][k.to_sym] = v
126
+ end
127
+
128
+ # callback
129
+ @space.annotation_added(ref, ns) #if method_defined?(:annotation_added)
130
+ end
131
+
132
+ end
133
+
134
+ end
135
+
136
+ end
@@ -0,0 +1,136 @@
1
+ module Anise
2
+
3
+ # TODO: The ann(x).name notation is kind of nice. Would like to add that
4
+ # back-in if reasonable.
5
+
6
+ # The Annotation provides a framework for annotating class and module related
7
+ # objects, typically symbols representing methods, with arbitrary metadata.
8
+ # These annotations do not do anything in themselves. They are simply data.
9
+ # But they can be put to good use. For instance an attribute validator might
10
+ # check for an annotation called :valid and test against it.
11
+ #
12
+ # The standard annotator is `:ann` and is the defualt value of annotating
13
+ # methods.
14
+ #
15
+ # class X
16
+ # extend Anise::Annotations
17
+ #
18
+ # ann :a, :desc => "A Number"
19
+ #
20
+ # attr :a
21
+ # end
22
+ #
23
+ # X.ann(:a, :desc) #=> "A Number"
24
+ #
25
+ # As stated, annotations need not only annotate methods, they are
26
+ # arbitrary, so they can be used for any purpose. For example, we
27
+ # may want to annotate instance variables.
28
+ #
29
+ # class X
30
+ # ann :@a, :valid => lambda{ |x| x.is_a?(Integer) }
31
+ #
32
+ # def validate
33
+ # instance_variables.each do |iv|
34
+ # if validator = self.class.ann(iv)[:valid]
35
+ # value = instance_variable_get(iv)
36
+ # unless validator.call(value)
37
+ # raise "Invalid value #{value} for #{iv}"
38
+ # end
39
+ # end
40
+ # end
41
+ # end
42
+ # end
43
+ #
44
+ # Or, we could even annotate the class itself.
45
+ #
46
+ # class X
47
+ # ann self, :valid => lambda{ |x| x.is_a?(Enumerable) }
48
+ # end
49
+ #
50
+ # Although annotations are arbitrary they are tied to the class or
51
+ # module they are defined against.
52
+ #
53
+ # Creating custom annotators used to entail using a special `#annotator` method,
54
+ # but this limited the way custom annotators could operate. The new way
55
+ # is to define a custom class method that calls the usual `#ann` method,
56
+ # but add in a namespace.
57
+ #
58
+ # class X
59
+ # def self.cool(ref, *keys)
60
+ # ann "#{ref}/cool", *keys
61
+ # end
62
+ #
63
+ # cool :a, :desc => "Awesome!"
64
+ # end
65
+ #
66
+ # X.cool(:a, :desc) #=> "Awesome!"
67
+ #
68
+ # The result is exactly the same as before, but now the custom annotator
69
+ # has complete control over the process.
70
+ #
71
+ module Annotations
72
+
73
+ #
74
+ # Access to a class or module's annotations.
75
+ #
76
+ def annotations
77
+ @annotations ||= Store.new(self)
78
+ end
79
+
80
+ #
81
+ # Callback method. This method is called for each new annotation.
82
+ #
83
+ def annotation_added(ref, ns=:ann)
84
+ super(ref, ns) if defined?(super)
85
+ end
86
+
87
+ # Get/set annotations.
88
+ #
89
+ # A namespace other than the standard one is given after a slash.
90
+ #
91
+ # @examples
92
+ # ann :ref, :key=>value
93
+ # ann 'ref/ns', :key=>value
94
+ #
95
+ # ann :ref, :key
96
+ # ann 'ref/ns', :key
97
+ #
98
+ def ann(ref, *keys)
99
+ if ref.to_s.index('/')
100
+ ref, ns = ref.to_s.split('/')
101
+ else
102
+ ns = :ann
103
+ end
104
+ ref, ns = ref.to_sym, ns.to_sym
105
+
106
+ if keys.empty?
107
+ annotations.lookup(ref, ns)
108
+ else
109
+ annotations.annotate(ns, ref, *keys)
110
+ end
111
+ end
112
+
113
+ #
114
+ # Get/set annotations in-place. Use this method instead of `#ann`
115
+ # when performing mass updates.
116
+ #
117
+ def ann!(ref, *keys)
118
+ if ref.to_s.index('/')
119
+ ref, ns = ref.to_s.split('/')
120
+ else
121
+ ns = :ann
122
+ end
123
+ ref, ns = ref.to_sym, ns.to_sym
124
+
125
+ if keys.empty?
126
+ annotations[ref, ns]
127
+ else
128
+ annotations.annotate!(ns, ref, *keys)
129
+ end
130
+ end
131
+
132
+ end
133
+
134
+ end
135
+
136
+ # Copyright (c) 2006 Rubyworks. All rights reserved. (BSD-2-Clause License)