pragma-operation 0.1.1 → 0.1.2
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 +4 -4
- data/lib/pragma/operation/authorization.rb +16 -11
- data/lib/pragma/operation/validation.rb +18 -13
- data/lib/pragma/operation/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a0e4b70a6dbf00fc017919c4a20a4fcab1314caf
|
|
4
|
+
data.tar.gz: 10bd799eabb43d6f76a9d29f80a737df2c2902e0
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ff8e629a189aa67af574569fbf2fa2db77e65e753d553f4d6ae2fc3d8b892693060a223a0f69a6112f8e9ba6ba0479fc44b77550616a9a97e7567f0565ca36a1
|
|
7
|
+
data.tar.gz: 5db34e59a55d5251eb05d0f33283bb83f26027afe491c8150094076eee53af092f0d20fd0f345381f1dfa7c879643f2e9868611b4f6b0b6cbe0f827a94bd1418
|
|
@@ -1,33 +1,38 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
module Pragma
|
|
3
3
|
module Operation
|
|
4
|
+
# Provides integration with {https://github.com/pragmarb/pragma-policy Pragma::Policy}.
|
|
5
|
+
#
|
|
6
|
+
# @author Alessandro Desantis
|
|
4
7
|
module Authorization
|
|
5
8
|
def self.included(base)
|
|
6
9
|
base.extend ClassMethods
|
|
7
10
|
base.include InstanceMethods
|
|
8
11
|
end
|
|
9
12
|
|
|
10
|
-
module ClassMethods
|
|
11
|
-
# Sets the
|
|
13
|
+
module ClassMethods # :nodoc:
|
|
14
|
+
# Sets the policy to use for authorizing this operation.
|
|
12
15
|
#
|
|
13
|
-
# @param klass [Class] a subclass of +Pragma::
|
|
14
|
-
def
|
|
15
|
-
@
|
|
16
|
+
# @param klass [Class] a subclass of +Pragma::Policy::Base+
|
|
17
|
+
def policy(klass)
|
|
18
|
+
@policy = klass
|
|
16
19
|
end
|
|
17
20
|
|
|
18
|
-
# Builds the
|
|
21
|
+
# Builds the policy for the given user and resource, using the previous defined policy
|
|
22
|
+
# class.
|
|
19
23
|
#
|
|
24
|
+
# @param user [Object]
|
|
20
25
|
# @param resource [Object]
|
|
21
26
|
#
|
|
22
|
-
# @return [Pragma::
|
|
27
|
+
# @return [Pragma::Policy::Base]
|
|
23
28
|
#
|
|
24
|
-
# @see #
|
|
25
|
-
def
|
|
26
|
-
@
|
|
29
|
+
# @see #policy
|
|
30
|
+
def build_policy(user:, resource:)
|
|
31
|
+
@policy.new(user: user, resource: resource)
|
|
27
32
|
end
|
|
28
33
|
end
|
|
29
34
|
|
|
30
|
-
module InstanceMethods
|
|
35
|
+
module InstanceMethods # :nodoc:
|
|
31
36
|
# Builds the policy for the current user and the given resource, using the previously
|
|
32
37
|
# defined policy class.
|
|
33
38
|
#
|
|
@@ -1,35 +1,36 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
module Pragma
|
|
3
3
|
module Operation
|
|
4
|
+
# Provides integration with {https://github.com/pragmarb/pragma-contract Pragma::Contract}.
|
|
5
|
+
#
|
|
6
|
+
# @author Alessandro Desantis
|
|
4
7
|
module Validation
|
|
5
8
|
def self.included(base)
|
|
6
9
|
base.extend ClassMethods
|
|
7
10
|
base.include InstanceMethods
|
|
8
11
|
end
|
|
9
12
|
|
|
10
|
-
module ClassMethods
|
|
11
|
-
# Sets the
|
|
13
|
+
module ClassMethods # :nodoc:
|
|
14
|
+
# Sets the contract to use for validating this operation.
|
|
12
15
|
#
|
|
13
|
-
# @param klass [Class] a subclass of +Pragma::
|
|
14
|
-
def
|
|
15
|
-
@
|
|
16
|
+
# @param klass [Class] a subclass of +Pragma::Contract::Base+
|
|
17
|
+
def contract(klass)
|
|
18
|
+
@contract = klass
|
|
16
19
|
end
|
|
17
20
|
|
|
18
|
-
# Builds the
|
|
19
|
-
# class.
|
|
21
|
+
# Builds the contract for the given resource, using the previous defined contract class.
|
|
20
22
|
#
|
|
21
|
-
# @param user [Object]
|
|
22
23
|
# @param resource [Object]
|
|
23
24
|
#
|
|
24
|
-
# @return [Pragma::
|
|
25
|
+
# @return [Pragma::Contract::Base]
|
|
25
26
|
#
|
|
26
|
-
# @see #
|
|
27
|
-
def
|
|
28
|
-
@
|
|
27
|
+
# @see #contract
|
|
28
|
+
def build_contract(resource)
|
|
29
|
+
@contract.new(resource)
|
|
29
30
|
end
|
|
30
31
|
end
|
|
31
32
|
|
|
32
|
-
module InstanceMethods
|
|
33
|
+
module InstanceMethods # :nodoc:
|
|
33
34
|
# Builds the contract for the given resource, using the previously defined contract class.
|
|
34
35
|
#
|
|
35
36
|
# This is just an instance-level alias of {.build_contract}. You should use this from inside
|
|
@@ -51,11 +52,13 @@ module Pragma
|
|
|
51
52
|
#
|
|
52
53
|
# @return [Boolean] whether the operation is valid
|
|
53
54
|
def validate(validatable)
|
|
55
|
+
# rubocop:disable Metrics/LineLength
|
|
54
56
|
contract = if defined?(Pragma::Contract::Base) && validatable.is_a?(Pragma::Contract::Base)
|
|
55
57
|
validatable
|
|
56
58
|
else
|
|
57
59
|
build_contract(validatable)
|
|
58
60
|
end
|
|
61
|
+
# rubocop:enable Metrics/LineLength
|
|
59
62
|
|
|
60
63
|
contract.validate(params)
|
|
61
64
|
end
|
|
@@ -65,11 +68,13 @@ module Pragma
|
|
|
65
68
|
#
|
|
66
69
|
# @param validatable [Object|Pragma::Contract::Base] contract or resource
|
|
67
70
|
def validate!(validatable)
|
|
71
|
+
# rubocop:disable Metrics/LineLength
|
|
68
72
|
contract = if defined?(Pragma::Contract::Base) && validatable.is_a?(Pragma::Contract::Base)
|
|
69
73
|
validatable
|
|
70
74
|
else
|
|
71
75
|
build_contract(validatable)
|
|
72
76
|
end
|
|
77
|
+
# rubocop:enable Metrics/LineLength
|
|
73
78
|
|
|
74
79
|
return if validate(contract)
|
|
75
80
|
|