var_block 1.0.3 → 1.0.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c54f547c317f7e4e0e1b6250f1567d240fc27939
4
- data.tar.gz: 9e02a66cc863bb96b82db8aeb0cf6c0d86ae48dc
3
+ metadata.gz: 1558d1459d0a4c2e35d2310b99f8477f04b5b92d
4
+ data.tar.gz: bcc50d0ccb6d7d08f2569f428c2ba88e94c9f751
5
5
  SHA512:
6
- metadata.gz: 4667d1bfc1f5cdb2d18ac34e0907d4f494d25ae17fbb65c3a33dad0da430b8f108c2c473cab281372f81a640b27c8e9447c80bfe3c9b5567f57f9f3f017f5f03
7
- data.tar.gz: 41e1f9c0fd07c1c07f4b78997fa23725850f772c744a455266d40f18deb003f88c9da1e9746fad1a496e9ed0394d475c676f61da893e1fb190509954e7c56dfa
6
+ metadata.gz: 7dd24620dd90615fb567107d70507b8e15531a45f7b7acec06951e53a05fb287f424e8b8eec5c2a86076c69ba9f6fef28bb9740495f308f4f7d078aa9d2c6738
7
+ data.tar.gz: b677a48857503d2201933ca60f1a9224375e6bb1eb59970534162f6d47d70ca53f2d7e6686e7d50131523e2c64f8596601319b8a6e87d14a6342267bc899054b
@@ -3,81 +3,94 @@ require 'var_block/support'
3
3
  module VarBlock
4
4
  module GetvarHandlers
5
5
 
6
- SUPPORTED_OPTIONS = [:truthy?, :any?].freeze
6
+ OPTIONS = [:truthy?, :any?].freeze
7
7
 
8
- class << self
9
- include VarBlock::Support
10
-
11
- def handle_var_array(value, context, options)
12
- # if :truthy?, we need to check each item in the array, and return false immediately if at least one is found to be not "truthy", else return true
13
- if options.any?
14
- return handle_options(value, options)
15
-
16
- # else, if no options, defaults to return as a wrapped Array
17
- else
18
- merged_values = []
19
-
20
- value.each do |v|
21
- if v.is_a? Proc
22
- merged_values = merged_values + array_wrap(handle_proc(v, context))
23
- else
24
- merged_values = merged_values + array_wrap(handle_default(v))
25
- end
26
- end
27
-
28
- return merged_values
29
- end
8
+ module DefaultHandler
9
+ def self.handle(value, options = [])
10
+ supported_options = []
11
+ unsupported_options = options - supported_options
12
+ raise ArgumentError, "#{unsupported_options.map(&:inspect).join(', ')} option(s) are not supported on non-merged variables" if unsupported_options.any?
13
+ value
30
14
  end
15
+ end
31
16
 
32
- def handle_proc(value, context)
17
+ module ProcHandler
18
+ def self.handle(value, context, options = [])
19
+ supported_options = []
20
+ unsupported_options = options - supported_options
21
+ raise ArgumentError, "#{unsupported_options.map(&:inspect).join(', ')} option(s) are not supported on non-merged variables" if unsupported_options.any?
33
22
  context.instance_exec(&value)
34
23
  end
24
+ end
35
25
 
36
- def handle_default(value)
37
- value
38
- end
39
-
40
- private
26
+ module VarArrayHandler
27
+ class << self
28
+ include VarBlock::Support
41
29
 
42
- def handle_options(value, options)
43
- options.each do |option|
44
- case option
30
+ def handle(value, context, options)
31
+ if options.any?
32
+ return handle_options(value, options)
45
33
 
46
- when :truthy?
47
- return handle_option_truthy(value)
34
+ # else, if no options, defaults to return as a wrapped Array
35
+ else
36
+ merged_values = []
37
+
38
+ value.each do |v|
39
+ if v.is_a? Proc
40
+ merged_values = merged_values + array_wrap(ProcHandler.handle(v, context))
41
+ else
42
+ merged_values = merged_values + array_wrap(DefaultHandler.handle(v))
43
+ end
44
+ end
48
45
 
