action_annotation 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,2 @@
1
+ = 1.0.1
2
+ * first public release
@@ -0,0 +1,18 @@
1
+ Copyright (c) 2009 Nico Rehwaldt, Arian Treffer
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the "Software"), to deal in
5
+ the Software without restriction, including without limitation the rights to
6
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7
+ the Software, and to permit persons to whom the Software is furnished to do so,
8
+ subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,15 @@
1
+
2
+ = Action Annotation
3
+
4
+ This plug-in provides means to describe what controller actions are doing in a
5
+ human readable way and the possibility to access these descriptions in a
6
+ computer-friendly way during runtime.
7
+
8
+ See ActionAnnotation::Annotations::ClassMethods for examples.
9
+
10
+ == License
11
+
12
+ Copyright Nico Rehwaldt, Arian Treffer 2009
13
+
14
+ You may use, copy and redistribute this library under the same terms as
15
+ {Ruby itself}[http://www.ruby-lang.org/en/LICENSE.txt] or under the MIT-License.
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/clean'
4
+ require 'rake/gempackagetask'
5
+ require 'rake/rdoctask'
6
+ require 'spec/rake/spectask'
7
+
8
+ spec = Gem::Specification.new do |s|
9
+ s.name = 'action_annotation'
10
+ s.version = '1.0.1'
11
+ s.has_rdoc = true
12
+ s.extra_rdoc_files = ['README', 'MIT-LICENSE', 'CHANGELOG']
13
+ s.summary = 'Add descriptions to methods'
14
+ s.description =
15
+ 'This gem introduces means for describing the content of methods in a ' +
16
+ 'human readable way. The descriptions are parsed and provided as hashes ' +
17
+ 'at runtime. This feature is intended to be used for controller ' +
18
+ 'actions to automatize parts of your rails application, but it ' +
19
+ 'can be included in other classes as well.'
20
+ s.author = 'Nico Rehwaldt, Arian Treffer'
21
+ s.email = 'ruby@nixis.de'
22
+ s.homepage = 'http://tech.lefedt.de/2010/3/annotation-based-security-for-rails'
23
+ s.files = %w(CHANGELOG MIT-LICENSE README Rakefile) + Dir.glob("{bin,lib,spec}/**/*")
24
+ s.require_path = "lib"
25
+ s.bindir = "bin"
26
+ end
27
+
28
+ Rake::GemPackageTask.new(spec) do |p|
29
+ p.gem_spec = spec
30
+ p.need_tar = true
31
+ p.need_zip = true
32
+ end
33
+
34
+ Rake::RDocTask.new do |rdoc|
35
+ files = ['README', 'MIT-LICENSE', 'CHANGELOG', 'lib/**/*.rb']
36
+ rdoc.rdoc_files.add(files)
37
+ rdoc.main = "README" # page to start on
38
+ rdoc.title = "Action Annotation Docs"
39
+ rdoc.rdoc_dir = 'doc' # rdoc output folder
40
+ rdoc.options << '--line-numbers'
41
+ end
42
+
43
+ Spec::Rake::SpecTask.new do |t|
44
+ t.spec_files = FileList['spec/**/*_spec.rb']
45
+ end
@@ -0,0 +1,10 @@
1
+ # Contains the require definitions for this gem.
2
+ #
3
+
4
+ module ActionAnnotation # :nodoc:
5
+ end
6
+
7
+ dir = File.dirname(__FILE__)
8
+ require dir + "/action_annotation/utils"
9
+ require dir + "/action_annotation/annotations"
10
+ require dir + "/extensions/action_controller"
@@ -0,0 +1,199 @@
1
+ # Contains the ActionAnnotation::Annotations module.
2
+ #
3
+
4
+ #
5
+ module ActionAnnotation # :nodoc:
6
+
7
+ # Include this module in your class to enable descriptions.
8
+ # Is already included in ActionController::Base.
9
+ #
10
+ # See ActionAnnotation::Annotations::ClassMethods for more information.
11
+ #
12
+ module Annotations
13
+
14
+ def self.included(base) # :nodoc:
15
+ base.extend(ClassMethods)
16
+ base.send :include, InstanceMethods
17
+ end
18
+
19
+ # This module contains methods for defining descriptions.
20
+ #
21
+ # == Syntax of descriptions
22
+ #
23
+ # A description always contains an action, which is executed, and optionally
24
+ # a resource type and a source for the resource, on which the action is
25
+ # performed.
26
+ #
27
+ # A description is specified as a string of the form
28
+ # ACTION (* RESOURCE)? ((in|from|by) SOURCE)?
29
+ # and will be transformed into a corrsponding hash.
30
+ #
31
+ # At any point, comments can be inserted using brackets.
32
+ #
33
+ # === Examples
34
+ # * '*show*' -- { :action => :show }
35
+ # * '*show* *comment*' -- { :action => :show, :resource => :comment }
36
+ # * '*shows* all *comments*' -- { :action => :show, :resource => :comment }
37
+ # * '(if necessary) *show* all new *comments* (of this user)' -- { :action => :show, :resource => :comment }
38
+ # * '*show* a *comment* by :id' -- { :action => :show, :resource => :comment, :source => :id }
39
+ # * '*show* all *comments* in <b>@comments</b>' -- { :action => :show, :resource => :comment, :source => '@comments' }
40
+ # Notice that verbs a transformed into infinitive and resources are singularized.
41
+ # Unless the source starts with a colon, it will be provided as string
42
+ #
43
+ # == Defining descriptions
44
+ #
45
+ # To add an description to a method, use #describe or #desc.
46
+ #
47
+ # Also notice that adding new descriptions at runtime is not possible,
48
+ # once #descriptions_of was called.
49
+ #
50
+ module ClassMethods
51
+
52
+ # This module uses the +method_added+ callback. If this method is
53
+ # overwritten and not called using +super+, +desc+ will not work.
54
+ #
55
+ def method_added(method)
56
+ check_pending_descriptions(method)
57
+ super(method)
58
+ end
59
+
60
+ # Adds descriptions to a method, but raises an argument error if the
61
+ # method was already described.
62
+ # * +method+ symbol or string
63
+ # * +descriptions+ list of description strings
64
+ #
65
+ def describe!(method, *descriptions)
66
+ raise_if_already_described! method
67
+ describe method, *descriptions
68
+ end
69
+
70
+ # Adds descriptions to a method.
71
+ # * +method+ symbol or string
72
+ # * +descriptions+ list of description strings
73
+ #
74
+ # ==== Example
75
+ # describe :show, "shows a comment"
76
+ # def show
77
+ # @comment = Comment.find(params[:id])
78
+ # end
79
+ #
80
+ def describe(method, *descriptions)
81
+ plain_descriptions_of(method).push(*descriptions)
82
+ end
83
+
84
+ # Adds descriptions to the method that will be defined next.
85
+ # * +descriptions+ list of description strings
86
+ #
87
+ # ==== Example
88
+ # desc "shows a comment"
89
+ # def show
90
+ # @comment = Comment.find(params[:id])
91
+ # end
92
+ #
93
+ def desc(*descriptions)
94
+ unassigned_descriptions.push(*descriptions)
95
+ end
96
+
97
+ # Returns the description strings that were added to a method, returns an
98
+ # empty array if no descriptions were provided.
99
+ # * +method+ symbol or string
100
+ #
101
+ def plain_descriptions_of(method)
102
+ plain_descriptions[method.to_sym]
103
+ end
104
+
105
+ def plain_descriptions # :nodoc:
106
+ @plain_descriptions ||= Hash.new do |h,k|
107
+ h[k] = fetch_inherited(k)
108
+ end
109
+ end
110
+
111
+ # Returns the descriptions that were added to a method, returns an empty
112
+ # array if no descriptions were provided.
113
+ # * +method+ symbol or string
114
+ #
115
+ # ==== Example
116
+ # desc "shows a comment"
117
+ # def show
118
+ # @comment = Comment.find(params[:id])
119
+ # end
120
+ #
121
+ # descriptions_of(:show) # == { :action => :show, :resource => :comment }
122
+ #
123
+ def descriptions_of(method)
124
+ parsed_descriptions[method.to_sym]
125
+ end
126
+
127
+ def parsed_descriptions # :nodoc:
128
+ @parsed_descriptions ||= Hash.new do |h,k|
129
+ h[k] = parse_descriptions(k)
130
+ end
131
+ end
132
+
133
+ private
134
+
135
+ def fetch_inherited(method) # :nodoc:
136
+ if superclass.respond_to? :plain_descriptions_of
137
+ return superclass.plain_descriptions_of(method).dup
138
+ end
139
+ []
140
+ end
141
+
142
+ def parse_descriptions(method) # :nodoc:
143
+ plain_descriptions_of(method).collect \
144
+ { |desc| ActionAnnotation::Utils.parse_description(desc, true) }
145
+ end
146
+
147
+ def check_pending_descriptions(method) # :nodoc:
148
+ unless @unassigned_descriptions.nil?
149
+ describe method, *@unassigned_descriptions
150
+ @unassigned_descriptions = nil
151
+ end
152
+ end
153
+
154
+ def unassigned_descriptions # :nodoc:
155
+ @unassigned_descriptions ||= []
156
+ end
157
+
158
+ def raise_if_already_described!(method) # :nodoc:
159
+ unless plain_descriptions_of(method).empty?
160
+ raise ArgumentError, "Description for '#{method}' already set"
161
+ end
162
+ end
163
+
164
+ end
165
+
166
+ # Contains methods for fetching the value as defined by the source-part
167
+ # of a description.
168
+ #
169
+ module InstanceMethods
170
+
171
+ # Returns a list of values that is associated with a source
172
+ # * +source+ Either a symbol or an evaluatable string
173
+ #
174
+ def values_of_source(source)
175
+ value = value_of_source(source)
176
+ # TODO: what happens in case of a hash?
177
+ value.is_a?(Array) ? value : [value]
178
+ end
179
+
180
+ # Returns the value that is associated with a source
181
+ # * +source+ Either a symbol or an evaluatable string
182
+ #
183
+ # value_of_binding(:id) # == params[:id]
184
+ # value_of_binding("@value") # == @value
185
+ #
186
+ def value_of_source(source)
187
+ if source.is_a? String
188
+ instance_eval(source)
189
+ elsif source.is_a? Symbol
190
+ params[source]
191
+ else
192
+ raise ArgumentError, "Unknown source #{source}"
193
+ end
194
+ end
195
+
196
+ end
197
+
198
+ end
199
+ end
@@ -0,0 +1,50 @@
1
+ #
2
+
3
+ # Provides some methods that are needed at several locations in the plug-in.
4
+ #
5
+ class ActionAnnotation::Utils
6
+
7
+ # Regexp for parsing description strings
8
+ PARSE_REGEXP =
9
+ # ACTION any RESOURCE (in|from|by) SOURCE
10
+ /^([_\w]+)(.*\s([_\w]+))?(\s+(in|from|by)\s+(\S+))?$/ # :nodoc:
11
+
12
+ # Parses a description string
13
+ # * +description+ description of a controller action
14
+ # * +allow_source+ if false, an exception is raised if the description
15
+ # contains a variable
16
+ # Returns action, resource and source.
17
+ # See ActionAnnotation::Annotations::ClassMethods for details.
18
+ #
19
+ def self.parse_description(description,allow_source=true)
20
+ # description = "shows all courses in @courses (ignore this comment)"
21
+ action, resource, source = get_tokens(description)
22
+ # 'shows', 'courses', '@courses'
23
+ if source
24
+ raise ArgumentError, "Found unexpected source in '#{description}'" unless allow_source
25
+ source = (source.last(-1)).to_sym if source.starts_with? ':'
26
+ end
27
+ returning Hash.new do |result|
28
+ result[:action] = infinitive(action).to_sym
29
+ result[:resource] = resource.singularize.to_sym if resource
30
+ result[:source] = source if source
31
+ # { :action => :show, :resource => :course, :source => '@courses' }
32
+ end
33
+ end
34
+
35
+ def self.get_tokens(description) # :nodoc:
36
+ description = description.gsub(/\(.*\)/,'').strip
37
+ #description = "shows all courses in @courses"
38
+ matches = PARSE_REGEXP.match(description)
39
+ raise ArgumentError, "'#{description}' could not be matches" unless matches
40
+ [matches[1], matches[3], matches[6]]
41
+ end
42
+
43
+ @infinitive_hash = {"is" => "be", "has" => "have"} # :nodoc:
44
+
45
+ def self.infinitive(verb) # :nodoc:
46
+ @infinitive_hash[verb] ||
47
+ (verb.ends_with?("s") ? verb.first(-1) : verb)
48
+ end
49
+
50
+ end
@@ -0,0 +1,13 @@
1
+ # Extends ActionController::Base to enable action descriptions.
2
+ #
3
+
4
+ #
5
+ module ActionController # :nodoc:
6
+
7
+ class Base # :nodoc:
8
+
9
+ include ::ActionAnnotation::Annotations
10
+
11
+ end
12
+
13
+ end
@@ -0,0 +1,36 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe CommentController do
4
+
5
+ data = [
6
+ [:index, [{:action => :list, :resource => :comment}]],
7
+ [:show, [{:action => :show, :resource => :comment, :source => :id}]],
8
+ [:edit, [{:action => :edit, :resource => :comment},
9
+ {:action => :show, :resource => :comment}]],
10
+ [:update, [{:action => :edit, :resource => :comment},
11
+ {:action => :show, :resource => :comment, :source => '@comment'}]],
12
+ [:destroy, [{:action => :delete},
13
+ {:action => :delete, :source => '@comments'}]],
14
+ ]
15
+
16
+ data.each do |test|
17
+ it "should have valid description for #{test.first}" do
18
+ desc = CommentController.descriptions_of(test.first)
19
+ desc.should == test.second
20
+ end
21
+ end
22
+
23
+ it "should raise an error on :describe!" do
24
+ lambda {
25
+ CommentController.describe! :update, "foo"
26
+ }.should raise_error(ArgumentError)
27
+ end
28
+
29
+ it "should raise an error on an invalid description" do
30
+ lambda {
31
+ CommentController.describe :foo, "show comment using @foo"
32
+ CommentController.descriptions_of(:foo)
33
+ }.should raise_error(ArgumentError)
34
+ end
35
+
36
+ end
@@ -0,0 +1,34 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe ActionAnnotation::Utils do
4
+
5
+ it 'should parse descriptions without bindings correctly' do
6
+ # Test result
7
+ result = {:action => :show, :resource => :resource}
8
+ # Test data
9
+ ['show a resource', 'show with some text ignored a resource',
10
+ 'show pluralized resources', '(ignoring comments) show a resource',
11
+ 'show a resource (with comment at the end)'].each do |s|
12
+ # Test #parse_descriptions
13
+ ActionAnnotation::Utils.parse_description(s).should == result
14
+ end
15
+ end
16
+
17
+ it 'should detect bindings of a description' do
18
+ { # Test data => test result
19
+ 'show the resource in @res' =>
20
+ {:action => :show,:resource => :resource,:source => '@res'},
21
+ 'show the resource from :id' =>
22
+ {:action => :show,:resource => :resource,:source => :id},
23
+ }.each_pair do |key, value|
24
+ ActionAnnotation::Utils.parse_description(key,true).should == value
25
+ end
26
+ end
27
+
28
+ it 'should raise an error if an unexpected binding is detected in a description' do
29
+ lambda {
30
+ ActionAnnotation::Utils.parse_description('show the resource :id')
31
+ }.should raise_error(ArgumentError)
32
+ end
33
+
34
+ end
@@ -0,0 +1,25 @@
1
+ class CommentController < ActionController::Base
2
+
3
+ desc "list all comments"
4
+ def index
5
+ end
6
+
7
+ describe :show, "shows a comment by :id"
8
+ def show
9
+ end
10
+
11
+ desc "(invokes the action that) edits the comment",
12
+ "shows the same comment (as well)"
13
+ def edit
14
+ end
15
+
16
+ describe :update, "edits a comment",
17
+ "shows the same comment (if the value is) in @comment"
18
+ def update
19
+ end
20
+
21
+ desc "deletes", "deletes from @comments"
22
+ def destroy
23
+ end
24
+
25
+ end
@@ -0,0 +1,3 @@
1
+ require "active_support"
2
+ require File.dirname(__FILE__) + '/../lib/action_annotation'
3
+ require File.dirname(__FILE__) + '/helper/comment_controller'
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: action_annotation
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nico Rehwaldt, Arian Treffer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-03-13 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: This gem introduces means for describing the content of methods in a human readable way. The descriptions are parsed and provided as hashes at runtime. This feature is intended to be used for controller actions to automatize parts of your rails application, but it can be included in other classes as well.
17
+ email: ruby@nixis.de
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ - MIT-LICENSE
25
+ - CHANGELOG
26
+ files:
27
+ - CHANGELOG
28
+ - MIT-LICENSE
29
+ - README
30
+ - Rakefile
31
+ - lib/action_annotation/annotations.rb
32
+ - lib/action_annotation/utils.rb
33
+ - lib/action_annotation.rb
34
+ - lib/extensions/action_controller.rb
35
+ - spec/actionannotation/annotations_spec.rb
36
+ - spec/actionannotation/utils_spec.rb
37
+ - spec/helper/comment_controller.rb
38
+ - spec/spec_helper.rb
39
+ has_rdoc: true
40
+ homepage: http://tech.lefedt.de/2010/3/annotation-based-security-for-rails
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options: []
45
+
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.3.5
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: Add descriptions to methods
67
+ test_files: []
68
+