policy_check 0.1.0

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
+ SHA256:
3
+ metadata.gz: b9da007a15a7dec7f84d7752477e67a5ad9e157adba82d5927809770f25fa561
4
+ data.tar.gz: bfc62ac4ecc7cfd5d4647741b508ebea6614ad224bc27fa3cb042d61ec5dce51
5
+ SHA512:
6
+ metadata.gz: 999496e740414550cc81955aaabf761a0e427d2c37261cb8a1fad61c91c3ff4767f0317ba50c400dca0ec3422d7e4353bfe03e92cb1569e156d2a57bc4064fff
7
+ data.tar.gz: 6268828a9de862256b57d6f66b5e7ebbfe7af5ef864c72e876418f042adf3a9a5c2e3296457c447ea46dcc26e7c12e18580fa5114cc46271a05650429e485b31
@@ -0,0 +1,24 @@
1
+ name: lint
2
+
3
+ on: [push, pull_request]
4
+
5
+ permissions: # added using https://github.com/step-security/secure-workflows
6
+ contents: read
7
+
8
+ jobs:
9
+ lint:
10
+ runs-on: ubuntu-latest
11
+ continue-on-error: true
12
+ timeout-minutes: 10
13
+
14
+ steps:
15
+ - uses: actions/checkout@v3
16
+
17
+ - name: Set up Ruby
18
+ uses: ruby/setup-ruby@v1
19
+ with:
20
+ ruby-version: '3.1'
21
+ bundler-cache: true
22
+
23
+ - name: Run rubocop
24
+ run: bundle exec rubocop
@@ -0,0 +1,27 @@
1
+ name: test
2
+
3
+ on: [push, pull_request]
4
+
5
+ permissions: # added using https://github.com/step-security/secure-workflows
6
+ contents: read
7
+
8
+ jobs:
9
+ test:
10
+ runs-on: ubuntu-latest
11
+ timeout-minutes: 10
12
+
13
+ strategy:
14
+ matrix:
15
+ ruby-version: [3.2, 3.1, '3.0', 2.7, ruby-head]
16
+
17
+ steps:
18
+ - uses: actions/checkout@v3
19
+
20
+ - name: Set up Ruby ${{ matrix.ruby-version }}
21
+ uses: ruby/setup-ruby@v1
22
+ with:
23
+ ruby-version: ${{ matrix.ruby-version }}
24
+ bundler-cache: true
25
+
26
+ - name: Run tests
27
+ run: bundle exec rspec
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /vendor
10
+ /.ruby-version
11
+ /policy_check-*.gem
12
+
13
+ # rspec failure tracking
14
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,10 @@
1
+ require:
2
+ - rubocop-rspec
3
+ - rubocop-rake
4
+
5
+ AllCops:
6
+ TargetRubyVersion: 2.7
7
+ NewCops: enable
8
+
9
+ Style/StringLiterals:
10
+ EnforcedStyle: double_quotes
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in policy_check.gemspec
6
+ gemspec
7
+
8
+ group :development do
9
+ gem "bundler"
10
+ gem "rake"
11
+ gem "rspec"
12
+ gem "rubocop"
13
+ gem "rubocop-rake"
14
+ gem "rubocop-rspec"
15
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,67 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ policy_check (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ diff-lcs (1.5.0)
11
+ json (2.6.3)
12
+ parallel (1.22.1)
13
+ parser (3.2.0.0)
14
+ ast (~> 2.4.1)
15
+ rainbow (3.1.1)
16
+ rake (13.0.6)
17
+ regexp_parser (2.6.2)
18
+ rexml (3.2.5)
19
+ rspec (3.12.0)
20
+ rspec-core (~> 3.12.0)
21
+ rspec-expectations (~> 3.12.0)
22
+ rspec-mocks (~> 3.12.0)
23
+ rspec-core (3.12.0)
24
+ rspec-support (~> 3.12.0)
25
+ rspec-expectations (3.12.2)
26
+ diff-lcs (>= 1.2.0, < 2.0)
27
+ rspec-support (~> 3.12.0)
28
+ rspec-mocks (3.12.3)
29
+ diff-lcs (>= 1.2.0, < 2.0)
30
+ rspec-support (~> 3.12.0)
31
+ rspec-support (3.12.0)
32
+ rubocop (1.44.1)
33
+ json (~> 2.3)
34
+ parallel (~> 1.10)
35
+ parser (>= 3.2.0.0)
36
+ rainbow (>= 2.2.2, < 4.0)
37
+ regexp_parser (>= 1.8, < 3.0)
38
+ rexml (>= 3.2.5, < 4.0)
39
+ rubocop-ast (>= 1.24.1, < 2.0)
40
+ ruby-progressbar (~> 1.7)
41
+ unicode-display_width (>= 2.4.0, < 3.0)
42
+ rubocop-ast (1.24.1)
43
+ parser (>= 3.1.1.0)
44
+ rubocop-capybara (2.17.0)
45
+ rubocop (~> 1.41)
46
+ rubocop-rake (0.6.0)
47
+ rubocop (~> 1.0)
48
+ rubocop-rspec (2.18.1)
49
+ rubocop (~> 1.33)
50
+ rubocop-capybara (~> 2.17)
51
+ ruby-progressbar (1.11.0)
52
+ unicode-display_width (2.4.2)
53
+
54
+ PLATFORMS
55
+ ruby
56
+
57
+ DEPENDENCIES
58
+ bundler
59
+ policy_check!
60
+ rake
61
+ rspec
62
+ rubocop
63
+ rubocop-rake
64
+ rubocop-rspec
65
+
66
+ BUNDLED WITH
67
+ 2.4.5
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 yuhei mukoyama
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,85 @@
1
+ [![test](https://github.com/hazi/policy_check/actions/workflows/test.yml/badge.svg)](https://github.com/hazi/policy_check/actions/workflows/test.yml) [![lint](https://github.com/hazi/policy_check/actions/workflows/lint.yml/badge.svg)](https://github.com/hazi/policy_check/actions/workflows/lint.yml)
2
+
3
+ # PolicyCheck
4
+
5
+ PolicyCheck provides a DSL for policy definitions and allows you to get reasons for policy violations.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'policy_check', github: 'hazi/policy_check'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+
20
+ ## Usage
21
+
22
+ ### Inline policy
23
+
24
+ ```ruby
25
+ class Post
26
+ extend PolicyCheck #-> only add `.policy` method
27
+
28
+ def initialize
29
+ @status = :draft
30
+ @body = ""
31
+ end
32
+
33
+ def not_draft?
34
+ @status != :draft
35
+ end
36
+
37
+ policy :publishable do #-> only create `#publishable?` and `#publishable_errors` method
38
+ error "status is not draft", &:not_draft?
39
+ error("body is empty") { @body.empty? }
40
+ end
41
+
42
+ def publish!
43
+ fail publishable_errors.join(", ") unless publishable?
44
+ publish
45
+ end
46
+ end
47
+
48
+ post = Post.new
49
+ post.publishable_errors #=> ["body is empty"]
50
+ post.publishable? #=> false
51
+ ```
52
+
53
+ ### Policy class
54
+
55
+ ```ruby
56
+ class PostPublishablePolicy
57
+ extend PolicyCheck #-> only add `.policy`
58
+
59
+ def initialize(post, user)
60
+ @post = post
61
+ @user = user
62
+ end
63
+ attr_reader :post, :user
64
+
65
+ def not_admin?
66
+ !user.admin?
67
+ end
68
+
69
+ policy do #-> only create `#valid?` and `#error_messages` method
70
+ error "user is not admin", &:not_admin?
71
+ error("status is not `draft`") { post.status != :draft }
72
+ error("body is empty") { post.body.empty? }
73
+ end
74
+ end
75
+
76
+ post = Post.find(1)
77
+ user = current_user
78
+ PostPublishablePolicy.new(post, user).error_messages #=> ["body is empty", "write is not admin"]
79
+ PostPublishablePolicy.new(post, user).valid? #=> false
80
+ PostPublishablePolicy.new(post, user).invalid? #=> true
81
+ ```
82
+
83
+ ## License
84
+
85
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+ require "rubocop/rake_task"
6
+
7
+ RSpec::Core::RakeTask.new(:spec)
8
+ RuboCop::RakeTask.new
9
+
10
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "policy_check"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PolicyCheck
4
+ # Determines the error
5
+ class Error
6
+ def initialize(model, message, &block)
7
+ @model = model
8
+ @block = block
9
+ @message = message
10
+ end
11
+ attr_reader :model, :message
12
+
13
+ def error?
14
+ model.instance_eval(&@block)
15
+ end
16
+
17
+ def error_message
18
+ error? ? message : nil
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "policy_check/error"
4
+
5
+ module PolicyCheck
6
+ # Create a summary error model and summarize the decision
7
+ class ErrorList
8
+ def initialize(model, &block)
9
+ @model = model
10
+ @block = block
11
+ @errors = instance_eval(&block)
12
+ end
13
+
14
+ # @return [Boolean] true if all errors are false
15
+ def valid?
16
+ errors.none?(&:error?)
17
+ end
18
+
19
+ # @return [Array<String>] error messages
20
+ def error_messages
21
+ errors.map(&:error_message).compact
22
+ end
23
+
24
+ private
25
+
26
+ attr_reader :model, :block, :errors
27
+
28
+ # called when a policy is defined
29
+ def error(message, &block)
30
+ @errors ||= []
31
+ @errors << Error.new(model, message, &block)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "policy_check/error_list"
4
+
5
+ module PolicyCheck
6
+ # Module Builder to define methods to make decisions
7
+ class ModuleBuilder < Module
8
+ def initialize(name, &block) # rubocop:disable Lint/MissingSuper, Metrics/MethodLength
9
+ define_method name ? "#{name}?" : "valid?" do
10
+ errors = ErrorList.new(self, &block)
11
+ errors.valid?
12
+ end
13
+
14
+ if name.nil?
15
+ define_method "invalid?" do
16
+ errors = ErrorList.new(self, &block)
17
+ !errors.valid?
18
+ end
19
+ end
20
+
21
+ define_method name ? "#{name}_errors" : "error_messages" do
22
+ errors = ErrorList.new(self, &block)
23
+ errors.error_messages
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PolicyCheck
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "policy_check/version"
4
+ require "policy_check/module_builder"
5
+
6
+ # extend this module to add `.policy` method
7
+ #
8
+ # @example inline policy
9
+ # class Post
10
+ # extend PolicyCheck #-> only add `.policy` method
11
+ #
12
+ # policy :publishable do #-> only create `#publishable?` and `#publishable_errors` method
13
+ # error 'status is not draft', &:not_draft?
14
+ # error('body is empty') { @body.empty? }
15
+ # end
16
+ # end
17
+ #
18
+ # @example policy class
19
+ # class Post
20
+ # extend PolicyCheck #-> only add `.policy` method
21
+ #
22
+ # policy do #-> only create `#valid?`, `#invalid?` and `#error_messages` method
23
+ # error 'status is not draft', &:not_draft?
24
+ # error('body is empty') { @body.empty? }
25
+ # end
26
+ # end
27
+ module PolicyCheck
28
+ # @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
+ def policy(name = nil, &block)
32
+ name = name&.to_sym
33
+ class_exec { include PolicyCheck::ModuleBuilder.new(name, &block) }
34
+ end
35
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path("lib", __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "policy_check/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.required_ruby_version = ">= 2.7.0"
9
+
10
+ spec.name = "policy_check"
11
+ spec.version = PolicyCheck::VERSION
12
+ spec.authors = ["HAZI"]
13
+ spec.email = ["yuhei.mukoyama@gmail.com"]
14
+
15
+ spec.summary = "Create policies for models or policy class"
16
+ spec.homepage = "https://github.com/hazi/policy_check"
17
+ spec.license = "MIT"
18
+
19
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
20
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
21
+ if spec.respond_to?(:metadata)
22
+ spec.metadata["rubygems_mfa_required"] = "true"
23
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
24
+ else
25
+ raise "RubyGems 2.0 or newer is required to protect against " \
26
+ "public gem pushes."
27
+ end
28
+
29
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
30
+ f.match(%r{^(test|spec|features)/})
31
+ end
32
+ spec.require_paths = ["lib"]
33
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: policy_check
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - HAZI
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-01-31 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - yuhei.mukoyama@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".github/workflows/lint.yml"
21
+ - ".github/workflows/test.yml"
22
+ - ".gitignore"
23
+ - ".rspec"
24
+ - ".rubocop.yml"
25
+ - Gemfile
26
+ - Gemfile.lock
27
+ - LICENSE
28
+ - README.md
29
+ - Rakefile
30
+ - bin/console
31
+ - bin/setup
32
+ - lib/policy_check.rb
33
+ - lib/policy_check/error.rb
34
+ - lib/policy_check/error_list.rb
35
+ - lib/policy_check/module_builder.rb
36
+ - lib/policy_check/version.rb
37
+ - policy_check.gemspec
38
+ homepage: https://github.com/hazi/policy_check
39
+ licenses:
40
+ - MIT
41
+ metadata:
42
+ rubygems_mfa_required: 'true'
43
+ allowed_push_host: https://rubygems.org
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 2.7.0
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubygems_version: 3.3.26
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: Create policies for models or policy class
63
+ test_files: []