mayi 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 2.8.0"
10
+ gem "rdoc", "~> 3.12"
11
+ gem "bundler"
12
+ gem "jeweler", "~> 1.8.4"
13
+ end
@@ -0,0 +1,31 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.3)
5
+ git (1.2.5)
6
+ jeweler (1.8.4)
7
+ bundler (~> 1.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rdoc
11
+ json (1.7.5)
12
+ rake (0.9.2.2)
13
+ rdoc (3.12)
14
+ json (~> 1.4)
15
+ rspec (2.8.0)
16
+ rspec-core (~> 2.8.0)
17
+ rspec-expectations (~> 2.8.0)
18
+ rspec-mocks (~> 2.8.0)
19
+ rspec-core (2.8.0)
20
+ rspec-expectations (2.8.0)
21
+ diff-lcs (~> 1.1.2)
22
+ rspec-mocks (2.8.0)
23
+
24
+ PLATFORMS
25
+ ruby
26
+
27
+ DEPENDENCIES
28
+ bundler
29
+ jeweler (~> 1.8.4)
30
+ rdoc (~> 3.12)
31
+ rspec (~> 2.8.0)
@@ -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. <http://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.
@@ -0,0 +1,159 @@
1
+ # MayI
2
+
3
+ A plugable access rights API. Meant to make integrations easier. Werry useful as an integration point for blog,forum and CMS components. Also its much nicer to read than the basic stuff i usually do.
4
+
5
+ Before without MayI.
6
+
7
+ ```ruby
8
+ if user_object.is_admin?
9
+
10
+ end
11
+ ```
12
+
13
+ Now with MayI.
14
+
15
+ ```ruby
16
+ access.may_add_user! do
17
+
18
+ end
19
+ ```
20
+
21
+
22
+
23
+
24
+ ## Basics
25
+
26
+ You have a class that implements boolean questions.
27
+
28
+ ```ruby
29
+ class MyBasicAccess
30
+ def initialize(data)
31
+ @data = data
32
+ end
33
+
34
+ def may_view_secret_stuff(stuff)
35
+ stuff.owner_id == data[:session][:user_id]
36
+ end
37
+
38
+ def may_create_new_record
39
+ data[:session][:user_type] == "admin"
40
+ end
41
+ end
42
+ ```
43
+
44
+ This can then be used with the MayI::Access class.
45
+
46
+
47
+ ```ruby
48
+ access = MayI::Access.new(MyBasicAccess)
49
+
50
+ # Simple boolean
51
+ if access.may_create_new_record?
52
+ # You do stuff here
53
+ end
54
+
55
+ # With a block
56
+ access.may_create_new_record? do
57
+ # You do stuff here
58
+ end
59
+ ```
60
+
61
+ Now with exceptions. On failure the MayI::AccessDeniedError error is raised.
62
+
63
+ ```ruby
64
+ access = MayI::Access.new(MyBasicAccess)
65
+
66
+ # Simple boolean
67
+ if access.may_create_new_record!
68
+ # You do stuff here
69
+ end
70
+
71
+ # With a block
72
+ access.may_create_new_record! do
73
+ # You do stuff here
74
+ end
75
+ ```
76
+
77
+ With custom error message
78
+
79
+ ```ruby
80
+ access.error_message("Sorry but you are not allowed to do this!").may_create_new_record! do
81
+ # You do stuff here
82
+ end
83
+ ```
84
+
85
+
86
+
87
+
88
+
89
+ ## A Rails example
90
+
91
+ On each request we create a new instance of MyBasicAccess with the current session.
92
+
93
+ ```ruby
94
+ class ApplicationController < ActionController::Base
95
+ before_filter :init_access
96
+
97
+ def init_access
98
+ # Refresh with the relevant data for this request
99
+ ApplicationController.access.refresh({:session => session})
100
+ end
101
+
102
+ def self.access
103
+ @@access_cache
104
+ end
105
+
106
+ def access
107
+ @@access_cache
108
+ end
109
+
110
+ @@access_cache = MayI::Access.new
111
+ @@access_cache.implementation = MyBasicAccess
112
+ end
113
+ ```
114
+
115
+ We use the API to check if a user should be able to view some secret stuff.
116
+
117
+ ```ruby
118
+ class SecretStuffController < ApplicationController
119
+
120
+ def show
121
+ stuff = Stuff.find(params[:id])
122
+ access.may_view_secret_stuff?(stuff) do
123
+
124
+ end
125
+ end
126
+
127
+ end
128
+ ```
129
+
130
+ We can also use it in a model.
131
+
132
+ ```ruby
133
+ class ShortUrl < ActiveRecord::Base
134
+
135
+ def method_that_requires_special_access
136
+ ApplicationController.access.may_create_new_record! do
137
+
138
+ end
139
+ end
140
+
141
+ end
142
+ ```
143
+
144
+
145
+ ## Contributing to MayI
146
+
147
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
148
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
149
+ * Fork the project.
150
+ * Start a feature/bugfix branch.
151
+ * Commit and push until you are happy with your contribution.
152
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
153
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
154
+
155
+ ## Copyright
156
+
157
+ Copyright (c) 2012 Darwin. See LICENSE.txt for
158
+ further details.
159
+
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "mayi"
18
+ gem.homepage = "http://github.com/bjornblomqvist/mayi"
19
+ gem.license = "LGPL3"
20
+ gem.summary = %Q{ A plugable access rights api. Ment to make integrations easier. }
21
+ gem.description = %Q{ A plugable access rights api. Ment to make integrations easier. }
22
+ gem.email = "darwin@bits2life.com"
23
+ gem.authors = ["Darwin"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rdoc/task'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "mayi #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,9 @@
1
+
2
+ # Add the directory containing this file to the start of the load path if it
3
+ # isn't there already.
4
+ $:.unshift(File.dirname(__FILE__)) unless
5
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
6
+
7
+ require 'mayi/proxy'
8
+ require 'mayi/access_denied_error'
9
+ require 'mayi/access'
@@ -0,0 +1,71 @@
1
+ module MayI
2
+ class Access
3
+
4
+ attr_accessor :implementation
5
+
6
+ def initialize(implementation = nil)
7
+ self.implementation = implementation
8
+ end
9
+
10
+ def refresh(data = {})
11
+ raise "You have not set any implementation yet" unless self.implementation
12
+ Thread.current["mayi_access_implementation_#{self.object_id}"] = self.implementation.new(data)
13
+ end
14
+
15
+ def clear
16
+ Thread.current["mayi_access_implementation_#{self.object_id}"] = nil
17
+ end
18
+
19
+ def current_instance
20
+ Thread.current["mayi_access_implementation_#{self.object_id}"] || refresh
21
+ end
22
+
23
+ def method_missing(meth, *args, &block)
24
+ if meth.to_s =~ /^(may)_.*(\?|!)$/
25
+ if meth.to_s.end_with?("!")
26
+ if current_instance.send(meth.to_s[0..-2].to_sym,*args)
27
+ yield if block_given?
28
+ true
29
+ else
30
+ raise_error(meth)
31
+ end
32
+ elsif meth.to_s.end_with?("?")
33
+ if current_instance.send(meth.to_s[0..-2].to_sym,*args)
34
+ yield if block_given?
35
+ true
36
+ else
37
+ false
38
+ end
39
+ end
40
+ else
41
+ super(meth,*args,&block)
42
+ end
43
+ end
44
+
45
+ def respond_to?(meth)
46
+ if meth.to_s =~ /^(may)_.*(\?|!)$/
47
+ super(meth.to_s[0..-2].to_sym)
48
+ else
49
+ super(meth)
50
+ end
51
+ end
52
+
53
+ def error_message(message)
54
+ Proxy.new(self) do |state|
55
+ if state == :before
56
+ @error_message = message
57
+ else
58
+ @error_message = nil
59
+ end
60
+ end
61
+ end
62
+
63
+ private
64
+
65
+ def raise_error(meth)
66
+ error_message = meth.to_s[0..-2].gsub("_", " ").gsub("may","may not")
67
+ raise AccessDeniedError.new(@error_message || error_message)
68
+ end
69
+
70
+ end
71
+ end
@@ -0,0 +1,9 @@
1
+ module MayI
2
+ class AccessDeniedError < RuntimeError
3
+
4
+ def initialize(message = 'Access Denied')
5
+ super message
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,19 @@
1
+ module MayI
2
+ class Proxy
3
+ instance_methods.each { |m| undef_method m unless m =~ /(^__|^send$|^object_id$)/ }
4
+
5
+ def initialize(target,&block)
6
+ @target = target
7
+ @block = block
8
+ end
9
+
10
+ protected
11
+
12
+ def method_missing(name, *args, &block)
13
+ @block.call(:before)
14
+ @target.send(name, *args, &block)
15
+ ensure
16
+ @block.call(:after)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "MayI" do
4
+ it "fails" do
5
+ fail "hey buddy, you should probably rename this file and start specing for real"
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'may-i'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mayi
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Darwin
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.8.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 2.8.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: rdoc
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '3.12'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '3.12'
46
+ - !ruby/object:Gem::Dependency
47
+ name: bundler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: jeweler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 1.8.4
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 1.8.4
78
+ description: ! ' A plugable access rights api. Ment to make integrations easier. '
79
+ email: darwin@bits2life.com
80
+ executables: []
81
+ extensions: []
82
+ extra_rdoc_files:
83
+ - LICENSE.txt
84
+ - README.markdown
85
+ files:
86
+ - .document
87
+ - .rspec
88
+ - Gemfile
89
+ - Gemfile.lock
90
+ - LICENSE.txt
91
+ - README.markdown
92
+ - Rakefile
93
+ - VERSION
94
+ - lib/mayi.rb
95
+ - lib/mayi/access.rb
96
+ - lib/mayi/access_denied_error.rb
97
+ - lib/mayi/proxy.rb
98
+ - spec/mayi_spec.rb
99
+ - spec/spec_helper.rb
100
+ homepage: http://github.com/bjornblomqvist/mayi
101
+ licenses:
102
+ - LGPL3
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ segments:
114
+ - 0
115
+ hash: 2167523034853202393
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ none: false
118
+ requirements:
119
+ - - ! '>='
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 1.8.24
125
+ signing_key:
126
+ specification_version: 3
127
+ summary: A plugable access rights api. Ment to make integrations easier.
128
+ test_files: []