Implement basic Remote::Environment class:

Represent a registry for remote environment variables, will follow an
API similar to Hash except the constructor. Currently only #has_key? is
implemented.
This commit is contained in:
Thibault Jouan 2013-08-18 20:02:53 +00:00
parent ed99c191e0
commit 513ba4eedb
3 changed files with 62 additions and 0 deletions

View File

@ -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'

View File

@ -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

View File

@ -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