Skip to content

Commit 1efd806

Browse files
authored
Merge pull request #565 from DannyBen/refactor/variables
Refactor `command.variables` to a new `Variable` object
2 parents b61600d + 8be4bc0 commit 1efd806

File tree

9 files changed

+95
-29
lines changed

9 files changed

+95
-29
lines changed

.rubocop.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ Naming/AccessorMethodName:
2626
- 'lib/bashly/concerns/indentation_helper.rb'
2727

2828
# FIXME: The `Command` class is too long
29-
Metrics/ClassLength: { Exclude: [lib/bashly/script/command.rb] }
29+
Metrics/ClassLength:
30+
Exclude:
31+
- lib/bashly/script/command.rb
32+
- lib/bashly/config_validator.rb
3033

3134
# Allow irregular filenames in some cases
3235
RSpec/SpecFilePathFormat:

lib/bashly.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ module Bashly
2222
module Script
2323
autoloads 'bashly/script', %i[
2424
Argument Base CatchAll Command Dependency EnvironmentVariable Flag
25-
Wrapper
25+
Variable Wrapper
2626
]
2727

2828
module Introspection
2929
autoloads 'bashly/script/introspection', %i[
3030
Arguments Commands Dependencies EnvironmentVariables Examples Flags
31-
Visibility
31+
Variables Visibility
3232
]
3333
end
3434
end

lib/bashly/script/command.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class Command < Base
88
include Introspection::EnvironmentVariables
99
include Introspection::Examples
1010
include Introspection::Flags
11+
include Introspection::Variables
1112
include Introspection::Visibility
1213

1314
class << self
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module Bashly
2+
module Script
3+
module Introspection
4+
module Variables
5+
# Returns an array of Variable objects
6+
def variables
7+
return [] unless options['variables']
8+
9+
options['variables'].map do |options|
10+
Variable.new options
11+
end
12+
end
13+
end
14+
end
15+
end
16+
end

lib/bashly/script/variable.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module Bashly
2+
module Script
3+
class Variable < Base
4+
class << self
5+
def option_keys
6+
@option_keys ||= %i[name value]
7+
end
8+
end
9+
end
10+
end
11+
end
Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,5 @@
11
= view_marker
22

33
variables.each do |var|
4-
case var['value']
5-
when Array
6-
if var['value'].empty?
7-
> declare -a {{ var['name'] }}=()
8-
else
9-
> declare -a {{ var['name'] }}=(
10-
var['value'].each do |v|
11-
> "{{ v }}"
12-
end
13-
> )
14-
end
15-
when Hash
16-
if var['value'].empty?
17-
> declare -A {{ var['name'] }}=()
18-
else
19-
> declare -A {{ var['name'] }}=(
20-
var['value'].each do |k, v|
21-
> ["{{ k }}"]="{{ v }}"
22-
end
23-
> )
24-
end
25-
when String, NilClass
26-
> {{ var['name'] }}="{{ var['value'] }}"
27-
else
28-
> {{ var['name'] }}={{ var['value'] }}
29-
end
4+
= var.render(:definition)
305
end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
= view_marker
2+
3+
case value
4+
when Array
5+
if value.empty?
6+
> declare -a {{ name }}=()
7+
else
8+
> declare -a {{ name }}=(
9+
value.each do |v|
10+
> "{{ v }}"
11+
end
12+
> )
13+
end
14+
when Hash
15+
if value.empty?
16+
> declare -A {{ name }}=()
17+
else
18+
> declare -A {{ name }}=(
19+
value.each do |k, v|
20+
> ["{{ k }}"]="{{ v }}"
21+
end
22+
> )
23+
end
24+
when String, NilClass
25+
> {{ name }}="{{ value }}"
26+
else
27+
> {{ name }}={{ value }}
28+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
describe Script::Introspection::Variables do
2+
subject do
3+
result = Script::Command.new fixtures[fixture]
4+
result.parents = result.options['parents']
5+
result
6+
end
7+
8+
let(:fixtures) { load_fixture 'script/commands' }
9+
let(:fixture) { :variables }
10+
11+
describe '#variables' do
12+
it 'returns an array of Variable objects' do
13+
expect(subject.variables).to be_an Array
14+
expect(subject.variables).to all(be_a Script::Variable)
15+
end
16+
end
17+
end

spec/fixtures/script/commands.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,21 @@
489489
repeatable: true
490490
unique: true
491491

492+
:variables:
493+
name: get
494+
variables:
495+
- name: debug
496+
value: true
497+
- name: url
498+
value: http://hello.com
499+
- name: number
500+
value: 123
501+
- name: array
502+
value: [one, two]
503+
- name: hash
504+
value: { one: 1, two: 2 }
505+
- name: empty
506+
492507
:whitelist:
493508
name: cli
494509
args:

0 commit comments

Comments
 (0)