var_block 0.0.4 → 0.0.5
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 +7 -3
- data/lib/var_block/globals.rb +12 -14
- data/lib/var_block/support.rb +15 -0
- data/lib/var_block/var_hash.rb +18 -2
- data/lib/var_block/version.rb +1 -1
- metadata +16 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aa34f9f2b6748dc5e35d66e1ca06faf5936718b7
|
4
|
+
data.tar.gz: c33f79f5a249c36ead3d87105f5f23a4e7044516
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3a2faec471a4b069babadc3cbb666e476f7d506fbd385e773bfacb72ef7bb677129481dca25561a0c1ba30ef1ada705b0052095575f9510553387caf735d1e39
|
7
|
+
data.tar.gz: 2d895e33e052a8c03725e0cbfc2bc1f44edecd440ff20b87897882c76e67c324bc1d22bd5a5c943d622c63f544a1b8204beee95890d022f02f22815824feaacb
|
@@ -1,14 +1,18 @@
|
|
1
|
+
require 'var_block/support'
|
2
|
+
|
1
3
|
module VarBlock
|
2
4
|
module GetvarHandlers
|
3
5
|
class << self
|
6
|
+
include VarBlock::Support
|
7
|
+
|
4
8
|
def handle_var_array(value, context)
|
5
9
|
merged_values = []
|
6
10
|
|
7
11
|
value.each do |v|
|
8
12
|
if v.is_a? Proc
|
9
|
-
merged_values = merged_values + handle_proc(v, context)
|
13
|
+
merged_values = merged_values + array_wrap(handle_proc(v, context))
|
10
14
|
else
|
11
|
-
merged_values = merged_values + handle_default(v, context)
|
15
|
+
merged_values = merged_values + array_wrap(handle_default(v, context))
|
12
16
|
end
|
13
17
|
end
|
14
18
|
|
@@ -37,7 +41,7 @@ module VarBlock
|
|
37
41
|
end
|
38
42
|
|
39
43
|
return_value
|
40
|
-
end
|
44
|
+
end
|
41
45
|
end
|
42
46
|
end
|
43
47
|
end
|
data/lib/var_block/globals.rb
CHANGED
@@ -1,29 +1,27 @@
|
|
1
1
|
module VarBlock
|
2
2
|
module Globals
|
3
|
-
|
4
|
-
|
5
3
|
def self.included(base)
|
6
4
|
# fail if there is already same-name methods to prevent breaking dependencies
|
7
5
|
# thanks to Jack, https://stackoverflow.com/questions/44156150/how-to-raise-error-when-including-a-module-that-already-has-same-name-methods
|
8
|
-
overrides = instance_methods.
|
9
|
-
raise "#{
|
6
|
+
overrides = instance_methods.reject { |method| base.instance_method(method).owner == self }
|
7
|
+
raise "#{name} overrides #{overrides.join(', ')}" if overrides.any?
|
10
8
|
|
11
9
|
base.extend self
|
12
10
|
end
|
13
11
|
|
14
12
|
def getvar(var_hash, index, *options)
|
15
|
-
raise ArgumentError
|
13
|
+
raise ArgumentError, '1st argument should be a VarHash object!' unless var_hash.is_a? VarHash
|
16
14
|
|
17
15
|
value = var_hash[index]
|
18
16
|
|
19
|
-
case value
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
17
|
+
return_value = case value
|
18
|
+
when VarArray
|
19
|
+
VarBlock::GetvarHandlers.handle_var_array(value, self)
|
20
|
+
when Proc
|
21
|
+
VarBlock::GetvarHandlers.handle_proc(value, self)
|
22
|
+
else
|
23
|
+
VarBlock::GetvarHandlers.handle_default(value, self)
|
24
|
+
end
|
27
25
|
|
28
26
|
unless options.empty?
|
29
27
|
return_value = VarBlock::GetvarHandlers.handle_options(return_value, self, options)
|
@@ -42,4 +40,4 @@ module VarBlock
|
|
42
40
|
yield var_hash
|
43
41
|
end
|
44
42
|
end
|
45
|
-
end
|
43
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module VarBlock
|
2
|
+
module Support
|
3
|
+
# copied directly from Rails Array class
|
4
|
+
# https://apidock.com/rails/v4.2.7/Array/wrap/class
|
5
|
+
def array_wrap(object)
|
6
|
+
if object.nil?
|
7
|
+
[]
|
8
|
+
elsif object.respond_to?(:to_ary)
|
9
|
+
object.to_ary || [object]
|
10
|
+
else
|
11
|
+
[object]
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/var_block/var_hash.rb
CHANGED
@@ -1,6 +1,10 @@
|
|
1
|
+
require 'var_block/globals'
|
2
|
+
require 'var_block/support'
|
3
|
+
|
1
4
|
module VarBlock
|
2
5
|
class VarHash < Hash
|
3
6
|
include VarBlock::Globals
|
7
|
+
include VarBlock::Support
|
4
8
|
|
5
9
|
def initialize(var_hash: nil)
|
6
10
|
if var_hash
|
@@ -18,9 +22,21 @@ module VarBlock
|
|
18
22
|
variables.each do |key, value|
|
19
23
|
current_value = self[key]
|
20
24
|
|
21
|
-
|
22
|
-
self
|
25
|
+
# if variable already has a value, we need to wrap both values into a VarArray if not yet a VarArray
|
26
|
+
if self.has_key?(key)
|
27
|
+
self[key] = VarArray.new(array_wrap(current_value) + array_wrap(value)) unless current_value.is_a? VarArray
|
28
|
+
|
29
|
+
# else if new variable
|
30
|
+
else
|
31
|
+
self[key] = value
|
32
|
+
end
|
23
33
|
end
|
24
34
|
end
|
35
|
+
|
36
|
+
def merged_with(variables = {})
|
37
|
+
cloned_self = self.clone
|
38
|
+
cloned_self.merge(variables)
|
39
|
+
cloned_self.with() { yield(cloned_self) }
|
40
|
+
end
|
25
41
|
end
|
26
42
|
end
|
data/lib/var_block/version.rb
CHANGED
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.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jules Roman B. Polidario
|
@@ -30,6 +30,20 @@ dependencies:
|
|
30
30
|
- - ">="
|
31
31
|
- !ruby/object:Gem::Version
|
32
32
|
version: 3.2.0
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: rake
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
33
47
|
- !ruby/object:Gem::Dependency
|
34
48
|
name: byebug
|
35
49
|
requirement: !ruby/object:Gem::Requirement
|
@@ -55,6 +69,7 @@ files:
|
|
55
69
|
- lib/var_block.rb
|
56
70
|
- lib/var_block/getvar_handlers.rb
|
57
71
|
- lib/var_block/globals.rb
|
72
|
+
- lib/var_block/support.rb
|
58
73
|
- lib/var_block/var_array.rb
|
59
74
|
- lib/var_block/var_hash.rb
|
60
75
|
- lib/var_block/version.rb
|