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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ec18c7b8c88fd11fd894afb5dba8de1a30652dd7
4
- data.tar.gz: 664f35c4cc983eb19392e8296bb23c286dc2068f
3
+ metadata.gz: 670353916ff8b708f40b2e7eb3f9926fe2b732d9
4
+ data.tar.gz: 9bb7c2702a1a245f54250d7b2dccdb31a5b7aea6
5
5
  SHA512:
6
- metadata.gz: 9ceb5af0b5a1a1de6b6fa9ff8c5e35d48f83892f79e89c1c5abb7703531798e1b576fe774a8b8684f7abd1912f1a738b912bbf4e92ad9906d9d27183b4d4a14b
7
- data.tar.gz: 6258fc4ecd37a473f79f89d8c874911b3b9577565bba50450382f7dffd11dd96513520e10fbc67e240991683d0312a21e3df14990aefe844d7d98785ddf7c125
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
- merged_values = []
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
- value.each do |v|
12
- if v.is_a? Proc
13
- merged_values = merged_values + array_wrap(handle_proc(v, context))
14
- else
15
- merged_values = merged_values + array_wrap(handle_default(v, context))
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
- merged_values
39
+ return merged_values
40
+ end
20
41
  end
21
42
 
22
43
  def handle_proc(value, context)
23
- context.instance_exec &value
44
+ context.instance_exec(&value)
24
45
  end
25
46
 
26
- def handle_default(value, context)
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
@@ -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
- raise ArgumentError, '1st argument should be a VarHash object!' unless var_hash.is_a? VarHash
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, self)
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
 
@@ -19,6 +19,8 @@ module VarBlock
19
19
  end
20
20
 
21
21
  def merge(variables)
22
+ raise ArgumentError, '`merge` does not accept a block. Are you looking for `merged_with` instead?' if block_given?
23
+
22
24
  variables.each do |key, value|
23
25
  current_value = self[key]
24
26
 
@@ -1,3 +1,3 @@
1
1
  module VarBlock
2
- VERSION = '0.0.7'.freeze
2
+ VERSION = '0.0.8'.freeze
3
3
  end
data/lib/var_block.rb CHANGED
@@ -1,4 +1,4 @@
1
- files = Dir[__dir__ + '/var_block/*.rb'].each {|file| require file }
1
+ Dir[__dir__ + '/var_block/*.rb'].each {|file| require file }
2
2
 
3
3
  module VarBlock
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: var_block
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jules Roman B. Polidario