Skip to content

Commit 61d07b9

Browse files
authored
Merge pull request #2 from DannyBen/refactor/rubocop
Refactor with rubocop
2 parents 6eb6333 + e29060f commit 61d07b9

File tree

9 files changed

+96
-57
lines changed

9 files changed

+96
-57
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
LC_ALL: en_US.UTF-8 # consistent sort order
1313

1414
strategy:
15-
matrix: { ruby: ['2.7', '3.0', '3.1'] }
15+
matrix: { ruby: ['2.7', '3.0', '3.1', head] }
1616

1717
steps:
1818
- name: Checkout code

.rubocop.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
require:
2+
- rubocop-rspec
3+
- rubocop-performance
4+
5+
inherit_gem:
6+
rentacop: rentacop.yml
7+
8+
AllCops:
9+
TargetRubyVersion: 2.7.0
10+
Exclude:
11+
- 'debug.rb'
12+
- 'dev/**/*'
13+
14+
# Allow methods that start with `get_` in some cases
15+
Naming/AccessorMethodName:
16+
Exclude:
17+
- 'spec/fixtures/**/*'
18+
19+
# Prefer `receive` over `have_received`spies
20+
RSpec/MessageSpies:
21+
EnforcedStyle: receive
22+
23+
# Allow multiple expectations per block
24+
RSpec/MultipleExpectations:
25+
Max: 3
26+
27+
# Allow calling `subject` explicitly
28+
RSpec/NamedSubject:
29+
EnforcedStyle: named_only
30+
31+
# Allow stubbing other `subject` methods
32+
RSpec/SubjectStub:
33+
Enabled: false
34+
35+
# Allow unnamed doubles
36+
RSpec/VerifiedDoubles:
37+
Enabled: false
38+

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
source "https://rubygems.org"
1+
source 'https://rubygems.org'
22

33
gem 'byebug'
44
gem 'lp'

gtx.gemspec

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1-
lib = File.expand_path('../lib', __FILE__)
1+
lib = File.expand_path('lib', __dir__)
22
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3-
require 'date'
43
require 'gtx/version'
54

65
Gem::Specification.new do |s|
76
s.name = 'gtx'
87
s.version = GTX::VERSION
9-
s.date = Date.today.to_s
10-
s.summary = "GTX Template Engine"
11-
s.description = "Create templates that transpile to ERB"
12-
s.authors = ["Danny Ben Shitrit"]
8+
s.summary = 'GTX Template Engine'
9+
s.description = 'Create templates that transpile to ERB'
10+
s.authors = ['Danny Ben Shitrit']
1311
s.email = '[email protected]'
1412
s.files = Dir['README.md', 'lib/**/*.*']
1513
s.homepage = 'https://github.com/dannyben/gtx'
1614
s.license = 'MIT'
17-
s.required_ruby_version = ">= 2.7.0"
15+
s.required_ruby_version = '>= 2.7.0'
1816

1917
s.metadata = {
20-
"bug_tracker_uri" => "https://github.com/DannyBen/gtx/issues",
21-
"changelog_uri" => "https://github.com/DannyBen/gtx/blob/master/CHANGELOG.md",
22-
"homepage_uri" => 'https://github.com/dannyben/gtx',
23-
"source_code_uri" => 'https://github.com/dannyben/gtx',
18+
'bug_tracker_uri' => 'https://github.com/DannyBen/gtx/issues',
19+
'changelog_uri' => 'https://github.com/DannyBen/gtx/blob/master/CHANGELOG.md',
20+
'homepage_uri' => 'https://github.com/dannyben/gtx',
21+
'source_code_uri' => 'https://github.com/dannyben/gtx',
22+
'rubygems_mfa_required' => 'true',
2423
}
2524
end

lib/gtx.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ def render_file(path, context: nil, filename: nil)
1818
attr_reader :template, :filename
1919

2020
def initialize(template, filename: nil)
21-
@template, @filename = template, filename
21+
@template = template
22+
@filename = filename
2223
end
2324

