granite 0.9.3 → 0.9.8
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/config/rubocop-default.yml +3 -0
- data/lib/granite.rb +1 -0
- data/lib/granite/action.rb +1 -0
- data/lib/granite/action/precondition.rb +38 -0
- data/lib/granite/action/preconditions.rb +8 -1
- data/lib/granite/action/preconditions/base_precondition.rb +1 -4
- data/lib/granite/action/preconditions/object_precondition.rb +15 -0
- data/lib/granite/base.rb +1 -0
- data/lib/granite/projector/controller_actions.rb +2 -2
- data/lib/granite/represents/attribute.rb +1 -3
- data/lib/granite/util.rb +51 -0
- data/lib/granite/version.rb +1 -1
- data/lib/rubocop-granite.rb +8 -0
- data/lib/rubocop/granite.rb +9 -0
- data/lib/rubocop/granite/inject.rb +20 -0
- metadata +27 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a481550ca7df8de031fb4d8c118fac213c93d5e604217a0012c33b8a9d6b38c7
|
4
|
+
data.tar.gz: 10a9a6b2edce626d410095bede6c239b8745d077389570669cd0594071f127fc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f05bce79f8219eebee3a527aa1cf2ac451c9bac2d5e273123fdf599e050a6ec7000b669c78513bd00124f6b09f8305436dfdbede296b0d906ff3a5e5438151ef
|
7
|
+
data.tar.gz: daf3016428d057e31f14e140823278eb5b9150a43e805def99669269435ab48b1e97f2479d3dfda3c4f11d883e3fe08e7d1949ebdaefe7f47d56e867a6d3d08d
|
data/lib/granite.rb
CHANGED
data/lib/granite/action.rb
CHANGED
@@ -7,6 +7,7 @@ require 'granite/action/types'
|
|
7
7
|
require 'granite/action/error'
|
8
8
|
require 'granite/action/performing'
|
9
9
|
require 'granite/action/performer'
|
10
|
+
require 'granite/action/precondition'
|
10
11
|
require 'granite/action/preconditions'
|
11
12
|
require 'granite/action/policies'
|
12
13
|
require 'granite/action/projectors'
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Granite
|
2
|
+
class Action
|
3
|
+
class Precondition < BasicObject
|
4
|
+
UNDEFINED = ::Object.new.freeze
|
5
|
+
|
6
|
+
def self.description(text = UNDEFINED)
|
7
|
+
case text
|
8
|
+
when UNDEFINED
|
9
|
+
@description
|
10
|
+
else
|
11
|
+
@description = text
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def initialize(context)
|
16
|
+
@context = context
|
17
|
+
end
|
18
|
+
|
19
|
+
def call(*)
|
20
|
+
fail NotImplementedError, "#call method must be implemented for #{self.class}"
|
21
|
+
end
|
22
|
+
|
23
|
+
def method_missing(method_name, *args, &blk)
|
24
|
+
super unless @context.respond_to?(method_name)
|
25
|
+
|
26
|
+
@context.__send__(method_name, *args, &blk)
|
27
|
+
end
|
28
|
+
|
29
|
+
def respond_to_missing?(method_name, _include_private = false)
|
30
|
+
@context.respond_to?(method_name)
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
attr_reader :context
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'granite/action/preconditions/base_precondition'
|
2
2
|
require 'granite/action/preconditions/embedded_precondition'
|
3
|
+
require 'granite/action/preconditions/object_precondition'
|
3
4
|
|
4
5
|
module Granite
|
5
6
|
class Action
|
@@ -13,6 +14,10 @@ module Granite
|
|
13
14
|
extend ActiveSupport::Concern
|
14
15
|
|
15
16
|
class PreconditionsCollection
|
17
|
+
include Enumerable
|
18
|
+
|
19
|
+
delegate :each, to: :@preconditions
|
20
|
+
|
16
21
|
def initialize(*preconditions)
|
17
22
|
@preconditions = preconditions.flatten
|
18
23
|
end
|
@@ -43,8 +48,10 @@ module Granite
|
|
43
48
|
options = args.extract_options!
|
44
49
|
if block
|
45
50
|
add_precondition(BasePrecondition, options, &block)
|
51
|
+
elsif args.first.is_a?(Class)
|
52
|
+
add_precondition(ObjectPrecondition, *args, options)
|
46
53
|
else
|
47
|
-
common_options = options.extract!(:if, :unless)
|
54
|
+
common_options = options.extract!(:if, :unless, :desc, :description)
|
48
55
|
args.each do |type|
|
49
56
|
precondition common_options.merge(type => {})
|
50
57
|
end
|
@@ -9,10 +9,7 @@ module Granite
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def execute!(context)
|
12
|
-
|
13
|
-
return if @options[:unless] && context.instance_exec(&@options[:unless])
|
14
|
-
|
15
|
-
_execute(context)
|
12
|
+
_execute(context) if context.conditions_satisfied?(@options)
|
16
13
|
end
|
17
14
|
|
18
15
|
private
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'granite/action/preconditions/base_precondition'
|
2
|
+
|
3
|
+
module Granite
|
4
|
+
class Action
|
5
|
+
module Preconditions
|
6
|
+
class ObjectPrecondition < BasePrecondition
|
7
|
+
private
|
8
|
+
|
9
|
+
def _execute(context)
|
10
|
+
@args.first.new(context).call(**@options.except(:if, :unless))
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/granite/base.rb
CHANGED
@@ -23,11 +23,11 @@ module Granite
|
|
23
23
|
controller_class.__send__(:define_method, name, &block)
|
24
24
|
class_eval <<-METHOD, __FILE__, __LINE__ + 1
|
25
25
|
def #{name}_url(options = {})
|
26
|
-
action_url(:#{name}, options)
|
26
|
+
action_url(:#{name}, options.symbolize_keys)
|
27
27
|
end
|
28
28
|
|
29
29
|
def #{name}_path(options = {})
|
30
|
-
action_path(:#{name}, options)
|
30
|
+
action_path(:#{name}, options.symbolize_keys)
|
31
31
|
end
|
32
32
|
METHOD
|
33
33
|
else
|
data/lib/granite/util.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
module Granite
|
2
|
+
module Util
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
# Evaluates value and returns result based on what was passed:
|
6
|
+
# - if Proc was passed, then executes it in context of self
|
7
|
+
# - if Symbol was passed, then calls a method with that name and returns result
|
8
|
+
# - otherwise just returns the value itself
|
9
|
+
# @param value [Object] value to evaluate
|
10
|
+
# @return [Object] result of evaluation
|
11
|
+
def evaluate(value)
|
12
|
+
case value
|
13
|
+
when Proc
|
14
|
+
evaluate_proc(value)
|
15
|
+
when Symbol
|
16
|
+
evaluate_symbol(value)
|
17
|
+
else
|
18
|
+
value
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# Evaluates `if` or `unless` conditions present in the supplied
|
23
|
+
# `options` being it a symbol or callable.
|
24
|
+
#
|
25
|
+
# @param [Hash] options The method options to evaluate.
|
26
|
+
# @option options :if method name or callable
|
27
|
+
# @option options :unless method name or callable
|
28
|
+
# @return [Boolean] whether conditions are satisfied
|
29
|
+
def conditions_satisfied?(options)
|
30
|
+
fail ArgumentError, 'You cannot specify both if and unless' if options.key?(:if) && options.key?(:unless)
|
31
|
+
|
32
|
+
if options.key?(:if)
|
33
|
+
evaluate(options[:if])
|
34
|
+
elsif options.key?(:unless)
|
35
|
+
!evaluate(options[:unless])
|
36
|
+
else
|
37
|
+
true
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def evaluate_proc(value)
|
44
|
+
instance_exec(&value)
|
45
|
+
end
|
46
|
+
|
47
|
+
def evaluate_symbol(value)
|
48
|
+
__send__(value)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/lib/granite/version.rb
CHANGED
@@ -0,0 +1,9 @@
|
|
1
|
+
module RuboCop
|
2
|
+
module Granite
|
3
|
+
PROJECT_ROOT = Pathname.new(__dir__).parent.parent.expand_path.freeze
|
4
|
+
CONFIG_DEFAULT = PROJECT_ROOT.join('config', 'rubocop-default.yml').freeze
|
5
|
+
CONFIG = YAML.safe_load(CONFIG_DEFAULT.read).freeze
|
6
|
+
|
7
|
+
private_constant(:CONFIG_DEFAULT, :PROJECT_ROOT)
|
8
|
+
end
|
9
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# The original code is from https://github.com/rubocop-hq/rubocop-rspec/blob/master/lib/rubocop/rspec/inject.rb
|
4
|
+
# See https://github.com/rubocop-hq/rubocop-rspec/blob/master/MIT-LICENSE.md
|
5
|
+
module RuboCop
|
6
|
+
module Granite
|
7
|
+
# Because RuboCop doesn't yet support plugins, we have to monkey patch in a
|
8
|
+
# bit of our configuration.
|
9
|
+
module Inject
|
10
|
+
def self.defaults!
|
11
|
+
path = CONFIG_DEFAULT.to_s
|
12
|
+
hash = ConfigLoader.load_file(path)
|
13
|
+
config = Config.new(hash, path).tap(&:make_excludes_absolute)
|
14
|
+
puts "configuration from #{path}" if ConfigLoader.debug?
|
15
|
+
config = ConfigLoader.merge_with_default(config, path)
|
16
|
+
ConfigLoader.instance_variable_set(:@default_configuration, config)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: granite
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Arkadiy Zabazhanov & friends
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-05-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionpack
|
@@ -244,28 +244,42 @@ dependencies:
|
|
244
244
|
requirements:
|
245
245
|
- - "~>"
|
246
246
|
- !ruby/object:Gem::Version
|
247
|
-
version:
|
247
|
+
version: 0.78.0
|
248
248
|
type: :development
|
249
249
|
prerelease: false
|
250
250
|
version_requirements: !ruby/object:Gem::Requirement
|
251
251
|
requirements:
|
252
252
|
- - "~>"
|
253
253
|
- !ruby/object:Gem::Version
|
254
|
-
version:
|
254
|
+
version: 0.78.0
|
255
|
+
- !ruby/object:Gem::Dependency
|
256
|
+
name: rubocop-rails
|
257
|
+
requirement: !ruby/object:Gem::Requirement
|
258
|
+
requirements:
|
259
|
+
- - "~>"
|
260
|
+
- !ruby/object:Gem::Version
|
261
|
+
version: 2.4.1
|
262
|
+
type: :development
|
263
|
+
prerelease: false
|
264
|
+
version_requirements: !ruby/object:Gem::Requirement
|
265
|
+
requirements:
|
266
|
+
- - "~>"
|
267
|
+
- !ruby/object:Gem::Version
|
268
|
+
version: 2.4.1
|
255
269
|
- !ruby/object:Gem::Dependency
|
256
270
|
name: rubocop-rspec
|
257
271
|
requirement: !ruby/object:Gem::Requirement
|
258
272
|
requirements:
|
259
273
|
- - "~>"
|
260
274
|
- !ruby/object:Gem::Version
|
261
|
-
version:
|
275
|
+
version: 1.37.0
|
262
276
|
type: :development
|
263
277
|
prerelease: false
|
264
278
|
version_requirements: !ruby/object:Gem::Requirement
|
265
279
|
requirements:
|
266
280
|
- - "~>"
|
267
281
|
- !ruby/object:Gem::Version
|
268
|
-
version:
|
282
|
+
version: 1.37.0
|
269
283
|
- !ruby/object:Gem::Dependency
|
270
284
|
name: simplecov
|
271
285
|
requirement: !ruby/object:Gem::Requirement
|
@@ -288,6 +302,7 @@ extra_rdoc_files: []
|
|
288
302
|
files:
|
289
303
|
- LICENSE
|
290
304
|
- app/controllers/granite/controller.rb
|
305
|
+
- config/rubocop-default.yml
|
291
306
|
- lib/generators/USAGE
|
292
307
|
- lib/generators/granite/install_controller_generator.rb
|
293
308
|
- lib/generators/granite_generator.rb
|
@@ -305,9 +320,11 @@ files:
|
|
305
320
|
- lib/granite/action/policies/always_allow_strategy.rb
|
306
321
|
- lib/granite/action/policies/any_strategy.rb
|
307
322
|
- lib/granite/action/policies/required_performer_strategy.rb
|
323
|
+
- lib/granite/action/precondition.rb
|
308
324
|
- lib/granite/action/preconditions.rb
|
309
325
|
- lib/granite/action/preconditions/base_precondition.rb
|
310
326
|
- lib/granite/action/preconditions/embedded_precondition.rb
|
327
|
+
- lib/granite/action/preconditions/object_precondition.rb
|
311
328
|
- lib/granite/action/projectors.rb
|
312
329
|
- lib/granite/action/subject.rb
|
313
330
|
- lib/granite/action/transaction.rb
|
@@ -348,7 +365,11 @@ files:
|
|
348
365
|
- lib/granite/rspec/satisfy_preconditions.rb
|
349
366
|
- lib/granite/translations.rb
|
350
367
|
- lib/granite/typecasters.rb
|
368
|
+
- lib/granite/util.rb
|
351
369
|
- lib/granite/version.rb
|
370
|
+
- lib/rubocop-granite.rb
|
371
|
+
- lib/rubocop/granite.rb
|
372
|
+
- lib/rubocop/granite/inject.rb
|
352
373
|
homepage: https://github.com/toptal/granite
|
353
374
|
licenses:
|
354
375
|
- MIT
|