var_block 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/var_block/getvar_handlers.rb +32 -27
- data/lib/var_block/globals.rb +8 -7
- data/lib/var_block/var_hash.rb +2 -0
- data/lib/var_block/version.rb +1 -1
- data/lib/var_block.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 670353916ff8b708f40b2e7eb3f9926fe2b732d9
|
4
|
+
data.tar.gz: 9bb7c2702a1a245f54250d7b2dccdb31a5b7aea6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8cd81d7830bf189729010967b12ff753efb15bacf5fa0d224da528a49312586eef2ca834c41ce26ac3b2288d7cf6046001e9f5f0a9fc777ddb024be7f9011b79
|
7
|
+
data.tar.gz: 6e0cdfbb063eb895632d8c3fc06d9f917bf98b98b5800efa6b0f2dd0f18ad9bb47e7c9d6a247c71cd963fa6d9f7cb7e6520af7c2dc29638870399fc65ee579ad
|
@@ -2,46 +2,51 @@ require 'var_block/support'
|
|
2
2
|
|
3
3
|
module VarBlock
|
4
4
|
module GetvarHandlers
|
5
|
+
|
6
|
+
SUPPORTED_OPTIONS = [:truthy?].freeze
|
7
|
+
|
5
8
|
class << self
|
6
9
|
include VarBlock::Support
|
7
10
|
|
8
|
-
def handle_var_array(value, context)
|
9
|
-
|
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? && options.include?(:truthy?)
|
14
|
+
is_truthy = true
|
15
|
+
|
16
|
+
value.each do |v|
|
17
|
+
if v.is_a? Proc
|
18
|
+
is_truthy = handle_proc(v, context)
|
19
|
+
else
|
20
|
+
is_truthy = handle_default(v)
|
21
|
+
end
|
22
|
+
break unless is_truthy
|
23
|
+
end
|
24
|
+
|
25
|
+
return is_truthy
|
26
|
+
|
27
|
+
# else, if no options, defaults to return as a wrapped Array
|
28
|
+
else
|
29
|
+
merged_values = []
|
10
30
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
31
|
+
value.each do |v|
|
32
|
+
if v.is_a? Proc
|
33
|
+
merged_values = merged_values + array_wrap(handle_proc(v, context))
|
34
|
+
else
|
35
|
+
merged_values = merged_values + array_wrap(handle_default(v))
|
36
|
+
end
|
16
37
|
end
|
17
|
-
end
|
18
38
|
|
19
|
-
|
39
|
+
return merged_values
|
40
|
+
end
|
20
41
|
end
|
21
42
|
|
22
43
|
def handle_proc(value, context)
|
23
|
-
context.instance_exec
|
44
|
+
context.instance_exec(&value)
|
24
45
|
end
|
25
46
|
|
26
|
-
def handle_default(value
|
47
|
+
def handle_default(value)
|
27
48
|
value
|
28
49
|
end
|
29
|
-
|
30
|
-
def handle_options(value, context, options)
|
31
|
-
return_value = value
|
32
|
-
|
33
|
-
options.each do |option|
|
34
|
-
case option
|
35
|
-
when :truthy?
|
36
|
-
ArgumentError.new("value should be an Array, but is found to be a #{value.class}") unless value.is_a? Array
|
37
|
-
return_value = !return_value.any?{|v| !!!v }
|
38
|
-
else
|
39
|
-
raise ArgumentError.new("#{option} not supported!")
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
return_value
|
44
|
-
end
|
45
50
|
end
|
46
51
|
end
|
47
52
|
end
|
data/lib/var_block/globals.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'var_block/getvar_handlers'
|
2
|
+
|
1
3
|
module VarBlock
|
2
4
|
module Globals
|
3
5
|
def self.included(base)
|
@@ -10,23 +12,22 @@ module VarBlock
|
|
10
12
|
end
|
11
13
|
|
12
14
|
def getvar(var_hash, index, *options)
|
13
|
-
|
15
|
+
unsupported_options = (options - VarBlock::GetvarHandlers::SUPPORTED_OPTIONS)
|
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
|
+
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
|
+
raise ArgumentError, "2nd argument :#{index} is not defined. Defined are #{var_hash.keys.map(&:inspect).join(', ')}" unless var_hash.keys.include?(index)
|
14
19
|
|
15
20
|
value = var_hash[index]
|
16
21
|
|
17
22
|
return_value = case value
|
18
23
|
when VarArray
|
19
|
-
VarBlock::GetvarHandlers.handle_var_array(value, self)
|
24
|
+
VarBlock::GetvarHandlers.handle_var_array(value, self, options)
|
20
25
|
when Proc
|
21
26
|
VarBlock::GetvarHandlers.handle_proc(value, self)
|
22
27
|
else
|
23
|
-
VarBlock::GetvarHandlers.handle_default(value
|
28
|
+
VarBlock::GetvarHandlers.handle_default(value)
|
24
29
|
end
|
25
30
|
|
26
|
-
unless options.empty?
|
27
|
-
return_value = VarBlock::GetvarHandlers.handle_options(return_value, self, options)
|
28
|
-
end
|
29
|
-
|
30
31
|
return_value
|
31
32
|
end
|
32
33
|
|
data/lib/var_block/var_hash.rb
CHANGED
data/lib/var_block/version.rb
CHANGED
data/lib/var_block.rb
CHANGED