2425
def erb_source
@@ -34,7 +35,7 @@ def erb_source
3435
def erb
3536
ERB.new(erb_source, trim_mode: '-').tap { |a| a.filename = filename }
3637
end
37-
38+
3839
def parse(context = nil)
3940
context ||= self
4041
context = context.instance_eval { binding } unless context.is_a? Binding
@@ -48,5 +49,4 @@ def eval_vars(string)
4849
.gsub(/\\\}\\\}/, '}}')
4950
.gsub(/\\\{\\\{/, '{{')
5051
end
51-
5252
end

lib/gtx/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
class GTX
2-
VERSION = "0.1.0"
2+
VERSION = '0.1.0'
33
end

spec/fixtures/mock_context.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module MockContext
2+
class One
3+
def get_binding
4+
binding
5+
end
6+
end
7+
8+
class Two
9+
def report
10+
'success'
11+
end
12+
end
13+
end

spec/gtx/gtx_spec.rb

Lines changed: 27 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,86 +2,74 @@
22

33
describe GTX do
44
subject { described_class.new template }
5+
56
let(:context) { double user: 'admin' }
67
let(:template) { "> some output\nruby_code = 'yes'\n= ruby_code" }
7-
let(:example_path) { "examples/full.gtx" }
8+
let(:example_path) { 'examples/full.gtx' }
89

910
describe 'full example' do
1011
subject { described_class.load_file example_path }
1112

12-
it "generates the correct ERB source" do
13-
expect(subject.erb_source).to match_approval("examples/full.erb")
13+
it 'generates the correct ERB source' do
14+
expect(subject.erb_source).to match_approval('examples/full.erb')
1415
end
1516

16-
it "generates the correct output" do
17-
expect(subject.parse context).to match_approval("examples/full.txt")
17+
it 'generates the correct output' do
18+
expect(subject.parse context).to match_approval('examples/full.txt')
1819
end
1920
end
2021

2122
describe '#erb_source' do
22-
it "returns ERB code" do
23-
expect(subject.erb_source).to match_approval("gtx/erb_source")
23+
it 'returns ERB code' do
24+
expect(subject.erb_source).to match_approval('gtx/erb_source')
2425
end
2526
end
2627

2728
describe '#erb' do
28-
it "returns ERB object" do
29+
it 'returns ERB object' do
2930
expect(subject.erb).to be_an ERB
3031
end
3132
end
3233

3334
describe '#parse' do
34-
it "returns the parsed ERB output" do
35+
it 'returns the parsed ERB output' do
3536
expect(subject.parse context).to match_approval('gtx/parse')
3637
end
3738

38-
context "on error" do
39-
subject { described_class.load_file "spec/fixtures/error.gtx" }
39+
context 'when an error occurs' do
40+
subject { described_class.load_file 'spec/fixtures/error.gtx' }
4041

41-
it "registers the correct file and line number in the backtrace" do
42+
it 'registers the correct file and line number in the backtrace' do
4243
expect { subject.parse }.to raise_error(ZeroDivisionError) do |e|
43-
expect(e.backtrace.first).to include "spec/fixtures/error.gtx:18"
44+
expect(e.backtrace.first).to include 'spec/fixtures/error.gtx:18'
4445
end
4546
end
4647
end
4748

48-
context "when a Binding object is passed instead of a regular object" do
49-
let(:context) do
50-
module Context
51-
class One
52-
def get_binding; binding; end
53-
end
54-
55-
class Two
56-
def report; "success"; end
57-
end
58-
end
49+
context 'when a Binding object is passed instead of a regular object' do
50+
let(:context) { MockContext::One.new.get_binding }
51+
let(:template) { '= Two.new.report' }
5952

60-
Context::One.new.get_binding
61-
end
62-
63-
let(:template) { "= Two.new.report" }
64-
65-
it "uses the Binding object as is instead of re-binding it" do
53+
it 'uses the Binding object as is instead of re-binding it' do
6654
expect(subject.parse context).to match_approval('gtx/parse-binding')
6755
end
6856
end
6957
end
7058

71-
context "class methods" do
59+
describe 'class methods' do
7260
subject { described_class }
7361

74-
describe "::render" do
75-
it "returns a parsed result" do
62+
describe '::render' do
63+
it 'returns a parsed result' do
7664
expect(subject.render template, context: context).to match_approval('gtx/parse')
7765
end
7866
end
7967

80-
describe "::load_file" do
68+
describe '::load_file' do
8169
subject { described_class.load_file example_path }
8270

83-
it "returns a GTX object" do
84-
expect(subject).to be_a GTX
71+
it 'returns a GTX object' do
72+
expect(subject).to be_a described_class
8573
end
8674

8775
it "loads the file's content to the GTX object" do
@@ -93,15 +81,14 @@ def report; "success"; end
9381
end
9482
end
9583

96-
describe "::render_file" do
84+
describe '::render_file' do
9785
let(:gtx_double) { double :parse }
9886

99-
it "is a shortcut to ::load_file(path).parse context" do
100-
expect(subject).to receive(:load_file).with(example_path, filename: nil).and_return(gtx_double)
87+
it 'is a shortcut to ::load_file(path).parse context' do
88+
allow(subject).to receive(:load_file).with(example_path, filename: nil).and_return(gtx_double)
10189
expect(gtx_double).to receive(:parse).with(context)
10290
subject.render_file example_path, context: context
10391
end
10492
end
10593
end
106-
10794
end

spec/spec_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
ENV['COLUMNS'] = '80'
1414
ENV['LINES'] = '30'
1515

16+
require_relative 'fixtures/mock_context'
17+
1618
RSpec.configure do |c|
1719
c.strip_ansi_escape = true
1820
end

0 commit comments

Comments
 (0)