diff --git a/lib/producer/core.rb b/lib/producer/core.rb index 18f75ea..267d797 100644 --- a/lib/producer/core.rb +++ b/lib/producer/core.rb @@ -15,6 +15,7 @@ require 'producer/core/interpreter' require 'producer/core/recipe' require 'producer/core/recipe/dsl' require 'producer/core/remote' +require 'producer/core/remote/environment' require 'producer/core/task' require 'producer/core/task/dsl' require 'producer/core/version' diff --git a/lib/producer/core/remote/environment.rb b/lib/producer/core/remote/environment.rb new file mode 100644 index 0000000..f161ecb --- /dev/null +++ b/lib/producer/core/remote/environment.rb @@ -0,0 +1,27 @@ +module Producer + module Core + class Remote + class Environment + require 'forwardable' + + extend Forwardable + def_delegator :@variables, :has_key? + + def initialize(variables) + case 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 diff --git a/spec/producer/core/remote/environment_spec.rb b/spec/producer/core/remote/environment_spec.rb new file mode 100644 index 0000000..82b515a --- /dev/null +++ b/spec/producer/core/remote/environment_spec.rb @@ -0,0 +1,34 @@ +require 'spec_helper' + +module Producer::Core + describe Remote::Environment do + let(:variables) { { 'FOO' => 'bar', 'BAZ' => 'qux' } } + subject(:environment) { Remote::Environment.new(variables) } + + describe '#initialize' do + context 'when a hash is given' do + it 'assigns the key/value pairs' do + expect(environment.instance_eval { @variables }).to eq variables + end + end + + context 'when a string is given' do + subject(:environment) { Remote::Environment.new("FOO=bar\nBAZ=qux") } + + it 'assigns the key/value pairs' do + expect(environment.instance_eval { @variables }).to eq variables + end + end + end + + describe '#has_key?' do + let(:key) { 'SOME_KEY' } + + it 'forwards the message to @variables' do + expect(environment.instance_eval { @variables }) + .to receive(:has_key?).with(key) + environment.has_key? key + end + end + end +end