Skip to content

Commit d8f490e

Browse files
committed
refactor validate methods into Introspection::Validate
1 parent ed4497d commit d8f490e

File tree

10 files changed

+112
-124
lines changed

10 files changed

+112
-124
lines changed

lib/bashly.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ module Script
2828
module Introspection
2929
autoloads 'bashly/script/introspection', %i[
3030
Arguments Commands Dependencies EnvironmentVariables Examples Flags
31-
Variables Visibility
31+
Validate Variables Visibility
3232
]
3333
end
3434
end

lib/bashly/script/argument.rb

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
module Bashly
44
module Script
55
class Argument < Base
6+
include Introspection::Validate
7+
68
class << self
79
def option_keys
810
@option_keys ||= %i[
@@ -28,15 +30,6 @@ def label
2830
def usage_string
2931
required ? label : "[#{label}]"
3032
end
31-
32-
def validate
33-
return [] unless options['validate']
34-
35-
result = options['validate']
36-
result.is_a?(Array) ? result : [result]
37-
end
38-
39-
def validate? = validate.any?
4033
end
4134
end
4235
end

lib/bashly/script/environment_variable.rb

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Bashly
22
module Script
33
class EnvironmentVariable < Base
44
include Introspection::Visibility
5+
include Introspection::Validate
56

67
class << self
78
def option_keys
@@ -16,15 +17,6 @@ def usage_string(extended: false)
1617
result << strings[:required] if required && extended
1718
result.join ' '
1819
end
19-
20-
def validate
21-
return [] unless options['validate']
22-
23-
result = options['validate']
24-
result.is_a?(Array) ? result : [result]
25-
end
26-
27-
def validate? = validate.any?
2820
end
2921
end
3022
end

lib/bashly/script/flag.rb

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module Script
55
class Flag < Base
66
include Completions::Flag
77
include Introspection::Visibility
8+
include Introspection::Validate
89

910
class << self
1011
def option_keys
@@ -46,15 +47,6 @@ def usage_string(extended: false)
4647
result << strings[:repeatable] if repeatable && extended
4748
result.join ' '
4849
end
49-
50-
def validate
51-
return [] unless options['validate']
52-
53-
result = options['validate']
54-
result.is_a?(Array) ? result : [result]
55-
end
56-
57-
def validate? = validate.any?
5850
end
5951
end
6052
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module Bashly
2+
module Script
3+
module Introspection
4+
module Validate
5+
# Returns an array of validations
6+
def validate
7+
return [] unless options['validate']
8+
9+
result = options['validate']
10+
result.is_a?(Array) ? result : [result]
11+
end
12+
13+
# Returns true if there are any validations defined
14+
def validate? = validate.any?
15+
end
16+
end
17+
end
18+
end

spec/bashly/script/argument_spec.rb

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66

77
let(:fixture) { :basic_argument }
88

9+
describe 'composition' do
10+
it 'includes the necessary modules' do
11+
modules = [Script::Introspection::Validate]
12+
modules.each do |mod|
13+
expect(described_class.ancestors).to include(mod)
14+
end
15+
end
16+
end
17+
918
describe '#default_string' do
1019
context 'when default is an array' do
1120
let(:fixture) { :default_array }
@@ -59,36 +68,4 @@
5968
end
6069
end
6170
end
62-
63-
describe '#validate' do
64-
context 'with a string value' do
65-
let(:fixture) { :validate_string }
66-
67-
it 'returns it as an array' do
68-
expect(subject.validate).to eq ['file_exists']
69-
end
70-
end
71-
72-
context 'with an array value' do
73-
let(:fixture) { :validate_array }
74-
75-
it 'returns it as is' do
76-
expect(subject.validate).to eq ['file_exists', 'file_is_writable']
77-
end
78-
end
79-
end
80-
81-
describe '#validate?' do
82-
it 'returns false' do
83-
expect(subject.validate?).to be false
84-
end
85-
86-
context 'when validations are defined' do
87-
let(:fixture) { :validate_string }
88-
89-
it 'returns true' do
90-
expect(subject.validate?).to be true
91-
end
92-
end
93-
end
9471
end

spec/bashly/script/command_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,25 @@
88
let(:fixtures) { load_fixture 'script/commands' }
99
let(:fixture) { :basic_command }
1010

11+
describe 'composition' do
12+
it 'includes the necessary modules' do
13+
modules = [
14+
Script::Introspection::Arguments,
15+
Script::Introspection::Commands,
16+
Script::Introspection::Dependencies,
17+
Script::Introspection::EnvironmentVariables,
18+
Script::Introspection::Examples,
19+
Script::Introspection::Flags,
20+
Script::Introspection::Variables,
21+
Script::Introspection::Visibility,
22+
Completions::Command
23+
]
24+
modules.each do |mod|
25+
expect(described_class.ancestors).to include(mod)
26+
end
27+
end
28+
end
29+
1130
describe '#action_name' do
1231
context 'when it is the root command' do
1332
it 'returns root' do

spec/bashly/script/environment_variable_spec.rb

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66

77
let(:fixture) { :basic_env_var }
88

9+
describe 'composition' do
10+
it 'includes the necessary modules' do
11+
modules = [Script::Introspection::Visibility, Script::Introspection::Validate]
12+
modules.each do |mod|
13+
expect(described_class.ancestors).to include(mod)
14+
end
15+
end
16+
end
17+
918
describe '#usage_string' do
1019
it 'returns a string suitable to be used as a usage pattern' do
1120
expect(subject.usage_string).to eq 'BUILD_DIR'
@@ -27,36 +36,4 @@
2736
end
2837
end
2938
end
30-
31-
describe '#validate' do
32-
context 'with a string value' do
33-
let(:fixture) { :validate_string }
34-
35-
it 'returns it as an array' do
36-
expect(subject.validate).to eq ['dir_exists']
37-
end
38-
end
39-
40-
context 'with an array value' do
41-
let(:fixture) { :validate_array }
42-
43-
it 'returns it as is' do
44-
expect(subject.validate).to eq ['dir_exists', 'dir_is_writable']
45-
end
46-
end
47-
end
48-
49-
describe '#validate?' do
50-
it 'returns false' do
51-
expect(subject.validate?).to be false
52-
end
53-
54-
context 'when validations are defined' do
55-
let(:fixture) { :validate_string }
56-
57-
it 'returns true' do
58-
expect(subject.validate?).to be true
59-
end
60-
end
61-
end
6239
end

spec/bashly/script/flag_spec.rb

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@
66

77
let(:fixture) { :basic_flag }
88

9+
describe 'composition' do
10+
it 'includes the necessary modules' do
11+
modules = [
12+
Script::Introspection::Visibility, Script::Introspection::Validate,
13+
Completions::Flag
14+
]
15+
modules.each do |mod|
16+
expect(described_class.ancestors).to include(mod)
17+
end
18+
end
19+
end
20+
921
describe '#aliases' do
1022
context 'with long and short options' do
1123
it 'returns an array of both long and short values' do
@@ -109,36 +121,4 @@
109121
end
110122
end
111123
end
112-
113-
describe '#validate' do
114-
context 'with a string value' do
115-
let(:fixture) { :validate_string }
116-
117-
it 'returns it as an array' do
118-
expect(subject.validate).to eq ['file_exists']
119-
end
120-
end
121-
122-
context 'with an array value' do
123-
let(:fixture) { :validate_array }
124-
125-
it 'returns it as is' do
126-
expect(subject.validate).to eq ['file_exists', 'file_is_writable']
127-
end
128-
end
129-
end
130-
131-
describe '#validate?' do
132-
it 'returns false' do
133-
expect(subject.validate?).to be false
134-
end
135-
136-
context 'when validations are defined' do
137-
let(:fixture) { :validate_string }
138-
139-
it 'returns true' do
140-
expect(subject.validate?).to be true
141-
end
142-
end
143-
end
144124
end
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
describe Script::Introspection::Validate do
2+
subject do
3+
Script::Flag.new fixtures[fixture]
4+
end
5+
6+
let(:fixtures) { load_fixture 'script/arguments' }
7+
let(:fixture) { :basic_argument }
8+
9+
describe '#validate' do
10+
context 'with a string value' do
11+
let(:fixture) { :validate_string }
12+
13+
it 'returns it as an array' do
14+
expect(subject.validate).to eq ['file_exists']
15+
end
16+
end
17+
18+
context 'with an array value' do
19+
let(:fixture) { :validate_array }
20+
21+
it 'returns it as is' do
22+
expect(subject.validate).to eq ['file_exists', 'file_is_writable']
23+
end
24+
end
25+
end
26+
27+
describe '#validate?' do
28+
it 'returns false' do
29+
expect(subject.validate?).to be false
30+
end
31+
32+
context 'when validations are defined' do
33+
let(:fixture) { :validate_string }
34+
35+
it 'returns true' do
36+
expect(subject.validate?).to be true
37+
end
38+
end
39+
end
40+
end

0 commit comments

Comments
 (0)