granite 0.9.7 → 0.9.8

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
  SHA256:
3
- metadata.gz: 3b29f40ab453a5dccfe51b587a3bb8ac106441393110a34853aa7d6319a52dc8
4
- data.tar.gz: 7670225bab82074b188a5ec72222a3986da4da217f2268473f3881f5487fb3ed
3
+ metadata.gz: a481550ca7df8de031fb4d8c118fac213c93d5e604217a0012c33b8a9d6b38c7
4
+ data.tar.gz: 10a9a6b2edce626d410095bede6c239b8745d077389570669cd0594071f127fc
5
5
  SHA512:
6
- metadata.gz: 127fb08af9bb0a97162650884ed9f7b82793db3963fa236a35b9ed1c4cd859cf3a030743eac7bf25ac7845838b61da42bc588d3baad3aa5919e5a8ef495bdb14
7
- data.tar.gz: 5a9f6d3bd77cad622f2216706b46e413ed8263b960cd34eb5726c7cc3dc236910aa707acfa2e77df732cd15120be7774ff87261b3854bac8f65e1782096440a0
6
+ metadata.gz: f05bce79f8219eebee3a527aa1cf2ac451c9bac2d5e273123fdf599e050a6ec7000b669c78513bd00124f6b09f8305436dfdbede296b0d906ff3a5e5438151ef
7
+ data.tar.gz: daf3016428d057e31f14e140823278eb5b9150a43e805def99669269435ab48b1e97f2479d3dfda3c4f11d883e3fe08e7d1949ebdaefe7f47d56e867a6d3d08d
data/lib/granite.rb CHANGED
@@ -4,6 +4,7 @@ require 'action_controller'
4
4
  require 'granite/version'
5
5
  require 'granite/config'
6
6
  require 'granite/context'
7
+ require 'granite/util'
7
8
 
8
9
  module Granite
9
10
  def self.config
@@ -9,10 +9,7 @@ module Granite
9
9
  end
10
10
 
11
11
  def execute!(context)
12
- return if @options[:if] && !context.instance_exec(&@options[:if])
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
data/lib/granite/base.rb CHANGED
@@ -18,6 +18,7 @@ module Granite
18
18
  include ActiveData::Model::Primary
19
19
  include ActiveModel::Validations::Callbacks
20
20
 
21
+ include Granite::Util
21
22
  include Granite::Translations
22
23
  include Granite::Represents
23
24
  end
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Granite
2
- VERSION = '0.9.7'.freeze
2
+ VERSION = '0.9.8'.freeze
3
3
  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.7
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: 2021-01-22 00:00:00.000000000 Z
11
+ date: 2021-05-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
@@ -365,6 +365,7 @@ files:
365
365
  - lib/granite/rspec/satisfy_preconditions.rb
366
366
  - lib/granite/translations.rb
367
367
  - lib/granite/typecasters.rb
368
+ - lib/granite/util.rb
368
369
  - lib/granite/version.rb
369
370
  - lib/rubocop-granite.rb
370
371
  - lib/rubocop/granite.rb
@@ -388,7 +389,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
388
389
  - !ruby/object:Gem::Version
389
390
  version: '0'
390
391
  requirements: []
391
- rubygems_version: 3.0.9
392
+ rubygems_version: 3.0.6
392
393
  signing_key:
393
394
  specification_version: 4
394
395
  summary: Another business actions architecture for Rails apps