allowance 0.0.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/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in allowance.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Hendrik Mans
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Allowance
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'allowance'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install allowance
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ # integrate rspec
5
+ require 'rspec/core/rake_task'
6
+ RSpec::Core::RakeTask.new('spec')
7
+ task :default => :spec
data/allowance.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/allowance/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Hendrik Mans"]
6
+ gem.email = ["hendrik@mans.de"]
7
+ gem.description = %q{A generic, but decidedly awesome authorization control layer.}
8
+ gem.summary = %q{A generic, but decidedly awesome authorization control layer.}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "allowance"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Allowance::VERSION
17
+
18
+ gem.add_development_dependency 'rspec'
19
+ end
@@ -0,0 +1,77 @@
1
+ module Allowance
2
+ class Permissions
3
+ def initialize(context, &blk)
4
+ @permissions = {}
5
+ @context = context
6
+ instance_exec(context, &blk)
7
+ end
8
+
9
+ def can?(*args)
10
+ if ![Symbol, Class].include?(args.last.class)
11
+ thing = args.pop
12
+ args.push(thing.class)
13
+ end
14
+
15
+ if (p = find_permission(*args)).present?
16
+ thing ? scoped_model(*args).find(:first, conditions: { id: thing.id }).present? : true
17
+ else
18
+ false
19
+ end
20
+ end
21
+
22
+ def can(*args)
23
+ options = args.pop if args.last.is_a?(Hash) || args.last.is_a?(Proc)
24
+ object = args.pop unless args.last.is_a?(Symbol)
25
+
26
+ expand_permissions(args).each do |verb|
27
+ permissions[permission_identifier(verb, object)] ||= options || true
28
+ end
29
+ end
30
+
31
+ def scoped_model(*args)
32
+ model = args.last # TODO: check that model is a class
33
+
34
+ if p = find_permission(*args)
35
+ if p.is_a?(Hash)
36
+ model.where(p)
37
+ elsif p.is_a?(Proc)
38
+ (p.arity == 0 ? model.instance_exec(&p) : model.call(r))
39
+ else
40
+ model
41
+ end
42
+ else
43
+ model.where(false)
44
+ end
45
+ end
46
+
47
+ def find_permission(*args)
48
+ object = args.pop unless args.last.is_a?(Symbol)
49
+
50
+ args.flatten.each do |verb|
51
+ if p = permissions[permission_identifier(verb, object)]
52
+ return p
53
+ end
54
+ end
55
+ nil
56
+ end
57
+
58
+ private
59
+
60
+ def expand_permissions(*permissions)
61
+ permissions.flatten.map do |p|
62
+ case p
63
+ when :manage then [:manage, :index, :show, :new, :create, :edit, :update, :destroy]
64
+ when :view then [:view, :index, :show]
65
+ else p
66
+ end
67
+ end.flatten
68
+ end
69
+
70
+ def permission_identifier(verb, object)
71
+ raise "Can't use enumerables as verbs" if verb.is_a?(Enumerable)
72
+ [verb.to_sym, object].compact
73
+ end
74
+
75
+ attr_reader :permissions
76
+ end
77
+ end
@@ -0,0 +1,3 @@
1
+ module Allowance
2
+ VERSION = "0.0.1"
3
+ end
data/lib/allowance.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'allowance/version'
2
+ require 'allowance/permissions'
3
+
4
+ module Allowance
5
+ end
@@ -0,0 +1,4 @@
1
+ require 'spec_helper'
2
+
3
+ describe Allowance::Permissions do
4
+ end
@@ -0,0 +1,5 @@
1
+ SPEC_DIR = File.dirname(__FILE__)
2
+ lib_path = File.expand_path("#{SPEC_DIR}/../lib")
3
+ $LOAD_PATH.unshift lib_path unless $LOAD_PATH.include?(lib_path)
4
+
5
+ require 'allowance'
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: allowance
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Hendrik Mans
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70332184433920 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70332184433920
25
+ description: A generic, but decidedly awesome authorization control layer.
26
+ email:
27
+ - hendrik@mans.de
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ - allowance.gemspec
38
+ - lib/allowance.rb
39
+ - lib/allowance/permissions.rb
40
+ - lib/allowance/version.rb
41
+ - spec/permissions_spec.rb
42
+ - spec/spec_helper.rb
43
+ homepage: ''
44
+ licenses: []
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubyforge_project:
63
+ rubygems_version: 1.8.11
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: A generic, but decidedly awesome authorization control layer.
67
+ test_files:
68
+ - spec/permissions_spec.rb
69
+ - spec/spec_helper.rb
70
+ has_rdoc: