simple_rules 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3fd163623f2b64fb85fb0863693fa5ae9fc1aae9
4
+ data.tar.gz: 7a4f37a5633842770a15553e9b546b5c5b411295
5
+ SHA512:
6
+ metadata.gz: f936c8321594b7499db3ddaa87f2fbb618f9863cae4d0c51d65a55a8af0bf8a93d9f2da612ec67733ac981766ff7f912c59fc73d5e7679f18c9c844de7206f75
7
+ data.tar.gz: 392bdbe1242a2bf16380ae35909835c682bd9b0463bee5c97b5cf0a91d2efdc191cf514bd8996fcd6349c1c2cd71849c0f2ae570b5f9f87128f91b3e46a07da0
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ .idea
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --require spec_helper
3
+
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in simple_rules.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'rspec'
8
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 CoolElvis
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # SimpleRules
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'simple_rules'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install simple_rules
18
+
19
+ ## Usage
20
+
21
+ Define rule:
22
+
23
+ ```ruby
24
+ SimpleRules.can :some_action, SomeObject, error_message: 'Message' do |object, subject|
25
+ subject.some_attr == object.some_attr
26
+ end
27
+ ```
28
+
29
+ Checking action:
30
+ ```ruby
31
+ SimpleRules.can? :some_action, some_object, subject
32
+ ```
33
+
34
+ Configuration:
35
+ ```ruby
36
+ SimpleRules.configure do |config|
37
+ config.raise_not_authorized = true
38
+ end
39
+ ```
40
+ If raise_not_authorized is true then SimpleRules.can will raise SimpleRules:NotAuthorized exception with an error_message.
41
+
42
+
43
+ ## Contributing
44
+
45
+ Bug reports and pull requests are welcome on GitHub at https://github.com/CoolElvis/simple_rules. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
46
+
47
+
48
+ ## License
49
+
50
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
51
+
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new('spec')
5
+
6
+ # If you want to make this the default task
7
+ task :default => :spec
@@ -0,0 +1,22 @@
1
+ module SimpleRules
2
+ module Configuration
3
+
4
+ # Available options.
5
+ OPTION_NAMES = [
6
+ :raise_not_authorized
7
+ ]
8
+
9
+ attr_accessor *OPTION_NAMES
10
+
11
+ # A global configuration set via the block.
12
+ # @example
13
+ # SimpleRules.configure do |config|
14
+ # config.raise_not_authorized = true
15
+ # end
16
+ def configure
17
+ yield self if block_given?
18
+ self
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,21 @@
1
+ module SimpleRules
2
+ class Rule
3
+ attr_accessor :block, :action, :object_name, :error_message
4
+
5
+ def initialize (action, object_name, error_message, &block)
6
+ @action = action
7
+ @object_name = object_name
8
+ @block = block
9
+ @error_message = error_message
10
+
11
+ raise ArgumentError, 'Block is not given' unless block
12
+ end
13
+ end
14
+
15
+ class SimpleRulesError < StandardError; end
16
+
17
+ class RuleNotFindError < SimpleRulesError; end
18
+
19
+ class NotAuthorized < SimpleRulesError; end
20
+
21
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleRules
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,55 @@
1
+ require 'simple_rules/version'
2
+ require 'simple_rules/rule'
3
+ require 'simple_rules/configuration'
4
+
5
+ module SimpleRules
6
+ extend Configuration
7
+
8
+ class << self
9
+ @@simple_rules = Array.new
10
+
11
+ # @param action
12
+ # @param object [Class, Instance]
13
+ # @param subject [User, AnySubject]
14
+ # @raise NotAuthorized
15
+ # @example SimpleRules.can? :some_action, some_object, subject
16
+ def can?(action, object, subject)
17
+ if object.class == Class
18
+ klass = object.to_s.split(':').last
19
+ else
20
+ klass = object.class.to_s.split(':').last
21
+ end
22
+
23
+ rule = self.get_rule(action, klass)
24
+
25
+ if (rule.block.call object, subject)
26
+ true
27
+ else
28
+ if raise_not_authorized
29
+ raise NotAuthorized, rule.error_message
30
+ else
31
+ false
32
+ end
33
+ end
34
+ end
35
+
36
+ # @param action
37
+ # @param object [Class, Instance]
38
+ # @param error_message [String]
39
+ # @example
40
+ # SimpleRules.can :some_action, SomeObject do |object, subject|
41
+ # subject.some_attr == object.some_attr
42
+ # end
43
+ def can(action, object, error_message: nil, &block)
44
+ @@simple_rules << Rule.new(action, object.to_s, error_message: error_message, &block)
45
+ end
46
+
47
+ protected
48
+
49
+ def get_rule(action, object_name)
50
+ @@simple_rules.detect { |rule| (rule.action == action || rule.action == :manage) && rule.object_name == object_name } || raise(RuleNotFindError, 'Rule not found')
51
+ end
52
+
53
+ end
54
+
55
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'simple_rules/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "simple_rules"
8
+ spec.version = SimpleRules::VERSION
9
+ spec.authors = ["CoolElvis"]
10
+ spec.email = ["elvisplus2@gmail.com"]
11
+
12
+ spec.summary = %q{Simple authorization solution }
13
+ spec.description = %q{Simple authorization solution }
14
+ spec.homepage = 'https://github.com/CoolElvis/simple_rules'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_rules
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - CoolElvis
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-12-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: 'Simple authorization solution '
42
+ email:
43
+ - elvisplus2@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".rspec"
50
+ - CODE_OF_CONDUCT.md
51
+ - Gemfile
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - lib/simple_rules.rb
56
+ - lib/simple_rules/configuration.rb
57
+ - lib/simple_rules/rule.rb
58
+ - lib/simple_rules/version.rb
59
+ - simple_rules.gemspec
60
+ homepage: https://github.com/CoolElvis/simple_rules
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.4.5.1
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: Simple authorization solution
84
+ test_files: []
85
+ has_rdoc: