tram-policy 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 22af5024252d8e51fc4147c26998ea99f3131cbc
4
- data.tar.gz: e5ed607e1ffa5fabb805f611b0f227592c09ead4
3
+ metadata.gz: 165ccec9b65cb71c73b55cd3f4ec68fe806eaf93
4
+ data.tar.gz: f0b95281258862f368a2a7f8f9f4c800c6e71231
5
5
  SHA512:
6
- metadata.gz: e0c48d928ca99dc24d290121d557177c395dd994886b519f51b518b04d99bac2f6324e4257a26171d01e54df9b4d253c42f20d3b643c51ac8c9538d324aabdac
7
- data.tar.gz: d93356f5f5e2feb002dad6915f27c90d3094dd2f887b76c4414c08b6b3a0e09d9b2253fdd2a72497dbc5af8a677af827218bc661a9293408bd39a6b3251c9a75
6
+ metadata.gz: c5b1080576012e698a2664f0d30c61d356993f58895b67c92309567076e87c7d26a6c0eac230d994dcf17ce67b111d73690745ac7910c4ac76dd46e3494bb5a9
7
+ data.tar.gz: 936c1c08771c15b2f8ab3d48d5e1b3bc7b1e3645fadcec02450c8655ee65333bd3589bae2be28f0690044882f9aeae329ef08576a57d4e7ecf1dfb3f38e5f487
data/CHANGELOG.md CHANGED
@@ -4,6 +4,15 @@ 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.4] - [2017-12-03]
8
+
9
+ Some private methods has been refactored
10
+
11
+ ## Internals
12
+ - Renamed `Tram::Policy@__options__` -> `Tram::Policy##__attributes__` (nepalez)
13
+ - Removed `Tram::Policy::Validator##scope` in favor of `Tram::Policy.scope` (nepalez)
14
+ - Refactored spec matcher (nepalez)
15
+
7
16
  ## [0.2.3] - [2017-11-21]
8
17
 
9
18
  ### Fixed
data/README.md CHANGED
@@ -172,7 +172,7 @@ end
172
172
 
173
173
  Class method `.validate` supports several options:
174
174
 
175
- ### `stop_on_faiure`
175
+ ### `stop_on_failure`
176
176
 
177
177
  If a selected validation will fail (adds an error to the collection), the following validations won't be executed.
178
178
 
@@ -0,0 +1,50 @@
1
+ class Tram::Policy
2
+ # Class-level DSL for policy objects
3
+ module DSL
4
+ # @!method validate(name, opts)
5
+ # Registers a validator
6
+ #
7
+ # @param [#to_sym, nil] name (nil)
8
+ # @option opts [Boolean] :stop_on_failure
9
+ # @return [self]
10
+ #
11
+ def validate(name = nil, **opts, &block)
12
+ local_validators << Validator.new(name, block, opts)
13
+ self
14
+ end
15
+
16
+ # Policy constructor/validator (alias for [.new])
17
+ #
18
+ # @param [Object] *args
19
+ # @return [Tram::Policy]
20
+ #
21
+ def [](*args)
22
+ new(*args)
23
+ end
24
+
25
+ # Translation scope for a policy
26
+ #
27
+ # @return [Array<String>]
28
+ #
29
+ def scope
30
+ @scope ||= ["tram-policy", *Inflector.underscore(name)]
31
+ end
32
+
33
+ # List of validators defined by a policy per se
34
+ #
35
+ # @return [Array<Proc>]
36
+ #
37
+ def local_validators
38
+ @local_validators ||= []
39
+ end
40
+
41
+ # List of all applicable validators from both the policy and its parent
42
+ #
43
+ # @return [Array<Proc>]
44
+ #
45
+ def validators
46
+ parent_validators = self == Tram::Policy ? [] : superclass.validators
47
+ (parent_validators + local_validators).uniq
48
+ end
49
+ end
50
+ end
@@ -26,37 +26,20 @@ RSpec::Matchers.define :be_invalid_at do |**tags|
26
26
  @tags ||= {}
27
27
  end
28
28
 
29
- def messages
30
- @messages ||= {}
31
- end
32
-
33
29
  # ****************************************************************************
34
30
  # Helpers to provide results for all locales
35
31
  # ****************************************************************************
36
32
 
37
- # Runs block in every available locale
38
- def in_available_locales
39
- locales = if respond_to?(:available_locales)
40
- available_locales
41
- else
42
- I18n.available_locales
43
- end
44
-
45
- locales.flat_map { |locale| I18n.with_locale(locale) { yield } }
46
- end
47
-
48
33
  def prepare_localized_results(policy_block, tags, locale)
49
- localized_policy = policy_block.call
50
- localized_errors = localized_policy&.errors || []
51
- self.policy = localized_policy.inspect
52
- errors[locale] = localized_errors.by_tags(tags)
53
- messages[locale] = localized_errors.full_messages
34
+ I18n.locale = locale
35
+ local_policy = policy_block.call
36
+ self.policy = local_policy.inspect
37
+ errors[locale] = local_policy&.errors&.by_tags(tags)
54
38
  end
55
39
 
56
40
  def prepare_results(policy_block, tags)
57
41
  original = I18n.locale
58
42
  I18n.available_locales.each do |locale|
59
- I18n.locale = locale
60
43
  prepare_localized_results(policy_block, tags, locale)
61
44
  end
62
45
  ensure
@@ -85,9 +68,9 @@ RSpec::Matchers.define :be_invalid_at do |**tags|
85
68
 
86
69
  def report_errors
87
70
  text = "Actual errors:\n"
88
- messages.each do |locale, list|
71
+ errors.each do |locale, local_errors|
89
72
  text << " #{locale}:\n"
90
- list.each { |item| text << " - #{item}\n" }
73
+ local_errors&.each { |error| text << " - #{error.full_message}\n" }
91
74
  end
92
75
  text
93
76
  end
@@ -1,21 +1,19 @@
1
1
  class Tram::Policy
2
2
  # @private
3
3
  class Validator
4
- attr_reader :scope, :name, :block, :stop_on_failure
4
+ attr_reader :name, :block, :stop_on_failure
5
5
 
6
6
  def ==(other)
7
7
  other.is_a?(self.class) && name && other.name == name
8
8
  end
9
9
 
10
10
  def check(object)
11
- object.__send__ :instance_variable_set, :@__scope__, scope
12
11
  name ? object.__send__(name) : object.instance_exec(&block)
13
12
  end
14
13
 
15
14
  private
16
15
 
17
- def initialize(scope, name, block, stop_on_failure: false)
18
- @scope = scope
16
+ def initialize(name, block, stop_on_failure: false)
19
17
  @name = name&.to_sym
20
18
  @block = block
21
19
  raise "Provide either method name or a block" unless !name ^ !block
data/lib/tram/policy.rb CHANGED
@@ -10,55 +10,10 @@ module Tram
10
10
  require_relative "policy/error"
11
11
  require_relative "policy/errors"
12
12
  require_relative "policy/validator"
13
+ require_relative "policy/dsl"
13
14
 
14
15
  extend Dry::Initializer
15
-
16
- class << self
17
- # @!method validate(name, opts)
18
- # Registers a validator
19
- #
20
- # @param [#to_sym, nil] name (nil)
21
- # @option opts [Boolean] :stop_on_failure
22
- # @return [self]
23
- #
24
- def validate(name = nil, **opts, &block)
25
- local << Validator.new(scope, name, block, opts)
26
- self
27
- end
28
-
29
- # Policy constructor/validator (alias for [.new])
30
- #
31
- # @param [Object] *args
32
- # @return [Tram::Policy]
33
- #
34
- def [](*args)
35
- new(*args)
36
- end
37
-
38
- # Translation scope for a policy
39
- #
40
- # @return [Array<String>]
41
- #
42
- def scope
43
- @scope ||= ["tram-policy", *Inflector.underscore(name)]
44
- end
45
-
46
- # List of validators defined by a policy per se
47
- #
48
- # @return [Array<Proc>]
49
- #
50
- def local
51
- @local ||= []
52
- end
53
-
54
- # List of all applicable validators from both the policy and its parent
55
- #
56
- # @return [Array<Proc>]
57
- #
58
- def all
59
- (((self == Tram::Policy) ? [] : superclass.send(:all)) + local).uniq
60
- end
61
- end
16
+ extend DSL
62
17
 
63
18
  # Translates a message in the scope of current policy
64
19
  #
@@ -66,9 +21,9 @@ module Tram
66
21
  # @param [Hash<Symbol, Object>] options
67
22
  # @return [String]
68
23
  #
69
- def t(message, options = {})
24
+ def t(message, **options)
70
25
  return message.to_s unless message.is_a? Symbol
71
- I18n.t message, options.merge(scope: @__scope__)
26
+ I18n.t message, scope: self.class.scope, **options
72
27
  end
73
28
 
74
29
  # Collection of validation errors
@@ -116,7 +71,7 @@ module Tram
116
71
  # @return [String]
117
72
  #
118
73
  def inspect
119
- "#<#{self.class.name}[#{@__options__}]>"
74
+ "#<#{self.class.name}[#{__attributes__}]>"
120
75
  end
121
76
 
122
77
  private
@@ -124,13 +79,15 @@ module Tram
124
79
  def initialize(*)
125
80
  super
126
81
 
127
- @__options__ = self.class.dry_initializer.attributes(self)
128
-
129
- self.class.send(:all).each do |validator|
82
+ self.class.validators.each do |validator|
130
83
  size = errors.count
131
84
  validator.check(self)
132
85
  break if (errors.count > size) && validator.stop_on_failure
133
86
  end
134
87
  end
88
+
89
+ def __attributes__
90
+ @__attributes__ ||= self.class.dry_initializer.attributes(self)
91
+ end
135
92
  end
136
93
  end
data/tram-policy.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = "tram-policy"
3
- gem.version = "0.2.3"
3
+ gem.version = "0.2.4"
4
4
  gem.author = ["Viktor Sokolov (gzigzigzeo)", "Andrew Kozin (nepalez)"]
5
5
  gem.email = "andrew.kozin@gmail.com"
6
6
  gem.homepage = "https://github.com/tram/tram-policy"
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.3
4
+ version: 0.2.4
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-11-21 00:00:00.000000000 Z
12
+ date: 2017-12-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: dry-initializer
@@ -131,6 +131,7 @@ files:
131
131
  - bin/tram-policy
132
132
  - lib/tram-policy.rb
133
133
  - lib/tram/policy.rb
134
+ - lib/tram/policy/dsl.rb
134
135
  - lib/tram/policy/error.rb
135
136
  - lib/tram/policy/errors.rb
136
137
  - lib/tram/policy/generator.rb