Class: Cleon::Services::CloneGuard
Overview
Constant Summary
Constants included
from ArGuards
ArGuards::GuardModel
Instance Method Summary
collapse
#config, #do_require, #initialize, #renderer, #write_file
Methods inherited from Service
call
Instance Method Details
#arguards_source ⇒ Object
36
37
38
|
# File 'lib/cleon/services/clone_guard.rb', line 36
def arguards_source
"lib/#{@home.base}/arguards.rb"
end
|
#arguards_spec ⇒ Object
40
41
42
|
# File 'lib/cleon/services/clone_guard.rb', line 40
def arguards_spec
"test/#{@home.base}/arguards_spec.rb"
end
|
#call ⇒ Object
add new arguard into arguards.rb at the bottom of ArGuards module
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/cleon/services/clone_guard.rb', line 10
def call
@log = []
source = File.read(arguards_source).lines
source.insert(-3, guard_content)
write_file(arguards_source, source.join)
write_file(arguards_spec, shared_spec) unless File.exist?(arguards_spec)
source = File.read(arguards_spec)
write_file(arguards_spec, source + spec_content)
@log
end
|
#guard_content ⇒ Object
26
27
28
29
30
31
32
33
34
|
# File 'lib/cleon/services/clone_guard.rb', line 26
def guard_content
<<~EOF.lines.map{|l| ' '*4 + l}
Guard#{@model.const} = #{@model.root_const}::ArGuard.new(
'#{@model.name.downcase}', 'must be #{@model.const}',
Proc.new {|v|
raise "provide spec for Guard#{@model.const} file: \#{__FILE__} line: \#{__LINE__}"})
EOF
end
|
#shared_spec ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/cleon/services/clone_guard.rb', line 57
def shared_spec
<<~EOF
require_relative '../spec_helper'
include #{@model.root_const}::ArGuards
module SharedGuardSpecs
extend Minitest::Spec::DSL
# spec must provided the following variables:
# let(:guard) { GuardName }
# let(:valid) { ["name", :name] }
# let(:wrong) { [nil, 1, Object.new]}
it 'must return value' do
valid.each{|v| assert_equal v, guard.(v)}
end
it 'must raise ArgumentError' do
wrong.each{|w| assert_raises(ArgumentError) { guard.(w) }}
end
end
EOF
end
|
#spec_content ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/cleon/services/clone_guard.rb', line 44
def spec_content
<<~EOF
describe Guard#{@model.const} do
include SharedGuardSpecs
let(:guard) { Guard#{@model.const} }
let(:valid) { [nil, -1, 0, 1, "", "str", Object.new] }
let(:wrong) { [nil, -1, 0, 1, "", "str", Object.new] }
end
EOF
end
|