tram-policy 0.2.0 → 0.2.1
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/.travis.yml +2 -5
- data/CHANGELOG.md +8 -1
- data/README.md +1 -2
- data/lib/tram/policy/error.rb +1 -0
- data/lib/tram/policy/errors.rb +9 -3
- data/lib/tram/policy/generator.rb +1 -0
- data/lib/tram/policy/inflector.rb +3 -0
- data/lib/tram/policy/validator.rb +1 -1
- data/lib/tram/policy.rb +8 -3
- data/spec/fixtures/admin_policy.rb +0 -2
- data/spec/fixtures/user_policy.rb +1 -1
- data/spec/spec_helper.rb +0 -5
- data/tram-policy.gemspec +4 -4
- metadata +6 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0f05aaf4884e7456c1649ac556bdb9510596e69b
|
4
|
+
data.tar.gz: a7179cf1a45d8fa9a637bbf1114e9368ebbcb998
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1f8c7ca5fdf009cad77ccc6b622c36111304c2db5221cb6a109c59b0bd89e443e3521d0e41b0b5c73a0b3c31bffee8a169968bf1aaedf9eb5ae681c047a6284f
|
7
|
+
data.tar.gz: bb4d737514915ca82b2947b014c6542f8342bb32375ff1a8b604ddc3e96c608279a44ad47fcf808097ccac0cdab7859a9aadd0702f3911762128557969e50904
|
data/.travis.yml
CHANGED
@@ -6,19 +6,16 @@ bundler_args: --without benchmarks tools
|
|
6
6
|
script:
|
7
7
|
- bundle exec rake spec
|
8
8
|
rvm:
|
9
|
-
- 2.2
|
10
9
|
- 2.3.0
|
11
10
|
- 2.4.0
|
12
|
-
- jruby-9000
|
13
|
-
- rbx-2
|
14
|
-
- rbx-3
|
15
11
|
- ruby-head
|
12
|
+
- jruby-9.1.0.0
|
13
|
+
- rbx-3
|
16
14
|
env:
|
17
15
|
global:
|
18
16
|
- JRUBY_OPTS='--dev -J-Xmx1024M'
|
19
17
|
matrix:
|
20
18
|
allow_failures:
|
21
|
-
- rvm: rbx-2
|
22
19
|
- rvm: rbx-3
|
23
20
|
- rvm: ruby-head
|
24
21
|
- rvm: jruby-head
|
data/CHANGELOG.md
CHANGED
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
|
|
4
4
|
The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
5
5
|
and this project adheres to [Semantic Versioning](http://semver.org/).
|
6
6
|
|
7
|
+
## [0.2.1] - [2017-08-28]
|
8
|
+
|
9
|
+
### Changed
|
10
|
+
- Updated dependency from [dry-initializer] to v2.0.0 (nepalez)
|
11
|
+
|
7
12
|
## [0.2.0] - [2017-08-19]
|
8
13
|
|
9
14
|
### Added
|
@@ -88,10 +93,12 @@ The gem is battle-tested for production (in a real commertial project).
|
|
88
93
|
## [0.0.1] - [2017-04-18]
|
89
94
|
This is a first public release (@nepalez, @charlie-wasp, @JewelSam, @sergey-chechaev)
|
90
95
|
|
96
|
+
[dry-initializer]: https://github.com/dry-rb/dry-initializer
|
91
97
|
[Unreleased]: https://github.com/tram-rb/tram-policy
|
92
98
|
[0.0.1]: https://github.com/tram-rb/tram-policy/releases/tag/v0.0.1
|
93
99
|
[0.0.2]: https://github.com/tram-rb/tram-policy/compare/v0.0.1...v0.0.2
|
94
100
|
[0.0.3]: https://github.com/tram-rb/tram-policy/compare/v0.0.2...v0.0.3
|
95
101
|
[0.1.0]: https://github.com/tram-rb/tram-policy/compare/v0.0.3...v0.1.0
|
96
102
|
[0.1.1]: https://github.com/tram-rb/tram-policy/compare/v0.1.0...v0.1.1
|
97
|
-
[0.
|
103
|
+
[0.2.0]: https://github.com/tram-rb/tram-policy/compare/v0.1.1...v0.2.0
|
104
|
+
[0.2.1]: https://github.com/tram-rb/tram-policy/compare/v0.2.0...v0.2.1
|
data/README.md
CHANGED
@@ -8,7 +8,6 @@ Policy Object Pattern
|
|
8
8
|
[![Gem Version][gem-badger]][gem]
|
9
9
|
[![Build Status][travis-badger]][travis]
|
10
10
|
[![Dependency Status][gemnasium-badger]][gemnasium]
|
11
|
-
[![Code Climate][codeclimate-badger]][codeclimate]
|
12
11
|
[![Inline docs][inch-badger]][inch]
|
13
12
|
|
14
13
|
## Intro
|
@@ -132,7 +131,7 @@ class Article::PublicationPolicy < Tram::Policy
|
|
132
131
|
end
|
133
132
|
```
|
134
133
|
|
135
|
-
As mentioned above, sending a symbolic key to the `errors#add` means the key should be translated by [I18n][i18n]. The only magic under the hood concerns a scope for the translation. By default it is taken from the full name of current class prepended with `"
|
134
|
+
As mentioned above, sending a symbolic key to the `errors#add` means the key should be translated by [I18n][i18n]. The only magic under the hood concerns a scope for the translation. By default it is taken from the full name of current class prepended with `"tram-policy"`.
|
136
135
|
|
137
136
|
> You can redefine the scope by reloading private method `.scope` of the policy.
|
138
137
|
|
data/lib/tram/policy/error.rb
CHANGED
data/lib/tram/policy/errors.rb
CHANGED
@@ -14,6 +14,7 @@ class Tram::Policy
|
|
14
14
|
#
|
15
15
|
attr_reader :policy
|
16
16
|
|
17
|
+
# @!method add(message, tags)
|
17
18
|
# Adds error message to the collection
|
18
19
|
#
|
19
20
|
# @param [#to_s] message Either a message, or a symbolic key for translation
|
@@ -37,9 +38,10 @@ class Tram::Policy
|
|
37
38
|
@set.each { |error| yield(error) }
|
38
39
|
end
|
39
40
|
|
41
|
+
# @!method by_tags(filter)
|
40
42
|
# Selects errors filtered by tags
|
41
43
|
#
|
42
|
-
# @param [Hash<Symbol, Object>] filter
|
44
|
+
# @param [Hash<Symbol, Object>] filter List of options to filter by
|
43
45
|
# @return [Hash<Symbol, Object>]
|
44
46
|
#
|
45
47
|
def by_tags(**filter)
|
@@ -47,6 +49,7 @@ class Tram::Policy
|
|
47
49
|
reject { |error| (filter - error.to_h.to_a).any? }
|
48
50
|
end
|
49
51
|
|
52
|
+
# @!method empty?
|
50
53
|
# Checks whether a collection is empty
|
51
54
|
#
|
52
55
|
# @return [Boolean]
|
@@ -71,11 +74,14 @@ class Tram::Policy
|
|
71
74
|
@set.map(&:full_message).sort
|
72
75
|
end
|
73
76
|
|
77
|
+
# @!method merge(other, options)
|
74
78
|
# Merges other collection to the current one and returns new collection
|
75
79
|
# with the current scope
|
76
80
|
#
|
77
|
-
# param [Tram::Policy::Errors] other
|
78
|
-
#
|
81
|
+
# @param [Tram::Policy::Errors] other Collection to be merged
|
82
|
+
# @param [Hash<Symbol, Object>] options Options to be added to merged errors
|
83
|
+
# @yieldparam [Hash<Symbol, Object>] hash of error options
|
84
|
+
# @return [self]
|
79
85
|
#
|
80
86
|
# @example Add some tag to merged errors
|
81
87
|
# policy.merge(other) { |err| err[:source] = "other" }
|
@@ -1,9 +1,12 @@
|
|
1
1
|
class Tram::Policy
|
2
2
|
if Object.const_defined? "ActiveSupport::Inflector"
|
3
|
+
# @private
|
3
4
|
Inflector = ActiveSupport::Inflector
|
4
5
|
elsif Object.const_defined? "Inflecto"
|
6
|
+
# @private
|
5
7
|
Inflector = ::Inflecto
|
6
8
|
else
|
9
|
+
# @private
|
7
10
|
module Inflector
|
8
11
|
def self.underscore(name)
|
9
12
|
name&.dup&.tap do |n|
|
data/lib/tram/policy.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require "dry-initializer"
|
2
2
|
require "i18n"
|
3
3
|
|
4
|
+
# Top-level scope for Tram collection of gems
|
4
5
|
module Tram
|
5
6
|
# Base class for policy objects with composable validation errors
|
6
7
|
class Policy
|
@@ -13,9 +14,10 @@ module Tram
|
|
13
14
|
extend Dry::Initializer
|
14
15
|
|
15
16
|
class << self
|
17
|
+
# @!method validate(name, opts)
|
16
18
|
# Registers a validator
|
17
19
|
#
|
18
|
-
# @param [#to_sym,
|
20
|
+
# @param [#to_sym, nil] name (nil)
|
19
21
|
# @option opts [Boolean] :stop_on_failure
|
20
22
|
# @return [self]
|
21
23
|
#
|
@@ -44,7 +46,7 @@ module Tram
|
|
44
46
|
end
|
45
47
|
|
46
48
|
def all
|
47
|
-
((self == Tram::Policy) ? [] : superclass.send(:all)) + local
|
49
|
+
(((self == Tram::Policy) ? [] : superclass.send(:all)) + local).uniq
|
48
50
|
end
|
49
51
|
end
|
50
52
|
|
@@ -54,7 +56,7 @@ module Tram
|
|
54
56
|
# @param [Hash<Symbol, Object>] options
|
55
57
|
# @return [String]
|
56
58
|
#
|
57
|
-
def t(message,
|
59
|
+
def t(message, options = {})
|
58
60
|
return message.to_s unless message.is_a? Symbol
|
59
61
|
I18n.t message, options.merge(scope: @__scope__)
|
60
62
|
end
|
@@ -111,6 +113,9 @@ module Tram
|
|
111
113
|
|
112
114
|
def initialize(*)
|
113
115
|
super
|
116
|
+
|
117
|
+
@__options__ = self.class.dry_initializer.attributes(self)
|
118
|
+
|
114
119
|
self.class.send(:all).each do |validator|
|
115
120
|
size = errors.count
|
116
121
|
validator.check(self)
|
data/spec/spec_helper.rb
CHANGED
data/tram-policy.gemspec
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
Gem::Specification.new do |gem|
|
2
2
|
gem.name = "tram-policy"
|
3
|
-
gem.version = "0.2.
|
3
|
+
gem.version = "0.2.1"
|
4
4
|
gem.author = ["Viktor Sokolov (gzigzigzeo)", "Andrew Kozin (nepalez)"]
|
5
|
-
gem.email =
|
5
|
+
gem.email = "andrew.kozin@gmail.com"
|
6
6
|
gem.homepage = "https://github.com/tram/tram-policy"
|
7
7
|
gem.summary = "Policy Object Pattern"
|
8
8
|
gem.license = "MIT"
|
@@ -12,9 +12,9 @@ Gem::Specification.new do |gem|
|
|
12
12
|
gem.extra_rdoc_files = Dir["README.md", "LICENSE", "CHANGELOG.md"]
|
13
13
|
gem.executables = %w[tram-policy]
|
14
14
|
|
15
|
-
gem.required_ruby_version = ">= 2.
|
15
|
+
gem.required_ruby_version = ">= 2.3"
|
16
16
|
|
17
|
-
gem.add_runtime_dependency "dry-initializer", "~>
|
17
|
+
gem.add_runtime_dependency "dry-initializer", "~> 2.0"
|
18
18
|
gem.add_runtime_dependency "i18n", "~> 0.8"
|
19
19
|
|
20
20
|
gem.add_development_dependency "rake", "> 10"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tram-policy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Viktor Sokolov (gzigzigzeo)
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2017-08-
|
12
|
+
date: 2017-08-29 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: dry-initializer
|
@@ -17,14 +17,14 @@ dependencies:
|
|
17
17
|
requirements:
|
18
18
|
- - "~>"
|
19
19
|
- !ruby/object:Gem::Version
|
20
|
-
version: '
|
20
|
+
version: '2.0'
|
21
21
|
type: :runtime
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
25
|
- - "~>"
|
26
26
|
- !ruby/object:Gem::Version
|
27
|
-
version: '
|
27
|
+
version: '2.0'
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: i18n
|
30
30
|
requirement: !ruby/object:Gem::Requirement
|
@@ -110,8 +110,7 @@ dependencies:
|
|
110
110
|
- !ruby/object:Gem::Version
|
111
111
|
version: '0.19'
|
112
112
|
description:
|
113
|
-
email:
|
114
|
-
- andrew.kozin@gmail.com
|
113
|
+
email: andrew.kozin@gmail.com
|
115
114
|
executables:
|
116
115
|
- tram-policy
|
117
116
|
extensions: []
|
@@ -166,7 +165,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
166
165
|
requirements:
|
167
166
|
- - ">="
|
168
167
|
- !ruby/object:Gem::Version
|
169
|
-
version: '2.
|
168
|
+
version: '2.3'
|
170
169
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
171
170
|
requirements:
|
172
171
|
- - ">="
|