Skip to content

Commit 084e221

Browse files
committed
Add standardrb and fix issues in embeded ruby file
- ruby 2.6: add ignore for Style/ArgumentsForwarding
1 parent ec7c5e4 commit 084e221

5 files changed

Lines changed: 55 additions & 26 deletions

File tree

.github/workflows/ruby.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Run Specs
2+
on: [push, pull_request]
3+
4+
jobs:
5+
test_embedded_ruby:
6+
strategy:
7+
matrix:
8+
os: [ubuntu-latest, macos-latest]
9+
ruby: [ '2.6', '2.7', '3.0', '3.1', '3.2', '3.3', '3.4', head]
10+
runs-on: ${{ matrix.os }}
11+
steps:
12+
- uses: actions/checkout@v6
13+
- uses: ruby/setup-ruby@v1
14+
with:
15+
ruby-version: ${{ matrix.ruby }}
16+
- run: bundle install
17+
working-directory: templatescompiler/erbrenderer/
18+
- run: bundle exec rake
19+
working-directory: templatescompiler/erbrenderer/
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Gemfile.lock
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
source "https://rubygems.org"
2+
3+
group :test do
4+
gem "rake"
5+
gem "standardrb"
6+
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require "standard/rake"
2+
3+
task default: [:standard]

templatescompiler/erbrenderer/erb_renderer.rb

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
class Hash
99
def recursive_merge!(other)
10-
self.merge!(other) do |_, old_value, new_value|
11-
if old_value.class == Hash && new_value.class == Hash
10+
merge!(other) do |_, old_value, new_value|
11+
if old_value.class == Hash && new_value.class == Hash # rubocop:disable Style/ClassEqualityComparison
1212
old_value.recursive_merge!(new_value)
1313
else
1414
new_value
@@ -27,14 +27,14 @@ def initialize(spec)
2727
@name = spec["job"]["name"] if spec["job"].is_a?(Hash)
2828
@index = spec["index"]
2929

30-
if !spec['job_properties'].nil?
31-
properties1 = spec['job_properties']
30+
properties1 = if !spec["job_properties"].nil?
31+
spec["job_properties"]
3232
else
33-
properties1 = spec['global_properties'].recursive_merge!(spec['cluster_properties'])
33+
spec["global_properties"].recursive_merge!(spec["cluster_properties"])
3434
end
3535

3636
properties = {}
37-
spec['default_properties'].each do |name, value|
37+
spec["default_properties"].each do |name, value|
3838
copy_property(properties, properties1, name, value)
3939
end
4040

@@ -66,7 +66,7 @@ def if_p(*names)
6666
value
6767
end
6868

69-
yield *values
69+
yield(*values)
7070
InactiveElseBlock.new
7171
end
7272

@@ -97,13 +97,15 @@ def copy_property(dst, src, name, default = nil)
9797

9898
def openstruct(object)
9999
case object
100-
when Hash
101-
mapped = object.inject({}) { |h, (k,v)| h[k] = openstruct(v); h }
102-
OpenStruct.new(mapped)
103-
when Array
104-
object.map { |item| openstruct(item) }
105-
else
106-
object
100+
when Hash
101+
mapped = object.each_with_object({}) { |(k, v), h|
102+
h[k] = openstruct(v)
103+
}
104+
OpenStruct.new(mapped)
105+
when Array
106+
object.map { |item| openstruct(item) }
107+
else
108+
object
107109
end
108110
end
109111

@@ -137,13 +139,14 @@ def else
137139
yield
138140
end
139141

140-
def else_if_p(*names, &block)
141-
@context.if_p(*names, &block)
142+
def else_if_p(*names, &block) # rubocop:disable Style/ArgumentsForwarding
143+
@context.if_p(*names, &block) # rubocop:disable Style/ArgumentsForwarding
142144
end
143145
end
144146

145147
class InactiveElseBlock
146-
def else; end
148+
def else
149+
end
147150

148151
def else_if_p(*names)
149152
InactiveElseBlock.new
@@ -153,7 +156,7 @@ def else_if_p(*names)
153156

154157
# todo do not use JSON in releases
155158
class << JSON
156-
alias dump_array_or_hash dump
159+
alias_method :dump_array_or_hash, :dump
157160

158161
def dump(*args)
159162
arg = args[0]
@@ -174,18 +177,15 @@ def render(src_path, dst_path)
174177
erb = ERB.new(File.read(src_path), trim_mode: "-")
175178
erb.filename = src_path
176179

177-
context_hash = JSON.load(File.read(@json_context_path))
180+
context_hash = JSON.load_file(@json_context_path)
178181
template_evaluation_context = TemplateEvaluationContext.new(context_hash)
179182

180-
File.open(dst_path, "w") do |f|
181-
f.write(erb.result(template_evaluation_context.get_binding))
182-
end
183-
184-
rescue Exception => e
183+
File.write(dst_path, erb.result(template_evaluation_context.get_binding))
184+
rescue Exception => e # rubocop:disable Lint/RescueException
185185
name = "#{template_evaluation_context&.name}/#{template_evaluation_context&.index}"
186186

187-
line_i = e.backtrace.index { |l| l.include?("#{erb&.filename}") }
188-
line_num = line_i ? e.backtrace[line_i].split(':')[1] : "unknown"
187+
line_i = e.backtrace.index { |l| l.include?(erb&.filename.to_s) }
188+
line_num = line_i ? e.backtrace[line_i].split(":")[1] : "unknown"
189189
location = "(line #{line_num}: #{e.inspect})"
190190

191191
raise("Error filling in template '#{src_path}' for #{name} #{location}")

0 commit comments

Comments
 (0)