ostiary 0.9.0 → 0.10.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a4ac43974292700caba182663ecbd1f8993b9df1f5ed1e29205f2f35fb31f4bc
4
- data.tar.gz: b05dba4a650386ae4ab6c83d15e0bfb613a8bffe7994406b3cee6c70f427bf06
3
+ metadata.gz: 0b7ce83b09324ad3a1c1007d3dd1267c623f763867baccb06cc6e03f951090e1
4
+ data.tar.gz: 875b2111b2b9ff647371bbaaf4c969b975b3e1a5839a64e4d3785519c6a991aa
5
5
  SHA512:
6
- metadata.gz: 27cea053af1915dd68430018d7f9bc66b000e39f14bb4e9b40920b1304dd33a149c748b9123e03382e8c335623a06d0ccf36b3043dee73ca8f44e3b65f9d534a
7
- data.tar.gz: 49d80a51225c39c24184e63b30b66815a3785825115d4bfd10e47c320482a49f1a380cf3523877d3016323b242eea3737e77f125f338919c1dc18bfaabfe0e9c
6
+ metadata.gz: 6d5eec55afa4931f62b9d6b6c850967e49c9d0d5047c923167e7c3eafb3b4c0e1dbaaeb7cf66fbe7dd6b743254b9c057494e895ce85df6d47a36ac7646c792be
7
+ data.tar.gz: 97c50c8c0828bf690470af0bcbf7446252fd94a8905c85eb2e12db7824421192f28d18c9afa40336b21b40c6c0424e288c1abf92a0c64ce369291868cd80df4e
data/.gitignore ADDED
@@ -0,0 +1,53 @@
1
+ *.gem
2
+ *.rbc
3
+ .rspec_status
4
+ /.config
5
+ /coverage/
6
+ /InstalledFiles
7
+ /pkg/
8
+ /spec/reports/
9
+ /spec/examples.txt
10
+ /test/tmp/
11
+ /test/version_tmp/
12
+ /tmp/
13
+
14
+ # Used by dotenv library to load environment variables.
15
+ # .env
16
+
17
+ ## Specific to RubyMotion:
18
+ .dat*
19
+ .repl_history
20
+ build/
21
+ *.bridgesupport
22
+ build-iPhoneOS/
23
+ build-iPhoneSimulator/
24
+
25
+ ## Specific to RubyMotion (use of CocoaPods):
26
+ #
27
+ # We recommend against adding the Pods directory to your .gitignore. However
28
+ # you should judge for yourself, the pros and cons are mentioned at:
29
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
30
+ #
31
+ # vendor/Pods/
32
+
33
+ ## Documentation cache and generated files:
34
+ /.yardoc/
35
+ /_yardoc/
36
+ /doc/
37
+ /rdoc/
38
+
39
+ ## Environment normalization:
40
+ /.bundle/
41
+ /vendor/bundle
42
+ /lib/bundler/man/
43
+
44
+ # for a library or gem, you might want to ignore these files since the code is
45
+ # intended to run in multiple environments; otherwise, check them in:
46
+ Gemfile.lock
47
+ # .ruby-version
48
+ # .ruby-gemset
49
+
50
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
51
+ .rvmrc
52
+
53
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format documentation
3
+ --profile
@@ -28,15 +28,21 @@ module Ostiary
28
28
  # Exclude action(s) from requiring a role
29
29
  # except: [*actions]
30
30
  # By default a given role will be required for every action
31
- #
31
+ # Override role checking by passing a symbol as method;
32
+ # ostiary_policy method: :master?, only: :show
32
33
  # One line creates one policy, which are immediately created with the proper class
33
- def ostiary_policy(role, actions = {})
34
+ def ostiary_policy(role = nil, only: nil, except: nil, method: nil)
35
+ raise ArgumentError, "Use at least role or method" unless method || role
36
+ raise ArgumentError, "Use either role or method" if method && role
37
+ raise ArgumentError, "Use either only or except" if except && only
38
+ raise ArgumentError, "Use a symbol for method:" if method && !(method.is_a? Symbol)
39
+
34
40
  if actions.empty?
35
- self.ostiary.policies << Policy.new(role)
36
- elsif actions.has_key?(:only)
37
- self.ostiary.policies << PolicyLimited.new(role, actions[:only])
38
- elsif actions.has_key?(:except)
39
- self.ostiary.policies << PolicyExempted.new(role, actions[:except])
41
+ ostiary.policies << Policy.new(role, method: method&.to_proc)
42
+ elsif only
43
+ ostiary.policies << PolicyLimited.new(role, only, method: method&.to_proc)
44
+ elsif except
45
+ ostiary.policies << PolicyExempted.new(role, except, method: method&.to_proc)
40
46
  end
