policy_check 0.1.0 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b9da007a15a7dec7f84d7752477e67a5ad9e157adba82d5927809770f25fa561
4
- data.tar.gz: bfc62ac4ecc7cfd5d4647741b508ebea6614ad224bc27fa3cb042d61ec5dce51
3
+ metadata.gz: a4b25415b61494e375278f7c2279d387004e91120f2331bb4553614a26857bd3
4
+ data.tar.gz: 4ba22e0be2d3f2e43a356259ad2398df00622e8ab233e1eb6df6820e9f75b316
5
5
  SHA512:
6
- metadata.gz: 999496e740414550cc81955aaabf761a0e427d2c37261cb8a1fad61c91c3ff4767f0317ba50c400dca0ec3422d7e4353bfe03e92cb1569e156d2a57bc4064fff
7
- data.tar.gz: 6268828a9de862256b57d6f66b5e7ebbfe7af5ef864c72e876418f042adf3a9a5c2e3296457c447ea46dcc26e7c12e18580fa5114cc46271a05650429e485b31
6
+ metadata.gz: 571ee7602757f70b7e635497a50dd44211ac5342a115120f39ecfa1e6ec3861efba4797b36f1d5ebdac15a2d982f41fde9c741b5369936ff801d56794ea9f069
7
+ data.tar.gz: cff979634a25bed373743eadc0ee8519dfd43410ae6f96b0cffaecb2412e222030d81ac105aacc2b0ff2782d650ccc03afc11d8de0e327a6010c7bd568b4c3df
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- policy_check (0.1.0)
4
+ policy_check (0.1.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -2,14 +2,14 @@
2
2
 
3
3
  # PolicyCheck
4
4
 
5
- PolicyCheck provides a DSL for policy definitions and allows you to get reasons for policy violations.
5
+ DSL for policy definitions and allows get reasons for policy violations.
6
6
 
7
7
  ## Installation
8
8
 
9
9
  Add this line to your application's Gemfile:
10
10
 
11
11
  ```ruby
12
- gem 'policy_check', github: 'hazi/policy_check'
12
+ gem 'policy_check'
13
13
  ```
14
14
 
15
15
  And then execute:
@@ -19,11 +19,19 @@ And then execute:
19
19
 
20
20
  ## Usage
21
21
 
22
+ ```ruby
23
+ class Post
24
+ extend PolicyCheck #-> only add `.policy` method
25
+ end
26
+ ```
27
+
28
+ ## Example
29
+
22
30
  ### Inline policy
23
31
 
24
32
  ```ruby
25
33
  class Post
26
- extend PolicyCheck #-> only add `.policy` method
34
+ extend PolicyCheck
27
35
 
28
36
  def initialize
29
37
  @status = :draft
@@ -54,7 +62,7 @@ post.publishable? #=> false
54
62
 
55
63
  ```ruby
56
64
  class PostPublishablePolicy
57
- extend PolicyCheck #-> only add `.policy`
65
+ extend PolicyCheck
58
66
 
59
67
  def initialize(post, user)
60
68
  @post = post
@@ -66,7 +74,7 @@ class PostPublishablePolicy
66
74
  !user.admin?
67
75
  end
68
76
 
69
- policy do #-> only create `#valid?` and `#error_messages` method
77
+ policy do #-> only create `#valid?`, `#invalid?` and `#error_messages` method
70
78
  error "user is not admin", &:not_admin?
71
79
  error("status is not `draft`") { post.status != :draft }
72
80
  error("body is empty") { post.body.empty? }
@@ -75,11 +83,32 @@ end
75
83
 
76
84
  post = Post.find(1)
77
85
  user = current_user
78
- PostPublishablePolicy.new(post, user).error_messages #=> ["body is empty", "write is not admin"]
86
+ PostPublishablePolicy.new(post, user).error_messages #=> ["body is empty", "user is not admin"]
79
87
  PostPublishablePolicy.new(post, user).valid? #=> false
80
88
  PostPublishablePolicy.new(post, user).invalid? #=> true
81
89
  ```
82
90
 
91
+ ## Compatibility
92
+
93
+ PolicyCheck officially supports the following runtime Ruby implementations:
94
+
95
+ - MRI 2.7, 3.0, 3.1, 3.2
96
+
97
+ ## Development
98
+
99
+ After checking out the repo, run `bin/setup` to install dependencies.
100
+ Then, run `bundle exec rspec` to run the tests.
101
+ Before committing, run `bundle exec rubocop` to perform a style check.
102
+ You can also run `bin/console` for an interactive prompt that will allow you to experiment.
103
+
104
+ ## Contribution
105
+
106
+ 1. Fork it ( https://github.com/hazi/policy_check/fork )
107
+ 2. Create your feature branch (git checkout -b my-new-feature)
108
+ 3. Commit your changes (git commit -am 'Add some feature')
109
+ 4. Push to the branch (git push origin my-new-feature)
110
+ 5. Create new Pull Request
111
+
83
112
  ## License
84
113
 
85
114
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PolicyCheck
4
- # Determines the error
4
+ # Model representing one error defined in the {PolicyCheck#policy} block
5
5
  class Error
6
6
  def initialize(model, message, &block)
7
7
  @model = model
@@ -3,12 +3,13 @@
3
3
  require "policy_check/error"
4
4
 
5
5
  module PolicyCheck
6
- # Create a summary error model and summarize the decision
6
+ # A model that summarizes the errors defined in the {PolicyCheck#policy} block
7
7
  class ErrorList
8
8
  def initialize(model, &block)
9
9
  @model = model
10
10
  @block = block
11
- @errors = instance_eval(&block)
11
+ @errors = []
12
+ instance_eval(&block)
12
13
  end
13
14
 
14
15
  # @return [Boolean] true if all errors are false
@@ -27,7 +28,6 @@ module PolicyCheck
27
28
 
28
29
  # called when a policy is defined
29
30
  def error(message, &block)
30
- @errors ||= []
31
31
  @errors << Error.new(model, message, &block)
32
32
  end
33
33
  end
@@ -5,6 +5,7 @@ require "policy_check/error_list"
5
5
  module PolicyCheck
6
6
  # Module Builder to define methods to make decisions
7
7
  class ModuleBuilder < Module
8
+ # {PolicyCheck#policy} method will call this method to create a ModuleBuilder instance
8
9
  def initialize(name, &block) # rubocop:disable Lint/MissingSuper, Metrics/MethodLength
9
10
  define_method name ? "#{name}?" : "valid?" do
10
11
  errors = ErrorList.new(self, &block)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PolicyCheck
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.2"
5
5
  end
data/lib/policy_check.rb CHANGED
@@ -25,9 +25,9 @@ require "policy_check/module_builder"
25
25
  # end
26
26
  # end
27
27
  module PolicyCheck
28
+ # If a name is specified "#\\{name}?" and "#\\{name}_errors" method are added.
29
+ # if name is `nil`, add `#error_messages`, `#valid?` and `#invalid?` method
28
30
  # @param [#to_sym] name policy name, default is `nil`.
29
- # If a name is specified "#{name}?" and "#{name}_errors" method are added.
30
- # if `nil`, add `#error_messages`, `#valid?` and `#invalid?` method
31
31
  def policy(name = nil, &block)
32
32
  name = name&.to_sym
33
33
  class_exec { include PolicyCheck::ModuleBuilder.new(name, &block) }
data/policy_check.gemspec CHANGED
@@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
12
12
  spec.authors = ["HAZI"]
13
13
  spec.email = ["yuhei.mukoyama@gmail.com"]
14
14
 
15
- spec.summary = "Create policies for models or policy class"
15
+ spec.summary = "DSL for policy definitions and allows get reasons for policy violations."
16
16
  spec.homepage = "https://github.com/hazi/policy_check"
17
17
  spec.license = "MIT"
18
18
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: policy_check
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - HAZI
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-01-31 00:00:00.000000000 Z
11
+ date: 2023-02-05 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -59,5 +59,5 @@ requirements: []
59
59
  rubygems_version: 3.3.26
60
60
  signing_key:
61
61
  specification_version: 4
62
- summary: Create policies for models or policy class
62
+ summary: DSL for policy definitions and allows get reasons for policy violations.
63
63
  test_files: []