49
- when :any?
50
- return handle_option_any(value)
46
+ return merged_values
51
47
  end
52
48
  end
53
- end
54
49
 
55
- def handle_option_truthy(values)
56
- is_truthy = true
50
+ private
57
51
 
58
- values.each do |value|
59
- if value.is_a? Proc
60
- is_truthy = handle_proc(value, context)
61
- else
62
- is_truthy = handle_default(value)
52
+ def handle_options(value, options)
53
+ options.each do |option|
54
+ case option
55
+
56
+ when :truthy?
57
+ return handle_option_truthy(value)
58
+
59
+ when :any?
60
+ return handle_option_any(value)
61
+ end
63
62
  end
64
- break unless is_truthy
65
63
  end
66
64
 
67
- return is_truthy
68
- end
65
+ def handle_option_truthy(values)
66
+ is_truthy = true
69
67
 
70
- def handle_option_any(values)
71
- values.each do |value|
72
- if value.is_a? Proc
73
- is_truthy = handle_proc(value, context)
74
- else
75
- is_truthy = handle_default(value)
68
+ values.each do |value|
69
+ if value.is_a? Proc
70
+ evaluated_value = ProcHandler.handle(value, context)
71
+ else
72
+ evaluated_value = DefaultHandler.handle(value)
73
+ end
74
+ is_truthy = !!evaluated_value
75
+ break unless is_truthy
76
76
  end
77
- return true if is_truthy
77
+
78
+ return is_truthy
78
79
  end
79
80
 
80
- return false
81
+ def handle_option_any(values)
82
+ values.each do |value|
83
+ if value.is_a? Proc
84
+ evaluated_value = ProcHandler.handle(value, context)
85
+ else
86
+ evaluated_value = DefaultHandler.handle(value)
87
+ end
88
+ is_truthy = !!evaluated_value
89
+ return true if is_truthy
90
+ end
91
+
92
+ return false
93
+ end
81
94
  end
82
95
  end
83
96
  end
@@ -12,7 +12,7 @@ module VarBlock
12
12
  end
13
13
 
14
14
  def getvar(var_hash, index, *options)
15
- unsupported_options = (options - VarBlock::GetvarHandlers::SUPPORTED_OPTIONS)
15
+ unsupported_options = (options - VarBlock::GetvarHandlers::OPTIONS)
16
16
  raise ArgumentError, "3rd argument options Array only supports #{VarBlock::GetvarHandlers::SUPPORTED_OPTIONS}. Does not support #{unsupported_options.map(&:inspect).join(', ')}" if unsupported_options.any?
17
17
  raise ArgumentError, "1st argument should be a VarHash object, but is found to be a #{var_hash.class}" unless var_hash.is_a? VarHash
18
18
  raise ArgumentError, "2nd argument :#{index} is not defined. Defined are #{var_hash.keys.map(&:inspect).join(', ')}" unless var_hash.keys.include?(index)
@@ -21,11 +21,11 @@ module VarBlock
21
21
 
22
22
  return_value = case value
23
23
  when VarArray
24
- VarBlock::GetvarHandlers.handle_var_array(value, self, options)
24
+ VarBlock::GetvarHandlers::VarArrayHandler.handle(value, self, options)
25
25
  when Proc
26
- VarBlock::GetvarHandlers.handle_proc(value, self)
26
+ VarBlock::GetvarHandlers::ProcHandler.handle(value, self, options)
27
27
  else
28
- VarBlock::GetvarHandlers.handle_default(value)
28
+ VarBlock::GetvarHandlers::DefaultHandler.handle(value, options)
29
29
  end
30
30
 
31
31
  return_value
@@ -1,2 +1,3 @@
1
+ # VarArray objects are only created when using `merge` or `merge_with`. Do not initialise them manually.
1
2
  class VarBlock::VarArray < Array
2
3
  end
@@ -1,3 +1,3 @@
1
1
  module VarBlock
2
- VERSION = '1.0.3'.freeze
2
+ VERSION = '1.0.4'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: var_block
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jules Roman B. Polidario
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-27 00:00:00.000000000 Z
11
+ date: 2017-05-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec