hydrochlorb 0.0.1
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 +7 -0
- data/lib/hydrochlorb/builder.rb +57 -0
- data/lib/hydrochlorb/serializer.rb +42 -0
- data/lib/hydrochlorb/version.rb +8 -0
- data/lib/hydrochlorb.rb +10 -0
- data/spec/hydrochlorb_spec.rb +181 -0
- data/spec/spec_helper.rb +1 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 947e3a2ebc93d0120010473a9d369d012ea41fec964977f6fff0265c339dedb3
|
4
|
+
data.tar.gz: 940ed1b23d7494a12eb03b4c7149f726c3d1ecdec2a1d314cf0e97722bc9e65f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6819460fc7aa8892c4ce7487dc0599d90aa77540f8da6e4ab701e1161ea28dfd317c2da22239a795ed795c94cf00ae1dd63e6cf4877fdd1727ee87569ec754d2
|
7
|
+
data.tar.gz: 225db29231e48ee7e355fdfee03163ab5a3e47e69ad4b64b18d970243701536257e93943f1986c73dff22cef2bb7f1f3b2f91e503e842d5226216fa23f6985a2
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
class Hydrochlorb::Builder
|
4
|
+
def initialize(options = {}, &block)
|
5
|
+
@attributes = {}
|
6
|
+
@current = @attributes
|
7
|
+
|
8
|
+
build(&block) if block_given?
|
9
|
+
end
|
10
|
+
|
11
|
+
def build(&block)
|
12
|
+
return unless block_given?
|
13
|
+
|
14
|
+
instance_eval(&block)
|
15
|
+
|
16
|
+
self
|
17
|
+
end
|
18
|
+
|
19
|
+
def method_missing(method, *args, &block)
|
20
|
+
add(method, *args, &block)
|
21
|
+
end
|
22
|
+
|
23
|
+
def add(*args, &block)
|
24
|
+
if block_given?
|
25
|
+
obj = {}
|
26
|
+
k = args.first.to_sym
|
27
|
+
|
28
|
+
@current[k] = [] unless @current[k].is_a? Array
|
29
|
+
@current[k] << obj
|
30
|
+
|
31
|
+
c = obj
|
32
|
+
args[1..-1].each do |k|
|
33
|
+
obj = {}
|
34
|
+
c[k.to_sym] = obj
|
35
|
+
c = obj
|
36
|
+
end
|
37
|
+
|
38
|
+
previous = @current
|
39
|
+
@current = obj
|
40
|
+
instance_eval(&block)
|
41
|
+
@current = previous
|
42
|
+
elsif args.length > 1
|
43
|
+
method = args.shift.to_sym
|
44
|
+
@current[method] = args.length == 1 ? args.first : args
|
45
|
+
else
|
46
|
+
raise ArgumentError, "One key and at least one of values are required: #{args.join(',')}"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def to_json
|
51
|
+
@attributes.to_json
|
52
|
+
end
|
53
|
+
|
54
|
+
def to_hcl(*args)
|
55
|
+
Hydrochlorb::Serializer.serialize(@attributes, *args)
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
class Hydrochlorb::Serializer
|
2
|
+
class << self
|
3
|
+
def serialize(obj, options = {})
|
4
|
+
indent = options[:indent]
|
5
|
+
indent = 2 unless indent.is_a? Integer and indent >= 0
|
6
|
+
|
7
|
+
dump(obj, nil, indent, 0)
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def dump(obj, key = nil, indent = 0, depth = 0)
|
13
|
+
prefix = ' ' * indent * depth
|
14
|
+
|
15
|
+
case obj
|
16
|
+
when Array
|
17
|
+
if obj.all? { |v| v.is_a? Hash }
|
18
|
+
obj.map do |v|
|
19
|
+
send(__method__, v, key, indent, depth)
|
20
|
+
end.compact.join("\n")
|
21
|
+
else
|
22
|
+
"#{prefix}#{key} = [" + obj.map { |v| send(__method__, v) }.compact.join(', ') + ']'
|
23
|
+
end
|
24
|
+
when Hash
|
25
|
+
compacted = key.to_s.empty? || (!obj.empty? && obj.values.all? { |v| v.is_a? Hash })
|
26
|
+
|
27
|
+
str = obj.map do |k, v|
|
28
|
+
k = k.to_s.strip
|
29
|
+
k = k.inspect unless k =~ /^\w+$/ and (key.to_s.empty? or not compacted)
|
30
|
+
|
31
|
+
a = send(__method__, v, (compacted ? "#{key} #{k}".strip : k), indent, (compacted ? depth : depth + 1))
|
32
|
+
end.compact.join("\n")
|
33
|
+
|
34
|
+
compacted ? str : "#{prefix}#{key} {\n" + (str.empty? ? '' : "#{str}\n" )+ "#{prefix}}"
|
35
|
+
when Numeric, TrueClass, FalseClass
|
36
|
+
(key ? "#{prefix}#{key} = " : '') + obj.inspect
|
37
|
+
else
|
38
|
+
(key ? "#{prefix}#{key} = " : '') + obj.to_s.inspect unless obj.nil?
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/lib/hydrochlorb.rb
ADDED
@@ -0,0 +1,181 @@
|
|
1
|
+
describe Hydrochlorb do
|
2
|
+
it 'can handle primitive types' do
|
3
|
+
hcl = Hydrochlorb.build do
|
4
|
+
it_is_integer 1
|
5
|
+
it_is_negtive_integer -1
|
6
|
+
it_is_float 1.23
|
7
|
+
it_is_negtive_float -1.23
|
8
|
+
it_is_string 'foo'
|
9
|
+
it_is_null nil
|
10
|
+
it_is_true true
|
11
|
+
it_is_false false
|
12
|
+
it_is_array [1, -2, 3.3, -4.4, 'bar', nil, true, false]
|
13
|
+
end.to_hcl
|
14
|
+
|
15
|
+
expect(hcl).to eq <<~EOF.chomp
|
16
|
+
it_is_integer = 1
|
17
|
+
it_is_negtive_integer = -1
|
18
|
+
it_is_float = 1.23
|
19
|
+
it_is_negtive_float = -1.23
|
20
|
+
it_is_string = "foo"
|
21
|
+
it_is_true = true
|
22
|
+
it_is_false = false
|
23
|
+
it_is_array = [1, -2, 3.3, -4.4, "bar", true, false]
|
24
|
+
EOF
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'can handle array object' do
|
28
|
+
hcl = Hydrochlorb.build do
|
29
|
+
it_is_array_object {
|
30
|
+
foo 'bar'
|
31
|
+
}
|
32
|
+
end.to_hcl
|
33
|
+
|
34
|
+
expect(hcl).to eq <<~EOF.chomp
|
35
|
+
it_is_array_object {
|
36
|
+
foo = "bar"
|
37
|
+
}
|
38
|
+
EOF
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'can handle empty array object' do
|
42
|
+
hcl = Hydrochlorb.build do
|
43
|
+
it_is_array_object {
|
44
|
+
}
|
45
|
+
end.to_hcl
|
46
|
+
|
47
|
+
expect(hcl).to eq <<~EOF.chomp
|
48
|
+
it_is_array_object {
|
49
|
+
}
|
50
|
+
EOF
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'can handle object' do
|
54
|
+
hcl = Hydrochlorb.build do
|
55
|
+
it_is_object 'obj' do
|
56
|
+
foo 'bar'
|
57
|
+
end
|
58
|
+
end.to_hcl
|
59
|
+
|
60
|
+
expect(hcl).to eq <<~EOF.chomp
|
61
|
+
it_is_object "obj" {
|
62
|
+
foo = "bar"
|
63
|
+
}
|
64
|
+
EOF
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'can handle empty object' do
|
68
|
+
hcl = Hydrochlorb.build do
|
69
|
+
it_is_object 'obj' do
|
70
|
+
end
|
71
|
+
end.to_hcl
|
72
|
+
|
73
|
+
expect(hcl).to eq <<~EOF.chomp
|
74
|
+
it_is_object "obj" {
|
75
|
+
}
|
76
|
+
EOF
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'can handle object with multiple keys' do
|
80
|
+
hcl = Hydrochlorb.build do
|
81
|
+
it_is_object 'o', 'b', 'j' do
|
82
|
+
foo 'bar'
|
83
|
+
end
|
84
|
+
end.to_hcl
|
85
|
+
|
86
|
+
expect(hcl).to eq <<~EOF.chomp
|
87
|
+
it_is_object "o" "b" "j" {
|
88
|
+
foo = "bar"
|
89
|
+
}
|
90
|
+
EOF
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'can handle nested data types' do
|
94
|
+
hcl = Hydrochlorb.build do
|
95
|
+
foo 'bar'
|
96
|
+
obj 'nested', 'obj' do
|
97
|
+
obj 'obj', 'in', 'obj' do
|
98
|
+
float 3.14
|
99
|
+
arr {
|
100
|
+
int 123
|
101
|
+
}
|
102
|
+
arr {
|
103
|
+
bool true
|
104
|
+
arr_in_arr {
|
105
|
+
bool false
|
106
|
+
}
|
107
|
+
}
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end.to_hcl
|
111
|
+
|
112
|
+
expect(hcl).to eq <<~EOF.chomp
|
113
|
+
foo = "bar"
|
114
|
+
obj "nested" "obj" {
|
115
|
+
obj "obj" "in" "obj" {
|
116
|
+
float = 3.14
|
117
|
+
arr {
|
118
|
+
int = 123
|
119
|
+
}
|
120
|
+
arr {
|
121
|
+
bool = true
|
122
|
+
arr_in_arr {
|
123
|
+
bool = false
|
124
|
+
}
|
125
|
+
}
|
126
|
+
}
|
127
|
+
}
|
128
|
+
EOF
|
129
|
+
end
|
130
|
+
|
131
|
+
it 'can handle multiple builds' do
|
132
|
+
builder = Hydrochlorb.build do
|
133
|
+
obj {
|
134
|
+
first 1
|
135
|
+
}
|
136
|
+
end
|
137
|
+
|
138
|
+
hcl = builder.build do
|
139
|
+
second 2
|
140
|
+
end.to_hcl
|
141
|
+
|
142
|
+
expect(hcl).to eq <<~EOF.chomp
|
143
|
+
obj {
|
144
|
+
first = 1
|
145
|
+
}
|
146
|
+
second = 2
|
147
|
+
EOF
|
148
|
+
end
|
149
|
+
|
150
|
+
it 'can serialize with different indent' do
|
151
|
+
hcl = Hydrochlorb.build do
|
152
|
+
foo 'bar'
|
153
|
+
obj 'nested', 'obj' do
|
154
|
+
obj 'obj', 'in', 'obj' do
|
155
|
+
float 3.14
|
156
|
+
arr {
|
157
|
+
int 123
|
158
|
+
}
|
159
|
+
arr {
|
160
|
+
bool true
|
161
|
+
}
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end.to_hcl(indent: 4)
|
165
|
+
|
166
|
+
expect(hcl).to eq <<~EOF.chomp
|
167
|
+
foo = "bar"
|
168
|
+
obj "nested" "obj" {
|
169
|
+
obj "obj" "in" "obj" {
|
170
|
+
float = 3.14
|
171
|
+
arr {
|
172
|
+
int = 123
|
173
|
+
}
|
174
|
+
arr {
|
175
|
+
bool = true
|
176
|
+
}
|
177
|
+
}
|
178
|
+
}
|
179
|
+
EOF
|
180
|
+
end
|
181
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'hydrochlorb'
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hydrochlorb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rianol Jou
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-08-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 2.0.2
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.0.2
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 12.3.3
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 12.3.3
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 3.8.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.8.0
|
55
|
+
description: " Provide a similar way to config HCL (HashiCorp Configuration Language)
|
56
|
+
in ruby. And featuring all programming features (variables, iterators, functions,
|
57
|
+
regexp, etc) in ruby.\n"
|
58
|
+
email:
|
59
|
+
- rianol.jou@gmail.com
|
60
|
+
executables: []
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files: []
|
63
|
+
files:
|
64
|
+
- lib/hydrochlorb.rb
|
65
|
+
- lib/hydrochlorb/builder.rb
|
66
|
+
- lib/hydrochlorb/serializer.rb
|
67
|
+
- lib/hydrochlorb/version.rb
|
68
|
+
- spec/hydrochlorb_spec.rb
|
69
|
+
- spec/spec_helper.rb
|
70
|
+
homepage: https://github.com/RiANOl/hydrochlorb
|
71
|
+
licenses:
|
72
|
+
- MIT
|
73
|
+
metadata: {}
|
74
|
+
post_install_message:
|
75
|
+
rdoc_options: []
|
76
|
+
require_paths:
|
77
|
+
- lib
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
requirements: []
|
89
|
+
rubygems_version: 3.0.3
|
90
|
+
signing_key:
|
91
|
+
specification_version: 4
|
92
|
+
summary: Provide a similar way to config HCL (HashiCorp Configuration Language) in
|
93
|
+
ruby.
|
94
|
+
test_files:
|
95
|
+
- spec/hydrochlorb_spec.rb
|
96
|
+
- spec/spec_helper.rb
|