41
47
  end
42
48
 
@@ -6,24 +6,18 @@ module Ostiary
6
6
  @policies = []
7
7
  end
8
8
 
9
- def authorize!(action)
9
+ def authorize!(action, &block)
10
10
  policies.each do |policy|
11
- next if policy_met?(policy, action, &Proc.new)
11
+ next if policy.met?(action, block)
12
12
  raise PolicyBroken, policy.error_message(action)
13
13
  end
14
14
  end
15
15
 
16
- def authorized?(action)
16
+ def authorized?(action, &block)
17
17
  policies.all? do |policy|
18
- policy_met?(policy, action, &Proc.new)
18
+ policy.met?(action, block)
19
19
  end
20
20
  end
21
21
 
22
- private
23
-
24
- def policy_met?(policy, action)
25
- policy.met?(action) { yield(policy.name) }
26
- end
27
-
28
22
  end
29
23
  end
@@ -1,18 +1,20 @@
1
1
  module Ostiary
2
2
  class Policy
3
- attr_accessor :name, :rules
3
+ attr_reader :name, :method, :actions
4
4
 
5
- def initialize(name, rules = [])
6
- @name = name
7
- @rules = rules || []
5
+ def initialize(name, actions = [], method: nil)
6
+ @name = name
7
+ @method = method
8
+ @actions = actions
8
9
  end
9
10
 
10
11
  def inspect
11
12
  "#{name}"
12
13
  end
13
14
 
14
- def met?(*)
15
- yield
15
+ def met?(_action)
16
+ return yield name unless method
17
+ method.call
16
18
  end
17
19
 
18
20
  def error_message(action)
@@ -2,12 +2,12 @@ module Ostiary
2
2
  class PolicyExempted < Policy
3
3
 
4
4
  def inspect
5
- "#{name} except for #{rules.to_sentence}"
5
+ "#{name} except for #{actions.to_sentence}"
6
6
  end
7
7
 
8
8
  def met?(action)
9
- return true if rules.include?(action)
10
- yield
9
+ return true if actions.include?(action)
10
+ super
11
11
  end
12
12
 
13
13
  def error_message(action)
@@ -2,12 +2,12 @@ module Ostiary
2
2
  class PolicyLimited < Policy
3
3
 
4
4
  def inspect
5
- "#{name} only for #{rules.to_sentence}"
5
+ "#{name} only for #{actions.to_sentence}"
6
6
  end
7
7
 
8
8
  def met?(action)
9
- return true if !rules.include?(action)
10
- yield
9
+ return true unless actions.include?(action)
10
+ super
11
11
  end
12
12
 
13
13
  def error_message(action)
@@ -1,3 +1,3 @@
1
1
  module Ostiary
2
- VERSION = "0.9.0"
2
+ VERSION = "0.10.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ostiary
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jacques Hakvoort
@@ -67,11 +67,12 @@ executables: []
67
67
  extensions: []
68
68
  extra_rdoc_files: []
69
69
  files:
70
+ - ".gitignore"
71
+ - ".rspec"
70
72
  - ".ruby-gemset"
71
73
  - ".ruby-version"
72
74
  - ".travis.yml"
73
75
  - Gemfile
74
- - Gemfile.lock
75
76
  - LICENSE.txt
76
77
  - README.md
77
78
  - Rakefile
data/Gemfile.lock DELETED
@@ -1,35 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- ostiary (0.9.0)
5
-
6
- GEM
7
- remote: https://rubygems.org/
8
- specs:
9
- diff-lcs (1.2.5)
10
- rake (10.5.0)
11
- rspec (3.5.0)
12
- rspec-core (~> 3.5.0)
13
- rspec-expectations (~> 3.5.0)
14
- rspec-mocks (~> 3.5.0)
15
- rspec-core (3.5.4)
16
- rspec-support (~> 3.5.0)
17
- rspec-expectations (3.5.0)
18
- diff-lcs (>= 1.2.0, < 2.0)
19
- rspec-support (~> 3.5.0)
20
- rspec-mocks (3.5.0)
21
- diff-lcs (>= 1.2.0, < 2.0)
22
- rspec-support (~> 3.5.0)
23
- rspec-support (3.5.0)
24
-
25
- PLATFORMS
26
- ruby
27
-
28
- DEPENDENCIES
29
- bundler (~> 1.13)
30
- ostiary!
31
- rake (~> 10.0)
32
- rspec (~> 3.0)
33
-
34
- BUNDLED WITH
35
- 1.16.1