anise 0.7.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 +7 -0
- data/HISTORY.md +41 -0
- data/LICENSE.txt +1 -5
- data/README.md +108 -42
- data/lib/anise/annotations.rb +6 -2
- data/lib/anise/annotative/attributes.rb +87 -16
- data/lib/anise/annotative/methods.rb +9 -11
- data/lib/anise/annotative/variables.rb +6 -5
- data/lib/anise/core_ext.rb +10 -27
- data/lib/anise/version.rb +2 -15
- data/lib/anise.rb +0 -36
- metadata +88 -70
- data/.ruby +0 -62
- data/.yardopts +0 -7
- data/DEMO.md +0 -242
- data/demo/01_annotations.md +0 -81
- data/demo/03_attributes.md +0 -14
- data/demo/04_methods.md +0 -50
- data/demo/05_variables.md +0 -45
- data/demo/applique/ae.rb +0 -1
- data/demo/applique/anise.rb +0 -1
- data/demo/toplevel/01_annotations.md +0 -26
- data/demo/toplevel/03_attributes.md +0 -20
- data/lib/anise.yml +0 -62
- data/test/case_annotations.rb +0 -173
- data/test/case_attributes.rb +0 -46
- data/test/case_combined_usage.rb +0 -341
- data/test/case_methods.rb +0 -36
- data/test/case_variables.rb +0 -22
- data/test/helper.rb +0 -2
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
CHANGED
|
@@ -1,5 +1,46 @@
|
|
|
1
1
|
# RELEASE HISTORY
|
|
2
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
|
+
|
|
3
44
|
## 0.7.0 / 2012-03-26
|
|
4
45
|
|
|
5
46
|
This release has some major API changes. Most significantly a number
|
data/LICENSE.txt
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2008 Rubyworks, Thomas Sawyer
|
|
4
|
-
|
|
5
|
-
(spdx) BSD-2-Clause License
|
|
1
|
+
(BSD-2-Clause License)
|
|
6
2
|
|
|
7
3
|
Redistribution and use in source and binary forms, with or without
|
|
8
4
|
modification, are permitted provided that the following conditions are met:
|
data/README.md
CHANGED
|
@@ -1,38 +1,18 @@
|
|
|
1
1
|
# Anise
|
|
2
2
|
|
|
3
|
-
[
|
|
4
|
-
[
|
|
5
|
-
[
|
|
6
|
-
[
|
|
7
|
-
[IRC Channel](http://chat.us.freenode.new/rubyworks)
|
|
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
|
+
[](https://github.com/rubyworks/anise/actions/workflows/ci.yml)
|
|
8
7
|
|
|
9
|
-
[](http://travis-ci.org/rubyworks/anise)
|
|
10
8
|
|
|
11
|
-
|
|
12
|
-
## Introduction
|
|
9
|
+
## *Dynamic Annotations for Ruby*
|
|
13
10
|
|
|
14
11
|
Anise is an Annotation System for the Ruby programming language.
|
|
15
12
|
Unlike most other annotations systems it is not a comment-based or
|
|
16
13
|
macro-based system that sits over-and-above the rest of the code.
|
|
17
14
|
Rather, Anise is a dynamic annotations system operating at runtime.
|
|
18
15
|
|
|
19
|
-
|
|
20
|
-
## Installation
|
|
21
|
-
|
|
22
|
-
To install with RubyGems simply open a console and type:
|
|
23
|
-
|
|
24
|
-
gem install anise
|
|
25
|
-
|
|
26
|
-
To manually install you will need Setup.rb (see http://setup.rubyforge.org).
|
|
27
|
-
Then download the tarball package and do:
|
|
28
|
-
|
|
29
|
-
$ tar -xvzf anise-0.2.0.tgz
|
|
30
|
-
$ cd anise-0.2.0
|
|
31
|
-
$ sudo setup.rb all
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
## Instruction
|
|
35
|
-
|
|
36
16
|
The following example briefly demonstrates all three major features. To use
|
|
37
17
|
any of them first require the `anise` library.
|
|
38
18
|
|
|
@@ -59,7 +39,7 @@ module.
|
|
|
59
39
|
|
|
60
40
|
X.ann(:baz) #=> {:class=>Integer, :max=>10}
|
|
61
41
|
|
|
62
|
-
|
|
42
|
+
Method annotations can be had via the `Annotative::Methods` module.
|
|
63
43
|
|
|
64
44
|
class X
|
|
65
45
|
extend Anise::Annotative::Methods
|
|
@@ -77,8 +57,36 @@ Mewthod annotations can be had via the `AnnotatedMethods` module.
|
|
|
77
57
|
|
|
78
58
|
X.ann(:bar) #=> {:doc=>"This is an entry."}
|
|
79
59
|
|
|
80
|
-
|
|
81
|
-
and
|
|
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
|
|
82
90
|
using the later two.
|
|
83
91
|
|
|
84
92
|
class X
|
|
@@ -89,41 +97,99 @@ using the later two.
|
|
|
89
97
|
end
|
|
90
98
|
|
|
91
99
|
Note also that the `Anise` module is clean and contains only modules and classes
|
|
92
|
-
with detailed names starting
|
|
93
|
-
|
|
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.
|
|
94
102
|
|
|
95
103
|
module MyApp
|
|
96
104
|
include Anise
|
|
97
105
|
|
|
98
106
|
class Foo
|
|
99
|
-
extend Annotative::Attributes
|
|
107
|
+
extend MyApp::Annotative::Attributes
|
|
100
108
|
|
|
101
109
|
...
|
|
102
110
|
end
|
|
103
111
|
end
|
|
104
112
|
|
|
105
113
|
|
|
106
|
-
##
|
|
114
|
+
## More
|
|
107
115
|
|
|
108
|
-
|
|
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.
|
|
109
118
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
[RubyTest](http://rubyworks.github.com/rubytest).
|
|
119
|
+
class X
|
|
120
|
+
extend Anise::Annotations
|
|
113
121
|
|
|
114
|
-
|
|
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."
|
|
115
142
|
|
|
116
|
-
|
|
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.
|
|
117
146
|
|
|
118
|
-
To
|
|
147
|
+
To have `ann` in every class and module without extending each one, use
|
|
148
|
+
`require 'anise/universal'`.
|
|
119
149
|
|
|
120
|
-
|
|
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
|
|
121
184
|
|
|
122
185
|
|
|
123
186
|
## Copyrights
|
|
124
187
|
|
|
125
188
|
Copyright (c) 2008 Rubyworks. All rights reserved.
|
|
126
189
|
|
|
127
|
-
This program is distributed under the terms of the
|
|
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.
|
|
128
194
|
|
|
129
|
-
|
|
195
|
+
This project was created on *2008-02-21*.
|
data/lib/anise/annotations.rb
CHANGED
|
@@ -59,6 +59,8 @@ module Anise
|
|
|
59
59
|
# def self.cool(ref, *keys)
|
|
60
60
|
# ann "#{ref}/cool", *keys
|
|
61
61
|
# end
|
|
62
|
+
#
|
|
63
|
+
# cool :a, :desc => "Awesome!"
|
|
62
64
|
# end
|
|
63
65
|
#
|
|
64
66
|
# X.cool(:a, :desc) #=> "Awesome!"
|
|
@@ -84,12 +86,14 @@ module Anise
|
|
|
84
86
|
|
|
85
87
|
# Get/set annotations.
|
|
86
88
|
#
|
|
89
|
+
# A namespace other than the standard one is given after a slash.
|
|
90
|
+
#
|
|
87
91
|
# @examples
|
|
88
92
|
# ann :ref, :key=>value
|
|
89
|
-
# ann
|
|
93
|
+
# ann 'ref/ns', :key=>value
|
|
90
94
|
#
|
|
91
95
|
# ann :ref, :key
|
|
92
|
-
# ann
|
|
96
|
+
# ann 'ref/ns', :key
|
|
93
97
|
#
|
|
94
98
|
def ann(ref, *keys)
|
|
95
99
|
if ref.to_s.index('/')
|
|
@@ -17,6 +17,20 @@ module Anise
|
|
|
17
17
|
#
|
|
18
18
|
# See {Annotation} module for more information.
|
|
19
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
|
+
#
|
|
20
34
|
# @todo Currently annotated attributes alwasy use the standard
|
|
21
35
|
# annotator (:ann). In the future we might make this customizable.
|
|
22
36
|
#
|
|
@@ -32,22 +46,33 @@ module Anise
|
|
|
32
46
|
# The class or module to get features.
|
|
33
47
|
#
|
|
34
48
|
def self.extended(base)
|
|
35
|
-
#inheritor :instance_attributes, [], :|
|
|
36
49
|
base_class = (class << base; self; end)
|
|
37
|
-
|
|
38
|
-
base.attribute_methods.each do |attr_method|
|
|
50
|
+
attribute_methods(base).each do |attr_method|
|
|
39
51
|
define_annotated_attribute(base_class, attr_method)
|
|
40
52
|
end
|
|
41
53
|
end
|
|
42
54
|
|
|
43
|
-
#
|
|
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
|
|
44
67
|
|
|
45
68
|
#
|
|
46
69
|
# Define an annotated attribute method, given an existing
|
|
47
|
-
# non-annotated attribute method.
|
|
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.
|
|
48
73
|
#
|
|
49
74
|
def self.define_annotated_attribute(base, attr_method_name)
|
|
50
|
-
|
|
75
|
+
wrapper = Module.new do
|
|
51
76
|
define_method(attr_method_name) do |*args|
|
|
52
77
|
args.flatten!
|
|
53
78
|
|
|
@@ -60,12 +85,16 @@ module Anise
|
|
|
60
85
|
if args.empty? # hash mode
|
|
61
86
|
harg.each { |a,h| __send__(attr_method_name,a,h) }
|
|
62
87
|
else
|
|
63
|
-
|
|
88
|
+
harg[:class] = args.pop if args.last.is_a?(Class)
|
|
64
89
|
|
|
65
|
-
super(*args)
|
|
90
|
+
made = super(*args)
|
|
66
91
|
|
|
67
92
|
args.each{|a| ann(a.to_sym,harg)}
|
|
68
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
|
+
|
|
69
98
|
instance_attributes!.concat(args) #merge!
|
|
70
99
|
|
|
71
100
|
# Use this callback to customize for your needs.
|
|
@@ -73,11 +102,54 @@ module Anise
|
|
|
73
102
|
attr_callback(self, args, harg)
|
|
74
103
|
end
|
|
75
104
|
|
|
76
|
-
#
|
|
77
|
-
|
|
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
|
|
78
113
|
end
|
|
79
114
|
end
|
|
80
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
|
|
81
153
|
end
|
|
82
154
|
|
|
83
155
|
#
|
|
@@ -86,7 +158,7 @@ module Anise
|
|
|
86
158
|
def instance_attributes
|
|
87
159
|
a = []
|
|
88
160
|
ancestors.each do |anc|
|
|
89
|
-
next unless
|
|
161
|
+
next unless Attributes === anc
|
|
90
162
|
if x = anc.instance_attributes!
|
|
91
163
|
a |= x
|
|
92
164
|
end
|
|
@@ -107,10 +179,10 @@ module Anise
|
|
|
107
179
|
# class MyClass
|
|
108
180
|
# attr_accessor :test
|
|
109
181
|
# attr_accessor :name, String, :doc => 'Hello'
|
|
110
|
-
# attr_accessor :age,
|
|
182
|
+
# attr_accessor :age, Integer
|
|
111
183
|
# end
|
|
112
184
|
#
|
|
113
|
-
# MyClass.instance_attributes # => [:test, :name, :age
|
|
185
|
+
# MyClass.instance_attributes # => [:test, :name, :age]
|
|
114
186
|
# MyClass.classified_attributes # => [:name, :age]
|
|
115
187
|
#
|
|
116
188
|
def classified_attributes
|
|
@@ -120,9 +192,8 @@ module Anise
|
|
|
120
192
|
end
|
|
121
193
|
|
|
122
194
|
#
|
|
123
|
-
# This
|
|
124
|
-
#
|
|
125
|
-
# which gets rid of the secondary argument (or was suppose to!).
|
|
195
|
+
# This adjusts #attr so that it can take annotations, and still
|
|
196
|
+
# takes the old boolean argument for a writer.
|
|
126
197
|
#
|
|
127
198
|
def attr(*args)
|
|
128
199
|
args.flatten!
|
|
@@ -2,8 +2,6 @@ module Anise
|
|
|
2
2
|
|
|
3
3
|
module Annotative
|
|
4
4
|
|
|
5
|
-
# TODO: Ensure thread-safety of <code>@_pending_annotations</code> variable.
|
|
6
|
-
|
|
7
5
|
# The Annotator::Method module allows for the creation of annotations
|
|
8
6
|
# which attach to the next method defined.
|
|
9
7
|
#
|
|
@@ -46,10 +44,10 @@ module Anise
|
|
|
46
44
|
include Annotations
|
|
47
45
|
|
|
48
46
|
#
|
|
49
|
-
#
|
|
47
|
+
# The annotations waiting for the next method to be defined.
|
|
50
48
|
#
|
|
51
|
-
def
|
|
52
|
-
@_pending_annotations ||=
|
|
49
|
+
def pending_annotations
|
|
50
|
+
@_pending_annotations ||= []
|
|
53
51
|
end
|
|
54
52
|
|
|
55
53
|
#
|
|
@@ -65,7 +63,7 @@ module Anise
|
|
|
65
63
|
(class << self; self; end).module_eval do
|
|
66
64
|
define_method(name) do |*args|
|
|
67
65
|
anns = { name => (args.size > 1 ? args : args.first) }
|
|
68
|
-
|
|
66
|
+
pending_annotations << [anns, block]
|
|
69
67
|
end
|
|
70
68
|
end
|
|
71
69
|
end
|
|
@@ -93,7 +91,7 @@ module Anise
|
|
|
93
91
|
#
|
|
94
92
|
def method_annotation(*args, &block)
|
|
95
93
|
anns = (Hash === args.last ? args.pop : {})
|
|
96
|
-
|
|
94
|
+
pending_annotations << [anns, block]
|
|
97
95
|
end
|
|
98
96
|
|
|
99
97
|
#
|
|
@@ -103,8 +101,9 @@ module Anise
|
|
|
103
101
|
# The name of the method added.
|
|
104
102
|
#
|
|
105
103
|
def method_added(sym)
|
|
106
|
-
|
|
107
|
-
|
|
104
|
+
pending = pending_annotations
|
|
105
|
+
@_pending_annotations = []
|
|
106
|
+
pending.each do |anns, block|
|
|
108
107
|
if block
|
|
109
108
|
block.call(sym)
|
|
110
109
|
else
|
|
@@ -114,11 +113,10 @@ module Anise
|
|
|
114
113
|
else
|
|
115
114
|
ns = :ann
|
|
116
115
|
end
|
|
117
|
-
ann(sym
|
|
116
|
+
ann("#{sym}/#{ns}", name=>value)
|
|
118
117
|
end
|
|
119
118
|
end
|
|
120
119
|
end
|
|
121
|
-
Methods.pending_annotations[self] = []
|
|
122
120
|
super if defined?(super)
|
|
123
121
|
end
|
|
124
122
|
|
|
@@ -63,7 +63,7 @@ module Anise
|
|
|
63
63
|
raise ArgumentError, "not a valid instance variable -- #{iv}"
|
|
64
64
|
end
|
|
65
65
|
else
|
|
66
|
-
variable_annotator(iv,
|
|
66
|
+
variable_annotator(iv, &block)
|
|
67
67
|
end
|
|
68
68
|
end
|
|
69
69
|
|
|
@@ -78,14 +78,15 @@ module Anise
|
|
|
78
78
|
else
|
|
79
79
|
ns = :ann
|
|
80
80
|
end
|
|
81
|
-
|
|
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)
|
|
82
85
|
if block
|
|
83
86
|
block.call(sym, value)
|
|
84
87
|
else
|
|
85
|
-
ann(sym
|
|
88
|
+
ann("#{sym}/#{ns}", iv=>value)
|
|
86
89
|
end
|
|
87
|
-
# TODO: can we undefine the instance variable?
|
|
88
|
-
instance_variable_set(iv, nil)
|
|
89
90
|
end
|
|
90
91
|
super(sym) if defined?(super)
|
|
91
92
|
end
|
data/lib/anise/core_ext.rb
CHANGED
|
@@ -1,25 +1,9 @@
|
|
|
1
|
-
#require '
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
# to work with attribute annotations.
|
|
8
|
-
#
|
|
9
|
-
def attribute_methods
|
|
10
|
-
list = []
|
|
11
|
-
public_methods(true).each do |m|
|
|
12
|
-
list << m if m.to_s =~ /^attr_/
|
|
13
|
-
end
|
|
14
|
-
protected_methods(true).each do |m|
|
|
15
|
-
list << m if m.to_s =~ /^attr_/
|
|
16
|
-
end
|
|
17
|
-
private_methods(true).each do |m|
|
|
18
|
-
list << m if m.to_s =~ /^attr_/
|
|
19
|
-
end
|
|
20
|
-
return list
|
|
21
|
-
end
|
|
22
|
-
end
|
|
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', ...`.
|
|
23
7
|
|
|
24
8
|
class Symbol
|
|
25
9
|
#
|
|
@@ -27,10 +11,10 @@ class Symbol
|
|
|
27
11
|
#
|
|
28
12
|
# @example
|
|
29
13
|
# :foo/:bar #=> :'foo/bar'
|
|
30
|
-
#
|
|
14
|
+
#
|
|
31
15
|
def /(other)
|
|
32
16
|
"#{self}/#{other}".to_sym
|
|
33
|
-
end
|
|
17
|
+
end unless method_defined?(:/)
|
|
34
18
|
end
|
|
35
19
|
|
|
36
20
|
class String
|
|
@@ -39,11 +23,10 @@ class String
|
|
|
39
23
|
#
|
|
40
24
|
# @example
|
|
41
25
|
# 'foo'/'bar' #=> 'foo/bar'
|
|
42
|
-
#
|
|
26
|
+
#
|
|
43
27
|
def /(other)
|
|
44
28
|
"#{self}/#{other}"
|
|
45
|
-
end
|
|
29
|
+
end unless method_defined?(:/)
|
|
46
30
|
end
|
|
47
31
|
|
|
48
32
|
# Copyright (c) 2006 Rubyworks. All rights reserved. (BSD-2-Clause License)
|
|
49
|
-
|
data/lib/anise/version.rb
CHANGED
|
@@ -1,17 +1,4 @@
|
|
|
1
1
|
module Anise
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
def self.metadata
|
|
5
|
-
@metadata ||= (
|
|
6
|
-
require 'yaml'
|
|
7
|
-
YAML.load(File.new(File.dirname(__FILE__) + '/../anise.yml'))
|
|
8
|
-
)
|
|
9
|
-
end
|
|
10
|
-
|
|
11
|
-
#
|
|
12
|
-
def self.const_missing(name)
|
|
13
|
-
metadata[name.to_s.downcase] || super(name)
|
|
14
|
-
end
|
|
15
|
-
|
|
2
|
+
# The version of this library.
|
|
3
|
+
VERSION = '0.8.0'
|
|
16
4
|
end
|
|
17
|
-
|
data/lib/anise.rb
CHANGED
|
@@ -1,41 +1,5 @@
|
|
|
1
|
-
# Dynamic Annotations for Ruby.
|
|
2
|
-
#
|
|
3
|
-
# require 'anise'
|
|
4
|
-
#
|
|
5
|
-
# Provides annotations:
|
|
6
|
-
#
|
|
7
|
-
# class X
|
|
8
|
-
# extend Anise::Annotations
|
|
9
|
-
#
|
|
10
|
-
# ann :foo, :class=>String
|
|
11
|
-
# end
|
|
12
|
-
#
|
|
13
|
-
# Provides method annotations:
|
|
14
|
-
#
|
|
15
|
-
# class Y
|
|
16
|
-
# extend Anise::Annotator::Method
|
|
17
|
-
#
|
|
18
|
-
# def self.doc(string)
|
|
19
|
-
# method_annotation(:doc=>string)
|
|
20
|
-
# end
|
|
21
|
-
#
|
|
22
|
-
# doc "foo is cool"
|
|
23
|
-
# def foo
|
|
24
|
-
# # ...
|
|
25
|
-
# end
|
|
26
|
-
# end
|
|
27
|
-
#
|
|
28
|
-
# Provides annotated attributes:
|
|
29
|
-
#
|
|
30
|
-
# class Z
|
|
31
|
-
# extend Anise::Annotator::Attribute
|
|
32
|
-
#
|
|
33
|
-
# attr :bar, Integer, :max=>10
|
|
34
|
-
# end
|
|
35
|
-
#
|
|
36
1
|
module Anise
|
|
37
2
|
require 'anise/version'
|
|
38
|
-
require 'anise/core_ext'
|
|
39
3
|
require 'anise/annotations'
|
|
40
4
|
require 'anise/annotations/store'
|
|
41
5
|
require 'anise/annotative'
|