Skip to content

Commit

Permalink
Merge pull request #565 from DannyBen/refactor/variables
Browse files Browse the repository at this point in the history
Refactor `command.variables` to a new `Variable` object
  • Loading branch information
DannyBen authored Oct 23, 2024
2 parents b61600d + 8be4bc0 commit 1efd806
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 29 deletions.
5 changes: 4 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ Naming/AccessorMethodName:
- 'lib/bashly/concerns/indentation_helper.rb'

# FIXME: The `Command` class is too long
Metrics/ClassLength: { Exclude: [lib/bashly/script/command.rb] }
Metrics/ClassLength:
Exclude:
- lib/bashly/script/command.rb
- lib/bashly/config_validator.rb

# Allow irregular filenames in some cases
RSpec/SpecFilePathFormat:
Expand Down
4 changes: 2 additions & 2 deletions lib/bashly.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ module Bashly
module Script
autoloads 'bashly/script', %i[
Argument Base CatchAll Command Dependency EnvironmentVariable Flag
Wrapper
Variable Wrapper
]

module Introspection
autoloads 'bashly/script/introspection', %i[
Arguments Commands Dependencies EnvironmentVariables Examples Flags
Visibility
Variables Visibility
]
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/bashly/script/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Command < Base
include Introspection::EnvironmentVariables
include Introspection::Examples
include Introspection::Flags
include Introspection::Variables
include Introspection::Visibility

class << self
Expand Down
16 changes: 16 additions & 0 deletions lib/bashly/script/introspection/variables.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Bashly
module Script
module Introspection
module Variables
# Returns an array of Variable objects
def variables
return [] unless options['variables']

options['variables'].map do |options|
Variable.new options
end
end
end
end
end
end
11 changes: 11 additions & 0 deletions lib/bashly/script/variable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Bashly
module Script
class Variable < Base
class << self
def option_keys
@option_keys ||= %i[name value]
end
end
end
end
end
27 changes: 1 addition & 26 deletions lib/bashly/views/command/variables.gtx
Original file line number Diff line number Diff line change
@@ -1,30 +1,5 @@
= view_marker

variables.each do |var|
case var['value']
when Array
if var['value'].empty?
> declare -a {{ var['name'] }}=()
else
> declare -a {{ var['name'] }}=(
var['value'].each do |v|
> "{{ v }}"
end
> )
end
when Hash
if var['value'].empty?
> declare -A {{ var['name'] }}=()
else
> declare -A {{ var['name'] }}=(
var['value'].each do |k, v|
> ["{{ k }}"]="{{ v }}"
end
> )
end
when String, NilClass
> {{ var['name'] }}="{{ var['value'] }}"
else
> {{ var['name'] }}={{ var['value'] }}
end
= var.render(:definition)
end
28 changes: 28 additions & 0 deletions lib/bashly/views/variable/definition.gtx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
= view_marker

case value
when Array
if value.empty?
> declare -a {{ name }}=()
else
> declare -a {{ name }}=(
value.each do |v|
> "{{ v }}"
end
> )
end
when Hash
if value.empty?
> declare -A {{ name }}=()
else
> declare -A {{ name }}=(
value.each do |k, v|
> ["{{ k }}"]="{{ v }}"
end
> )
end
when String, NilClass
> {{ name }}="{{ value }}"
else
> {{ name }}={{ value }}
end
17 changes: 17 additions & 0 deletions spec/bashly/script/introspection/variables_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
describe Script::Introspection::Variables do
subject do
result = Script::Command.new fixtures[fixture]
result.parents = result.options['parents']
result
end

let(:fixtures) { load_fixture 'script/commands' }
let(:fixture) { :variables }

describe '#variables' do
it 'returns an array of Variable objects' do
expect(subject.variables).to be_an Array
expect(subject.variables).to all(be_a Script::Variable)
end
end
end
15 changes: 15 additions & 0 deletions spec/fixtures/script/commands.yml
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,21 @@
repeatable: true
unique: true

:variables:
name: get
variables:
- name: debug
value: true
- name: url
value: http://hello.com
- name: number
value: 123
- name: array
value: [one, two]
- name: hash
value: { one: 1, two: 2 }
- name: empty

:whitelist:
name: cli
args:
Expand Down

0 comments on commit 1efd806

Please sign in to comment.