var_block 0.0.1 → 0.0.2
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/lib/var_block/getvar_handlers.rb +39 -0
- data/lib/var_block/globals.rb +23 -3
- data/lib/var_block/var_array.rb +2 -0
- data/lib/var_block/var_hash.rb +12 -3
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: afff54f8fbdfa78d3381656b74cf55b61f2ad445
|
4
|
+
data.tar.gz: 44b695d5b423ec1df2cd73baa42d0663fb0c37b2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d16746b95437c2442412ec55c08969933f7b28890b56ed07baccdceb79e37bbd494bef279a065b7c8a9e0e8d36a35ad3527ee41e5363f5d570be41da1eda4574
|
7
|
+
data.tar.gz: 05d5f2049ea6f75902825a17eca1a7f5d56e11d1e85bdd139c7257d53e5e930318ccebc8f477aefce83e523653309e7b3236428008ac7c57baa210d27948feb3
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module VarBlock
|
2
|
+
module GetvarHandlers
|
3
|
+
class << self
|
4
|
+
def handle_var_array(value, context)
|
5
|
+
merged_values = []
|
6
|
+
|
7
|
+
value.each do |v|
|
8
|
+
merged_values = merged_values + handle_proc(v, context)
|
9
|
+
end
|
10
|
+
|
11
|
+
merged_values
|
12
|
+
end
|
13
|
+
|
14
|
+
def handle_proc(value, context)
|
15
|
+
context.instance_exec &value
|
16
|
+
end
|
17
|
+
|
18
|
+
def handle_default(value, context)
|
19
|
+
value
|
20
|
+
end
|
21
|
+
|
22
|
+
def handle_options(value, context, options)
|
23
|
+
return_value = value
|
24
|
+
|
25
|
+
options.each do |option|
|
26
|
+
case option
|
27
|
+
when :truthy?
|
28
|
+
ArgumentError.new("value should be an Array, but is found to be a #{value.class}") unless value.is_a? Array
|
29
|
+
return_value = !return_value.any?{|v| !!!v }
|
30
|
+
else
|
31
|
+
raise ArgumentError.new("#{option} not supported!")
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
return_value
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/lib/var_block/globals.rb
CHANGED
@@ -2,17 +2,35 @@ module VarBlock
|
|
2
2
|
module Globals
|
3
3
|
def self.included(base)
|
4
4
|
# fail if there is already same-name methods to prevent breaking dependencies
|
5
|
+
# thanks to Jack, https://stackoverflow.com/questions/44156150/how-to-raise-error-when-including-a-module-that-already-has-same-name-methods
|
5
6
|
overrides = instance_methods.select { |method| base.instance_method(method).owner != self }
|
6
7
|
raise "#{self.name} overrides #{overrides.join(', ')}" if overrides.any?
|
7
8
|
|
8
9
|
base.extend self
|
9
10
|
end
|
10
11
|
|
11
|
-
def getvar(
|
12
|
-
|
12
|
+
def getvar(var_hash, index, *options)
|
13
|
+
raise ArgumentError.new('1st argument should be a VarHash object!') unless var_hash.is_a? VarHash
|
14
|
+
|
15
|
+
value = var_hash[index]
|
16
|
+
|
17
|
+
case value
|
18
|
+
when VarArray
|
19
|
+
return_value = VarBlock::GetvarHandlers.handle_var_array(value, self)
|
20
|
+
when Proc
|
21
|
+
return_value = VarBlock::GetvarHandlers.handle_proc(value, self)
|
22
|
+
else
|
23
|
+
return_value = VarBlock::GetvarHandlers.handle_default(value, self)
|
24
|
+
end
|
25
|
+
|
26
|
+
unless options.empty?
|
27
|
+
return_value = VarBlock::GetvarHandlers.handle_options(return_value, self, options)
|
28
|
+
end
|
29
|
+
|
30
|
+
return_value
|
13
31
|
end
|
14
32
|
|
15
|
-
def with(
|
33
|
+
def with(var_hash = nil, **variables)
|
16
34
|
var_hash = VarHash.new(var_hash: var_hash)
|
17
35
|
|
18
36
|
variables.each do |key, value|
|
@@ -22,4 +40,6 @@ module VarBlock
|
|
22
40
|
yield var_hash
|
23
41
|
end
|
24
42
|
end
|
43
|
+
|
44
|
+
private
|
25
45
|
end
|
data/lib/var_block/var_hash.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module VarBlock
|
2
2
|
class VarHash < Hash
|
3
|
-
include Globals
|
3
|
+
include VarBlock::Globals
|
4
4
|
|
5
5
|
def initialize(var_hash: nil)
|
6
6
|
if var_hash
|
@@ -10,8 +10,17 @@ module VarBlock
|
|
10
10
|
self
|
11
11
|
end
|
12
12
|
|
13
|
-
def with(variables)
|
14
|
-
super(
|
13
|
+
def with(variables = {})
|
14
|
+
super(self, variables)
|
15
|
+
end
|
16
|
+
|
17
|
+
def merge(variables)
|
18
|
+
variables.each do |key, value|
|
19
|
+
current_value = self[key]
|
20
|
+
|
21
|
+
self[key] = VarArray.new([current_value]) unless current_value.is_a? VarArray
|
22
|
+
self[key].push(value)
|
23
|
+
end
|
15
24
|
end
|
16
25
|
end
|
17
26
|
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.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jules Roman B. Polidario
|
@@ -18,7 +18,9 @@ extensions: []
|
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
20
|
- lib/var_block.rb
|
21
|
+
- lib/var_block/getvar_handlers.rb
|
21
22
|
- lib/var_block/globals.rb
|
23
|
+
- lib/var_block/var_array.rb
|
22
24
|
- lib/var_block/var_hash.rb
|
23
25
|
homepage: http://rubygems.org/gems/var_block
|
24
26
|
licenses:
|