Simplify Remote::Environment with factory method:
* Implement .new_from_string factory class method; * Rename private method #parse_from_string as class method .string_to_hash; * Remove argument kind detection logic in constructor.
This commit is contained in:
parent
4885483d75
commit
012c776880
@ -45,7 +45,7 @@ module Producer
|
|||||||
end
|
end
|
||||||
|
|
||||||
def environment
|
def environment
|
||||||
Environment.new(execute 'env')
|
Environment.new_from_string(execute 'env')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -2,24 +2,23 @@ module Producer
|
|||||||
module Core
|
module Core
|
||||||
class Remote
|
class Remote
|
||||||
class Environment
|
class Environment
|
||||||
|
class << self
|
||||||
|
def string_to_hash(str)
|
||||||
|
Hash[str.each_line.map { |l| l.chomp.split '=', 2 }]
|
||||||
|
end
|
||||||
|
|
||||||
|
def new_from_string(str)
|
||||||
|
new string_to_hash str
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
require 'forwardable'
|
require 'forwardable'
|
||||||
|
|
||||||
extend Forwardable
|
extend Forwardable
|
||||||
def_delegator :@variables, :has_key?
|
def_delegator :@variables, :has_key?
|
||||||
|
|
||||||
def initialize(variables)
|
def initialize(variables)
|
||||||
case variables
|
@variables = variables
|
||||||
when String
|
|
||||||
@variables = parse_from_string variables
|
|
||||||
else
|
|
||||||
@variables = variables
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def parse_from_string(str)
|
|
||||||
Hash[str.each_line.map { |l| l.chomp.split '=', 2 }]
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -7,19 +7,29 @@ module Producer::Core
|
|||||||
let(:argument) { variables }
|
let(:argument) { variables }
|
||||||
subject(:environment) { Remote::Environment.new(argument) }
|
subject(:environment) { Remote::Environment.new(argument) }
|
||||||
|
|
||||||
describe '#initialize' do
|
describe '.string_to_hash' do
|
||||||
context 'when a hash is given' do
|
it 'converts key=value pairs separated by new lines to a hash' do
|
||||||
it 'assigns the key/value pairs' do
|
expect(Remote::Environment.string_to_hash(string))
|
||||||
expect(environment.instance_eval { @variables }).to eq variables
|
.to eq variables
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '.new_from_string' do
|
||||||
|
it 'builds a new instance after converting from string' do
|
||||||
|
expect(Remote::Environment).to receive(:new).with(variables)
|
||||||
|
Remote::Environment.new_from_string(string)
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when a string is given' do
|
it 'returns the instance' do
|
||||||
let(:argument) { string }
|
environment = double 'environment'
|
||||||
|
allow(Remote::Environment).to receive(:new) { environment }
|
||||||
|
expect(Remote::Environment.new_from_string(string)).to be environment
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
it 'assigns the key/value pairs' do
|
describe '#initialize' do
|
||||||
expect(environment.instance_eval { @variables }).to eq variables
|
it 'assigns the key/value pairs' do
|
||||||
end
|
expect(environment.instance_eval { @variables }).to eq variables
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -128,13 +128,13 @@ module Producer::Core
|
|||||||
end
|
end
|
||||||
|
|
||||||
it 'builds a remote environment with the result of `env` command' do
|
it 'builds a remote environment with the result of `env` command' do
|
||||||
expect(Remote::Environment).to receive(:new).with(output)
|
expect(Remote::Environment).to receive(:new_from_string).with(output)
|
||||||
remote.environment
|
remote.environment
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns the environment' do
|
it 'returns the environment' do
|
||||||
environment = double 'environment'
|
environment = double 'environment'
|
||||||
allow(Remote::Environment).to receive(:new) { environment }
|
allow(Remote::Environment).to receive(:new_from_string) { environment }
|
||||||
expect(remote.environment).to be environment
|
expect(remote.environment).to be environment
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user