annotable 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/COPYING.LESSER ADDED
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify all your gem's dependencies in annotable.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,85 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ annotable (0.1.2)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ commander (4.6.0)
11
+ highline (~> 2.0.0)
12
+ diff-lcs (1.5.0)
13
+ docile (1.4.0)
14
+ highline (2.0.3)
15
+ parallel (1.22.1)
16
+ parlour (5.0.0)
17
+ commander (~> 4.5)
18
+ parser
19
+ rainbow (~> 3.0)
20
+ sorbet-runtime (>= 0.5)
21
+ parser (3.1.2.0)
22
+ ast (~> 2.4.1)
23
+ rainbow (3.1.1)
24
+ rake (13.0.6)
25
+ regexp_parser (2.4.0)
26
+ rexml (3.2.5)
27
+ rspec (3.11.0)
28
+ rspec-core (~> 3.11.0)
29
+ rspec-expectations (~> 3.11.0)
30
+ rspec-mocks (~> 3.11.0)
31
+ rspec-core (3.11.0)
32
+ rspec-support (~> 3.11.0)
33
+ rspec-expectations (3.11.0)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.11.0)
36
+ rspec-mocks (3.11.1)
37
+ diff-lcs (>= 1.2.0, < 2.0)
38
+ rspec-support (~> 3.11.0)
39
+ rspec-support (3.11.0)
40
+ rubocop (1.30.0)
41
+ parallel (~> 1.10)
42
+ parser (>= 3.1.0.0)
43
+ rainbow (>= 2.2.2, < 4.0)
44
+ regexp_parser (>= 1.8, < 3.0)
45
+ rexml (>= 3.2.5, < 4.0)
46
+ rubocop-ast (>= 1.18.0, < 2.0)
47
+ ruby-progressbar (~> 1.7)
48
+ unicode-display_width (>= 1.4.0, < 3.0)
49
+ rubocop-ast (1.18.0)
50
+ parser (>= 3.1.1.0)
51
+ rubocop-rspec (2.11.1)
52
+ rubocop (~> 1.19)
53
+ ruby-progressbar (1.11.0)
54
+ simplecov (0.21.2)
55
+ docile (~> 1.1)
56
+ simplecov-html (~> 0.11)
57
+ simplecov_json_formatter (~> 0.1)
58
+ simplecov-html (0.12.3)
59
+ simplecov_json_formatter (0.1.4)
60
+ sorbet-runtime (0.5.10184)
61
+ sord (4.0.0)
62
+ commander (~> 4.5)
63
+ parlour (~> 5.0)
64
+ sorbet-runtime
65
+ yard
66
+ unicode-display_width (2.1.0)
67
+ webrick (1.7.0)
68
+ yard (0.9.28)
69
+ webrick (~> 1.7.0)
70
+
71
+ PLATFORMS
72
+ x86_64-linux
73
+ x86_64-linux-musl
74
+
75
+ DEPENDENCIES
76
+ annotable!
77
+ rake (~> 13.0)
78
+ rspec (~> 3.11)
79
+ rubocop (~> 1.29)
80
+ rubocop-rspec (~> 2.11)
81
+ simplecov (~> 0.21)
82
+ sord (~> 4.0)
83
+
84
+ BUNDLED WITH
85
+ 2.3.7
data/README.md ADDED
@@ -0,0 +1,95 @@
1
+ # Annotable
2
+
3
+ Annotable is a module that can extend any class or module to add the ability to annotate its methods.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem "annotable"
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install annotable
20
+
21
+ ## Usage
22
+ ```ruby
23
+ require "annotable" # Require the gem
24
+
25
+ class Needy
26
+ extend Annotable # Extend your class or module with Annotable
27
+ annotable :my_annotation, :my_other_annotation # Declare your annotations
28
+
29
+ my_annotation 42, hello: "world!" # Annotate your methods
30
+ def method_needing_meta_data
31
+ # ...
32
+ end
33
+
34
+ def regular_method
35
+ # ...
36
+ end
37
+ end
38
+ ```
39
+
40
+ `Annotable` adds several methods to your class or module, providing access to annotations and their metadata:
41
+ ```ruby
42
+ Needy.annotated_method_exist? :method_needing_meta_data
43
+ # => true
44
+ Needy.annotated_method_exist? :regular_method
45
+ # => false
46
+ Needy.annotated_methods
47
+ # => [#<Annotable::Method
48
+ # @annotations=[
49
+ # #<Annotable::Annotation
50
+ # @name=:my_annotation,
51
+ # @options={:hello=>"world!"},
52
+ # @params=[42]>
53
+ # ],
54
+ # @name=:method_needing_meta_data>
55
+ # ]
56
+ ```
57
+
58
+ `Annotable::Method` represents a method name along with its annotations:
59
+ ```ruby
60
+ method = Needy.annotated_methods.first
61
+ method.name
62
+ # => :method_needing_meta_data
63
+ method.annotations
64
+ # => [
65
+ # #<Annotable::Annotation
66
+ # @name=:my_annotation,
67
+ # @options={:hello=>"world!"},
68
+ # @params=[42]>
69
+ # ]
70
+ ```
71
+
72
+ `Annotable::Annotation` contains annotation's name and metadata:
73
+ ```ruby
74
+ annotation = method.annotations.first
75
+ annotation.name
76
+ # => :my_annotation
77
+ annotation.params
78
+ # => [42]
79
+ annotation.options
80
+ # => {:hello => "world!"}
81
+ ```
82
+
83
+ ## Development
84
+
85
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
86
+
87
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
88
+
89
+ ## Contributing
90
+
91
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ductr-io/annotable.
92
+
93
+ ## License
94
+
95
+ The gem is available as open source under the terms of the [LGPLv3 or later](https://opensource.org/license/lgpl-3-0/).
data/Rakefile ADDED
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+ require "sord"
6
+
7
+ RSpec::Core::RakeTask.new(:spec)
8
+
9
+ require "rubocop/rake_task"
10
+
11
+ RuboCop::RakeTask.new
12
+
13
+ task default: %i[spec rubocop]
14
+
15
+ desc "Generates RBS file from the yard documentation"
16
+ task :sord do
17
+ options = {
18
+ rbs: true,
19
+ sord_comments: true,
20
+ regenerate: true,
21
+ break_params: 4,
22
+ replace_errors_with_untyped: false,
23
+ replace_unresolved_with_untyped: false,
24
+ exclude_messages: nil,
25
+ include_messages: nil,
26
+ keep_original_comments: false,
27
+ skip_constants: false,
28
+ use_original_initialize_return: false,
29
+ exclude_untyped: false
30
+ }
31
+
32
+ plugin = Sord::ParlourPlugin.new(options)
33
+ plugin.parlour = Parlour::RbsGenerator.new(break_params: options[:break_params])
34
+ plugin.generate(plugin.parlour.root)
35
+
36
+ File.write("sig/annotable.rbs", plugin.parlour.send(:rbs))
37
+ end
data/annotable.gemspec ADDED
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/annotable/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "annotable"
7
+ spec.version = Annotable::VERSION
8
+ spec.authors = ["Mathieu MOREL"]
9
+ spec.email = ["mathieu@lamanufacture.dev"]
10
+
11
+ spec.summary = "A simple zero-dependency method annotation gem"
12
+ spec.description = "Provides a simple way to add annotations to your method declarations."
13
+ spec.homepage = "https://github.com/ductr-io/annotable"
14
+ spec.license = "LGPL-3.0-or-later"
15
+ spec.required_ruby_version = ">= 3.1.0"
16
+
17
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
18
+ spec.metadata["rubygems_mfa_required"] = "true"
19
+
20
+ spec.metadata["homepage_uri"] = spec.homepage
21
+ spec.metadata["source_code_uri"] = spec.homepage
22
+ spec.metadata["changelog_uri"] = "#{spec.homepage}/releases"
23
+
24
+ # Specify which files should be added to the gem when it is released.
25
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
26
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
27
+ `git ls-files -z`.split("\x0").reject do |f|
28
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
29
+ end
30
+ end
31
+ spec.bindir = "exe"
32
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
33
+ spec.require_paths = ["lib"]
34
+
35
+ spec.add_development_dependency "rake", "~> 13.0"
36
+ spec.add_development_dependency "rspec", "~> 3.11"
37
+ spec.add_development_dependency "rubocop", "~> 1.29"
38
+ spec.add_development_dependency "rubocop-rspec", "~> 2.11"
39
+ spec.add_development_dependency "simplecov", "~> 0.21"
40
+ spec.add_development_dependency "sord", "~> 4.0"
41
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Annotable
4
+ #
5
+ # Encapsulates annotation data: name, params & options.
6
+ #
7
+ # my_annotation = Annotation.new(:name, ["some", "params"], {some: "options"})
8
+ # my_annotation.name # => :name
9
+ # my_annotation.params # => ["some", "params"]
10
+ # my_annotation.options # => {some: "options"}
11
+ #
12
+ class Annotation
13
+ # @return [Symbol] The annotation's name
14
+ attr_reader :name
15
+ # @return [Array<Object>] The annotation's params
16
+ attr_reader :params
17
+ # @return [Hash<Symbol, Object>] The annotation's options
18
+ attr_reader :options
19
+
20
+ #
21
+ # Creates an new annotation.
22
+ #
23
+ # @param [Symbol] name The annotation's name
24
+ # @param [Array<Object>] params The annotation's params
25
+ # @param [Hash<Symbol, Object>] options The annotation's options
26
+ #
27
+ def initialize(name, params = [], options = {})
28
+ @name = name
29
+ @params = params
30
+ @options = options
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Annotable
4
+ #
5
+ # An annotated method with its annotations.
6
+ #
7
+ # one = Annotation.new(:one)
8
+ # two = Annotation.new(:two)
9
+ # some_method = Method.new(:some_method, one, two)
10
+ #
11
+ # some_method.annotation_exist?(:one) # => true
12
+ # some_method.annotation_exist?(:no) # => false
13
+ # some_method.select_annotations(:one) # => [#<Annotable::Annotation @name=:one, @options={}, @params=[]>]
14
+ #
15
+ class Method
16
+ # @return [Symbol] The method name
17
+ attr_reader :name
18
+ # @return [Array<Annotation>] The annotations declared for this method
19
+ attr_reader :annotations
20
+
21
+ #
22
+ # Creates a new annotated method.
23
+ #
24
+ # @param [Symbol] name The method name
25
+ # @param [Array<Annotation>] annotations The annotations linked to this method
26
+ #
27
+ def initialize(name, *annotations)
28
+ raise ArgumentError, "You must provide at least one annotation" if annotations.empty?
29
+
30
+ @name = name
31
+ @annotations = annotations
32
+ end
33
+
34
+ #
35
+ # Determines whether annotation exists based on its name.
36
+ #
37
+ # @param [Symbol] name The annotation's name to check for
38
+ #
39
+ # @return [Boolean] True if the annotation exists, false otherwise
40
+ #
41
+ def annotation_exist?(name)
42
+ !annotations.find { |a| a.name == name }.nil?
43
+ end
44
+
45
+ #
46
+ # Returns all annotations matching the given names.
47
+ #
48
+ # @param [Array<Symbol>] names Names of the annotations to select
49
+ #
50
+ # @return [Array<Annotation>] The matching annotations
51
+ #
52
+ def select_annotations(*names)
53
+ raise ArgumentError, "You must provide at least one name to select" if names.empty?
54
+
55
+ annotations.select do |a|
56
+ names.include? a.name
57
+ end
58
+ end
59
+
60
+ #
61
+ # Finds the first annotation matching one of the given names.
62
+ #
63
+ # @param [Array<Symbol>] *names The annotation names to find
64
+ #
65
+ # @return [Annotation] The matching annotation
66
+ #
67
+ def find_annotation(*names)
68
+ raise ArgumentError, "You must provide at least one name to find" if names.empty?
69
+
70
+ annotations.find do |a|
71
+ names.include? a.name
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Annotable
4
+ # @return [String] The gem version number
5
+ VERSION = "0.1.2"
6
+ end