anise 0.1.1
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/COPYING +168 -0
- data/HISTORY +16 -0
- data/MANIFEST +26 -0
- data/README +66 -0
- data/RELEASE +11 -0
- data/VERSION +1 -0
- data/lib/anise/annotatable.rb +60 -0
- data/lib/anise/annotations.rb +114 -0
- data/lib/anise/attributes.rb +122 -0
- data/lib/anise.rb +1 -0
- data/meta/abstract +1 -0
- data/meta/authors +1 -0
- data/meta/contact +1 -0
- data/meta/created +1 -0
- data/meta/homepage +1 -0
- data/meta/license +1 -0
- data/meta/require +1 -0
- data/meta/summary +1 -0
- data/test/suite.rb +8 -0
- data/test/test_annotatable.rb +28 -0
- data/test/test_attribute.rb +16 -0
- data/test/test_toplevel_annotatable.rb +32 -0
- data/test/test_toplevel_attribute.rb +27 -0
- metadata +102 -0
data/COPYING
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
.
|
|
2
|
+
|
|
3
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
|
4
|
+
Version 3, 29 June 2007
|
|
5
|
+
|
|
6
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
|
7
|
+
Everyone is permitted to copy and distribute verbatim copies
|
|
8
|
+
of this license document, but changing it is not allowed.
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
This version of the GNU Lesser General Public License incorporates
|
|
12
|
+
the terms and conditions of version 3 of the GNU General Public
|
|
13
|
+
License, supplemented by the additional permissions listed below.
|
|
14
|
+
|
|
15
|
+
0. Additional Definitions.
|
|
16
|
+
|
|
17
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
|
18
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
|
19
|
+
General Public License.
|
|
20
|
+
|
|
21
|
+
"The Library" refers to a covered work governed by this License,
|
|
22
|
+
other than an Application or a Combined Work as defined below.
|
|
23
|
+
|
|
24
|
+
An "Application" is any work that makes use of an interface provided
|
|
25
|
+
by the Library, but which is not otherwise based on the Library.
|
|
26
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
|
27
|
+
of using an interface provided by the Library.
|
|
28
|
+
|
|
29
|
+
A "Combined Work" is a work produced by combining or linking an
|
|
30
|
+
Application with the Library. The particular version of the Library
|
|
31
|
+
with which the Combined Work was made is also called the "Linked
|
|
32
|
+
Version".
|
|
33
|
+
|
|
34
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
|
35
|
+
Corresponding Source for the Combined Work, excluding any source code
|
|
36
|
+
for portions of the Combined Work that, considered in isolation, are
|
|
37
|
+
based on the Application, and not on the Linked Version.
|
|
38
|
+
|
|
39
|
+
The "Corresponding Application Code" for a Combined Work means the
|
|
40
|
+
object code and/or source code for the Application, including any data
|
|
41
|
+
and utility programs needed for reproducing the Combined Work from the
|
|
42
|
+
Application, but excluding the System Libraries of the Combined Work.
|
|
43
|
+
|
|
44
|
+
1. Exception to Section 3 of the GNU GPL.
|
|
45
|
+
|
|
46
|
+
You may convey a covered work under sections 3 and 4 of this License
|
|
47
|
+
without being bound by section 3 of the GNU GPL.
|
|
48
|
+
|
|
49
|
+
2. Conveying Modified Versions.
|
|
50
|
+
|
|
51
|
+
If you modify a copy of the Library, and, in your modifications, a
|
|
52
|
+
facility refers to a function or data to be supplied by an Application
|
|
53
|
+
that uses the facility (other than as an argument passed when the
|
|
54
|
+
facility is invoked), then you may convey a copy of the modified
|
|
55
|
+
version:
|
|
56
|
+
|
|
57
|
+
a) under this License, provided that you make a good faith effort to
|
|
58
|
+
ensure that, in the event an Application does not supply the
|
|
59
|
+
function or data, the facility still operates, and performs
|
|
60
|
+
whatever part of its purpose remains meaningful, or
|
|
61
|
+
|
|
62
|
+
b) under the GNU GPL, with none of the additional permissions of
|
|
63
|
+
this License applicable to that copy.
|
|
64
|
+
|
|
65
|
+
3. Object Code Incorporating Material from Library Header Files.
|
|
66
|
+
|
|
67
|
+
The object code form of an Application may incorporate material from
|
|
68
|
+
a header file that is part of the Library. You may convey such object
|
|
69
|
+
code under terms of your choice, provided that, if the incorporated
|
|
70
|
+
material is not limited to numerical parameters, data structure
|
|
71
|
+
layouts and accessors, or small macros, inline functions and templates
|
|
72
|
+
(ten or fewer lines in length), you do both of the following:
|
|
73
|
+
|
|
74
|
+
a) Give prominent notice with each copy of the object code that the
|
|
75
|
+
Library is used in it and that the Library and its use are
|
|
76
|
+
covered by this License.
|
|
77
|
+
|
|
78
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
|
79
|
+
document.
|
|
80
|
+
|
|
81
|
+
4. Combined Works.
|
|
82
|
+
|
|
83
|
+
You may convey a Combined Work under terms of your choice that,
|
|
84
|
+
taken together, effectively do not restrict modification of the
|
|
85
|
+
portions of the Library contained in the Combined Work and reverse
|
|
86
|
+
engineering for debugging such modifications, if you also do each of
|
|
87
|
+
the following:
|
|
88
|
+
|
|
89
|
+
a) Give prominent notice with each copy of the Combined Work that
|
|
90
|
+
the Library is used in it and that the Library and its use are
|
|
91
|
+
covered by this License.
|
|
92
|
+
|
|
93
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
|
94
|
+
document.
|
|
95
|
+
|
|
96
|
+
c) For a Combined Work that displays copyright notices during
|
|
97
|
+
execution, include the copyright notice for the Library among
|
|
98
|
+
these notices, as well as a reference directing the user to the
|
|
99
|
+
copies of the GNU GPL and this license document.
|
|
100
|
+
|
|
101
|
+
d) Do one of the following:
|
|
102
|
+
|
|
103
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
|
104
|
+
License, and the Corresponding Application Code in a form
|
|
105
|
+
suitable for, and under terms that permit, the user to
|
|
106
|
+
recombine or relink the Application with a modified version of
|
|
107
|
+
the Linked Version to produce a modified Combined Work, in the
|
|
108
|
+
manner specified by section 6 of the GNU GPL for conveying
|
|
109
|
+
Corresponding Source.
|
|
110
|
+
|
|
111
|
+
1) Use a suitable shared library mechanism for linking with the
|
|
112
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
|
113
|
+
a copy of the Library already present on the user's computer
|
|
114
|
+
system, and (b) will operate properly with a modified version
|
|
115
|
+
of the Library that is interface-compatible with the Linked
|
|
116
|
+
Version.
|
|
117
|
+
|
|
118
|
+
e) Provide Installation Information, but only if you would otherwise
|
|
119
|
+
be required to provide such information under section 6 of the
|
|
120
|
+
GNU GPL, and only to the extent that such information is
|
|
121
|
+
necessary to install and execute a modified version of the
|
|
122
|
+
Combined Work produced by recombining or relinking the
|
|
123
|
+
Application with a modified version of the Linked Version. (If
|
|
124
|
+
you use option 4d0, the Installation Information must accompany
|
|
125
|
+
the Minimal Corresponding Source and Corresponding Application
|
|
126
|
+
Code. If you use option 4d1, you must provide the Installation
|
|
127
|
+
Information in the manner specified by section 6 of the GNU GPL
|
|
128
|
+
for conveying Corresponding Source.)
|
|
129
|
+
|
|
130
|
+
5. Combined Libraries.
|
|
131
|
+
|
|
132
|
+
You may place library facilities that are a work based on the
|
|
133
|
+
Library side by side in a single library together with other library
|
|
134
|
+
facilities that are not Applications and are not covered by this
|
|
135
|
+
License, and convey such a combined library under terms of your
|
|
136
|
+
choice, if you do both of the following:
|
|
137
|
+
|
|
138
|
+
a) Accompany the combined library with a copy of the same work based
|
|
139
|
+
on the Library, uncombined with any other library facilities,
|
|
140
|
+
conveyed under the terms of this License.
|
|
141
|
+
|
|
142
|
+
b) Give prominent notice with the combined library that part of it
|
|
143
|
+
is a work based on the Library, and explaining where to find the
|
|
144
|
+
accompanying uncombined form of the same work.
|
|
145
|
+
|
|
146
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
|
147
|
+
|
|
148
|
+
The Free Software Foundation may publish revised and/or new versions
|
|
149
|
+
of the GNU Lesser General Public License from time to time. Such new
|
|
150
|
+
versions will be similar in spirit to the present version, but may
|
|
151
|
+
differ in detail to address new problems or concerns.
|
|
152
|
+
|
|
153
|
+
Each version is given a distinguishing version number. If the
|
|
154
|
+
Library as you received it specifies that a certain numbered version
|
|
155
|
+
of the GNU Lesser General Public License "or any later version"
|
|
156
|
+
applies to it, you have the option of following the terms and
|
|
157
|
+
conditions either of that published version or of any later version
|
|
158
|
+
published by the Free Software Foundation. If the Library as you
|
|
159
|
+
received it does not specify a version number of the GNU Lesser
|
|
160
|
+
General Public License, you may choose any version of the GNU Lesser
|
|
161
|
+
General Public License ever published by the Free Software Foundation.
|
|
162
|
+
|
|
163
|
+
If the Library as you received it specifies that a proxy can decide
|
|
164
|
+
whether future versions of the GNU Lesser General Public License shall
|
|
165
|
+
apply, that proxy's public statement of acceptance of any version is
|
|
166
|
+
permanent authorization for you to choose that version for the
|
|
167
|
+
Library.
|
|
168
|
+
|
data/HISTORY
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
== 0.0.0 // 2008-03-04
|
|
2
|
+
|
|
3
|
+
* Started project.
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
# Keep a running list of changes by version and date here.
|
|
7
|
+
# This is uploaded as the ChangeLog in the release process.
|
|
8
|
+
# It's better to keep a manual log, rather then use the log
|
|
9
|
+
# generated by the SCM because the SCM log will have gritty
|
|
10
|
+
# details with which end-users will not be concerned.
|
|
11
|
+
#
|
|
12
|
+
# If you want to use the SCM log anyway, you can of course
|
|
13
|
+
# do so. Just "cp log/Changelog.txt CHANGES" prior to release.
|
|
14
|
+
# (We might provide an automated option for this in the
|
|
15
|
+
# future, but for now its a manual task.)
|
|
16
|
+
|
data/MANIFEST
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
test
|
|
2
|
+
test/test_toplevel_attribute.rb
|
|
3
|
+
test/test_attribute.rb
|
|
4
|
+
test/test_annotatable.rb
|
|
5
|
+
test/suite.rb
|
|
6
|
+
test/test_toplevel_annotatable.rb
|
|
7
|
+
RELEASE
|
|
8
|
+
README
|
|
9
|
+
HISTORY
|
|
10
|
+
meta
|
|
11
|
+
meta/require
|
|
12
|
+
meta/created
|
|
13
|
+
meta/homepage
|
|
14
|
+
meta/summary
|
|
15
|
+
meta/abstract
|
|
16
|
+
meta/license
|
|
17
|
+
meta/authors
|
|
18
|
+
meta/contact
|
|
19
|
+
lib
|
|
20
|
+
lib/anise
|
|
21
|
+
lib/anise/attributes.rb
|
|
22
|
+
lib/anise/annotations.rb
|
|
23
|
+
lib/anise/annotatable.rb
|
|
24
|
+
lib/anise.rb
|
|
25
|
+
VERSION
|
|
26
|
+
COPYING
|
data/README
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
= Annotable
|
|
2
|
+
|
|
3
|
+
http://anise.rubyforge.org
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
== INTRODUCTION
|
|
7
|
+
|
|
8
|
+
Annotable is an Annotations Systems for the Ruby programming
|
|
9
|
+
lanaguage. Unlike other annotations system it is not a comment-based
|
|
10
|
+
system. Rather, it is a dynamic annotations system operating at runtime.
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
== RELEASE NOTES
|
|
14
|
+
|
|
15
|
+
Please see the RELEASE file.
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
== RECENT CHANGES
|
|
19
|
+
|
|
20
|
+
Please see the HISTORY file.
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
== INSTALLATION
|
|
24
|
+
|
|
25
|
+
To install with RubyGems simply open a console and type:
|
|
26
|
+
|
|
27
|
+
gem install annotable
|
|
28
|
+
|
|
29
|
+
To manually install you will need Setup.rb (see http://setup.rubyforge.org).
|
|
30
|
+
Then download the tarball package and do:
|
|
31
|
+
|
|
32
|
+
$ tar -xvzf anise-0.4.0.tgz
|
|
33
|
+
$ cd annotated-0.4.0.tgz
|
|
34
|
+
$ sudo setup.rb all
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
== TESTING
|
|
38
|
+
|
|
39
|
+
We recommend using Turn (http://turn.rubyforge.org) to run Anise's
|
|
40
|
+
unit tests.
|
|
41
|
+
|
|
42
|
+
$ turn test/test_*
|
|
43
|
+
|
|
44
|
+
But we have include a script test/suite.rb so you can easily run the
|
|
45
|
+
unit tests with the testrb command:
|
|
46
|
+
|
|
47
|
+
$ testrb test/suite.rb
|
|
48
|
+
|
|
49
|
+
or with Ruby alone:
|
|
50
|
+
|
|
51
|
+
$ ruby -rtest/unit test/suite.rb
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
== USAGE
|
|
55
|
+
|
|
56
|
+
For the moment please see the API documentation to learn how to use the library.
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
== LICENSE
|
|
60
|
+
|
|
61
|
+
Copyright (c) 2008 TigerOps
|
|
62
|
+
|
|
63
|
+
This program is ditributed under the terms of the LGPL License.
|
|
64
|
+
|
|
65
|
+
Please see COPYING file for details.
|
|
66
|
+
|
data/RELEASE
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
= Anise 0.1 hits the streets.
|
|
2
|
+
|
|
3
|
+
This is the initial release of Anise.
|
|
4
|
+
|
|
5
|
+
Anise is a spin off the the Facets annotations.rb library.
|
|
6
|
+
It includes the Annotations functionality, and adds
|
|
7
|
+
a mixin, Annotatable, that makes it easy to add new
|
|
8
|
+
annotations on the fly.
|
|
9
|
+
|
|
10
|
+
http://anise.rubyforge.org
|
|
11
|
+
|
data/VERSION
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
anise 0.1.1 beta (2008-09-27)
|
|
@@ -0,0 +1,60 @@
|
|
|
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
|
+
|
|
@@ -0,0 +1,114 @@
|
|
|
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
|
+
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
require 'facets/inheritor' # remove dependency?
|
|
2
|
+
|
|
3
|
+
module Anise
|
|
4
|
+
require 'anise/annotations'
|
|
5
|
+
|
|
6
|
+
module Annotatable
|
|
7
|
+
|
|
8
|
+
# = Annotated Attributes
|
|
9
|
+
#
|
|
10
|
+
# This framework modifies the major attr_* methods to allow easy addition of annotations.
|
|
11
|
+
# It modifies the built in attribute methods (attr, attr_reader, attr_writer and attr_accessor),
|
|
12
|
+
# to allow annotations to added to them directly rather than requireing a separate
|
|
13
|
+
# #ann statement.
|
|
14
|
+
#
|
|
15
|
+
# class X
|
|
16
|
+
# attr :a, :valid => lambda{ |x| x.is_a?(Integer) }
|
|
17
|
+
# end
|
|
18
|
+
#
|
|
19
|
+
# See annotation.rb for more information.
|
|
20
|
+
#
|
|
21
|
+
# NOTE This library was designed to be backward compatible with the standard versions of the
|
|
22
|
+
# same methods.
|
|
23
|
+
#
|
|
24
|
+
module Attributes
|
|
25
|
+
|
|
26
|
+
def self.append_features(base)
|
|
27
|
+
base.extend self
|
|
28
|
+
base.module_eval do
|
|
29
|
+
inheritor :instance_attributes, [], :|
|
|
30
|
+
|
|
31
|
+
annotatable_attribute_method(:attr_reader)
|
|
32
|
+
annotatable_attribute_method(:attr_writer)
|
|
33
|
+
annotatable_attribute_method(:attr_accessor)
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
#
|
|
38
|
+
def annotatable_attribute_method(attr_method_name)
|
|
39
|
+
attr_method = method(attr_method_name)
|
|
40
|
+
|
|
41
|
+
(class << self; self; end).instance_eval do
|
|
42
|
+
|
|
43
|
+
define_method(attr_method_name) do |*args|
|
|
44
|
+
|
|
45
|
+
args.flatten!
|
|
46
|
+
|
|
47
|
+
harg={}; while args.last.is_a?(Hash)
|
|
48
|
+
harg.update(args.pop)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
raise ArgumentError if args.empty? and harg.empty?
|
|
52
|
+
|
|
53
|
+
if args.empty? # hash mode
|
|
54
|
+
harg.each { |a,h| send(attr_method_name,a,h) }
|
|
55
|
+
else
|
|
56
|
+
klass = harg[:class] = args.pop if args.last.is_a?(Class)
|
|
57
|
+
|
|
58
|
+
attr_method.call(*args)
|
|
59
|
+
|
|
60
|
+
args.each { |a|
|
|
61
|
+
ann(a.to_sym,harg)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
instance_attributes!.concat(args) #merge!
|
|
65
|
+
|
|
66
|
+
# Use this callback to customize for your needs.
|
|
67
|
+
if respond_to?(:attr_callback)
|
|
68
|
+
attr_callback(self, args, harg)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# return the names of the attributes created
|
|
72
|
+
return args
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Return list of attributes that have a :class annotation.
|
|
81
|
+
#
|
|
82
|
+
# class MyClass
|
|
83
|
+
# attr_accessor :test
|
|
84
|
+
# attr_accessor :name, String, :doc => 'Hello'
|
|
85
|
+
# attr_accessor :age, Fixnum
|
|
86
|
+
# end
|
|
87
|
+
#
|
|
88
|
+
# MyClass.instance_attributes # => [:test, :name, :age, :body]
|
|
89
|
+
# MyClass.classified_attributes # => [:name, :age]
|
|
90
|
+
#
|
|
91
|
+
def classified_attributes
|
|
92
|
+
instance_attributes.find_all do |a|
|
|
93
|
+
self.ann(a, :class)
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# This define a simple adjustment to #attr to allow it to handle the boolean argument and
|
|
98
|
+
# to be able to accept attributes. It's backward compatible and is not needed for Ruby 1.9
|
|
99
|
+
# which gets rid of the secondary argument.
|
|
100
|
+
#
|
|
101
|
+
def attr(*args)
|
|
102
|
+
args.flatten!
|
|
103
|
+
case args.last
|
|
104
|
+
when TrueClass
|
|
105
|
+
args.pop
|
|
106
|
+
attr_accessor(*args)
|
|
107
|
+
when FalseClass
|
|
108
|
+
args.pop
|
|
109
|
+
attr_reader(*args)
|
|
110
|
+
else
|
|
111
|
+
attr_reader(*args)
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# Copyright (c) 2005, 2008 TigerOps
|
|
122
|
+
|
data/lib/anise.rb
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require 'anise/annotatable'
|
data/meta/abstract
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Annable is an annotations systems for the Ruby programming lanaguage.
|
data/meta/authors
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
trans <transfire@gmail.com>
|
data/meta/contact
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
tigerops-community@rubyforge.org
|
data/meta/created
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
2008-02-21
|
data/meta/homepage
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
http://
|
data/meta/license
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Ruby
|
data/meta/require
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
facets >2.4.0
|
data/meta/summary
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Dynamic Annotation for Ruby
|
data/test/suite.rb
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
require 'anise/annotatable'
|
|
2
|
+
|
|
3
|
+
class Test_Annotatable < Test::Unit::TestCase
|
|
4
|
+
|
|
5
|
+
include Anise::Annotatable
|
|
6
|
+
|
|
7
|
+
annotation :req
|
|
8
|
+
|
|
9
|
+
req 'r'
|
|
10
|
+
|
|
11
|
+
def a
|
|
12
|
+
"a"
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def test_annotated
|
|
16
|
+
assert_equal( {:req=>['r']}, self.class.ann(:a) )
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
req 's'
|
|
20
|
+
|
|
21
|
+
attr :b
|
|
22
|
+
|
|
23
|
+
def test_annotated
|
|
24
|
+
assert_equal( {:req=>['s']}, self.class.ann(:b) )
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
end
|
|
28
|
+
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
=begin
|
|
2
|
+
require 'annotable'
|
|
3
|
+
require 'test/unit'
|
|
4
|
+
|
|
5
|
+
include Annotatable
|
|
6
|
+
|
|
7
|
+
annotation :req
|
|
8
|
+
|
|
9
|
+
class Test_Annotatable < Test::Unit::TestCase
|
|
10
|
+
|
|
11
|
+
annotation :req
|
|
12
|
+
|
|
13
|
+
req 'r'
|
|
14
|
+
|
|
15
|
+
def a
|
|
16
|
+
"a"
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def test_annotated
|
|
20
|
+
assert_equal( {:req=>['r']}, self.class.ann(:a) )
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
req 's'
|
|
24
|
+
|
|
25
|
+
attr :b
|
|
26
|
+
|
|
27
|
+
def test_annotated
|
|
28
|
+
assert_equal( {:req=>['s']}, self.class.ann(:b) )
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
end
|
|
32
|
+
=end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
=begin
|
|
2
|
+
require 'annotable/attributes'
|
|
3
|
+
require 'test/unit'
|
|
4
|
+
|
|
5
|
+
include Annotatable::Attributes
|
|
6
|
+
|
|
7
|
+
$self = self
|
|
8
|
+
|
|
9
|
+
attr :b, :y => 1
|
|
10
|
+
|
|
11
|
+
class Test_Attribute_Toplevel < Test::Unit::TestCase
|
|
12
|
+
|
|
13
|
+
attr :q
|
|
14
|
+
|
|
15
|
+
attr :a, :x => 1
|
|
16
|
+
|
|
17
|
+
def test_attr_a
|
|
18
|
+
assert_equal( {:x=>1}, self.class.ann(:a) )
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def test_attr_b
|
|
22
|
+
assert_equal( {:y=>1}, $self.class.ann(:b) )
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
end
|
|
26
|
+
=end
|
|
27
|
+
|
metadata
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: anise
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- tigerops-community@rubyforge.org
|
|
8
|
+
- trans <transfire@gmail.com>
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
|
|
13
|
+
date: 2008-10-17 00:00:00 -04:00
|
|
14
|
+
default_executable:
|
|
15
|
+
dependencies:
|
|
16
|
+
- !ruby/object:Gem::Dependency
|
|
17
|
+
name: facets
|
|
18
|
+
type: :runtime
|
|
19
|
+
version_requirement:
|
|
20
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
21
|
+
requirements:
|
|
22
|
+
- - ">"
|
|
23
|
+
- !ruby/object:Gem::Version
|
|
24
|
+
version: 2.4.0
|
|
25
|
+
version:
|
|
26
|
+
description: Annable is an annotations systems for the Ruby programming lanaguage.
|
|
27
|
+
email:
|
|
28
|
+
executables: []
|
|
29
|
+
|
|
30
|
+
extensions: []
|
|
31
|
+
|
|
32
|
+
extra_rdoc_files:
|
|
33
|
+
- README
|
|
34
|
+
- MANIFEST
|
|
35
|
+
- RELEASE
|
|
36
|
+
- HISTORY
|
|
37
|
+
- VERSION
|
|
38
|
+
- COPYING
|
|
39
|
+
files:
|
|
40
|
+
- MANIFEST
|
|
41
|
+
- 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
|
+
- RELEASE
|
|
48
|
+
- README
|
|
49
|
+
- HISTORY
|
|
50
|
+
- meta
|
|
51
|
+
- meta/require
|
|
52
|
+
- meta/created
|
|
53
|
+
- meta/homepage
|
|
54
|
+
- meta/summary
|
|
55
|
+
- meta/abstract
|
|
56
|
+
- meta/license
|
|
57
|
+
- meta/authors
|
|
58
|
+
- 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
|
|
67
|
+
has_rdoc: true
|
|
68
|
+
homepage: http://
|
|
69
|
+
post_install_message:
|
|
70
|
+
rdoc_options:
|
|
71
|
+
- --inline-source
|
|
72
|
+
- --title
|
|
73
|
+
- anise api
|
|
74
|
+
- --main
|
|
75
|
+
- README
|
|
76
|
+
require_paths:
|
|
77
|
+
- lib
|
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - ">="
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: "0"
|
|
83
|
+
version:
|
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
85
|
+
requirements:
|
|
86
|
+
- - ">="
|
|
87
|
+
- !ruby/object:Gem::Version
|
|
88
|
+
version: "0"
|
|
89
|
+
version:
|
|
90
|
+
requirements: []
|
|
91
|
+
|
|
92
|
+
rubyforge_project: anise
|
|
93
|
+
rubygems_version: 1.2.0
|
|
94
|
+
signing_key:
|
|
95
|
+
specification_version: 2
|
|
96
|
+
summary: Annable is an annotations systems for the Ruby programming lanaguage.
|
|
97
|
+
test_files:
|
|
98
|
+
- test/test_toplevel_attribute.rb
|
|
99
|
+
- test/test_attribute.rb
|
|
100
|
+
- test/test_annotatable.rb
|
|
101
|
+
- test/suite.rb
|
|
102
|
+
- test/test_toplevel_annotatable.rb
|