Refactor Environment to not inherit from hash

This commit is contained in:
Andrew Tomaka 2018-04-09 18:08:14 -04:00
parent 2891c0d168
commit a6f8135a17
Signed by: atomaka
GPG key ID: 61209BF70A5B18BE
6 changed files with 27 additions and 34 deletions

View file

@ -1,10 +1,10 @@
PATH PATH
remote: . remote: .
specs: specs:
psenv (0.1.0) psenv (0.2.0)
aws-sdk-ssm (~> 1) aws-sdk-ssm (~> 1)
psenv-rails (0.1.0) psenv-rails (0.2.0)
psenv (= 0.1.0) psenv (= 0.2.0)
railties (>= 3.2, < 5.2) railties (>= 3.2, < 5.2)
GEM GEM
@ -31,8 +31,8 @@ GEM
addressable (2.5.2) addressable (2.5.2)
public_suffix (>= 2.0.2, < 4.0) public_suffix (>= 2.0.2, < 4.0)
ast (2.4.0) ast (2.4.0)
aws-partitions (1.72.0) aws-partitions (1.74.0)
aws-sdk-core (3.17.1) aws-sdk-core (3.18.0)
aws-partitions (~> 1.0) aws-partitions (~> 1.0)
aws-sigv4 (~> 1.0) aws-sigv4 (~> 1.0)
jmespath (~> 1.0) jmespath (~> 1.0)
@ -65,7 +65,7 @@ GEM
powerpack (0.1.1) powerpack (0.1.1)
public_suffix (3.0.2) public_suffix (3.0.2)
rack (2.0.4) rack (2.0.4)
rack-test (0.8.3) rack-test (1.0.0)
rack (>= 1.0, < 3) rack (>= 1.0, < 3)
rails-dom-testing (2.0.3) rails-dom-testing (2.0.3)
activesupport (>= 4.2.0) activesupport (>= 4.2.0)

View file

@ -9,12 +9,12 @@ module Psenv
def load(*paths) def load(*paths)
paths.unshift(ENV["PARAMETER_STORE_PATH"]) if ENV["PARAMETER_STORE_PATH"] paths.unshift(ENV["PARAMETER_STORE_PATH"]) if ENV["PARAMETER_STORE_PATH"]
Environment.create(*paths.map { |path| retrieve_variables(path) }).apply Environment.new(*paths.map { |path| retrieve_variables(path) }).apply
end end
def overload(*paths) def overload(*paths)
paths.unshift(ENV["PARAMETER_STORE_PATH"]) if ENV["PARAMETER_STORE_PATH"] paths.unshift(ENV["PARAMETER_STORE_PATH"]) if ENV["PARAMETER_STORE_PATH"]
Environment.create(*paths.map { |path| retrieve_variables(path) }).apply! Environment.new(*paths.map { |path| retrieve_variables(path) }).apply!
end end
def retrieve_variables(path) def retrieve_variables(path)

View file

@ -1,15 +1,17 @@
module Psenv module Psenv
class Environment < Hash class Environment
def initialize(*variables)
@variables = variables.reverse.reduce({}, :merge)
end
def apply def apply
each { |k, v| ENV.store(k.to_s, v) unless ENV.has_key?(k.to_s) } @variables.each do |k, v|
ENV.store(k.to_s, v) unless ENV.has_key?(k.to_s)
end
end end
def apply! def apply!
each { |k, v| ENV.store(k.to_s, v) } @variables.each { |k, v| ENV.store(k.to_s, v) }
end
def self.create(*variables)
Environment[variables.reverse.reduce({}, :merge)]
end end
end end
end end

View file

@ -1,3 +1,3 @@
module Psenv module Psenv
VERSION = "0.1.0".freeze VERSION = "0.2.0".freeze
end end

View file

@ -4,27 +4,18 @@ RSpec.describe Psenv::Environment do
let(:environment1) { { A: "1", B: "1" } } let(:environment1) { { A: "1", B: "1" } }
let(:environment2) { { B: "2", C: "2" } } let(:environment2) { { B: "2", C: "2" } }
context ".create" do context ".new" do
subject { Psenv::Environment.create(environment1, environment2) } subject { Psenv::Environment.new(environment1, environment2) }
it "returns an environment object" do it "returns an environment object" do
expect(subject).to be_kind_of(Psenv::Environment) expect(subject).to be_kind_of(Psenv::Environment)
end end
it "is also a hash" do
expect(subject).to be_kind_of(Hash)
end
it "creates the correct environment" do
expected = { A: "1", B: "1", C: "2" }
expect(subject).to eq(expected)
end
end end
context "#apply" do context "#apply" do
before(:each) do before(:each) do
ENV.store("A", "0") ENV.store("A", "0")
environment = Psenv::Environment.create(environment1, environment2) environment = Psenv::Environment.new(environment1, environment2)
environment.apply environment.apply
end end
@ -44,7 +35,7 @@ RSpec.describe Psenv::Environment do
context "#apply!" do context "#apply!" do
before(:each) do before(:each) do
ENV.store("A", "0") ENV.store("A", "0")
environment = Psenv::Environment.create(environment1, environment2) environment = Psenv::Environment.new(environment1, environment2)
environment.apply! environment.apply!
end end

View file

@ -19,7 +19,7 @@ RSpec.describe Psenv do
allow(retriever1).to receive(:call) { env_variables } allow(retriever1).to receive(:call) { env_variables }
allow(retriever2).to receive(:call) { arg_variables[0] } allow(retriever2).to receive(:call) { arg_variables[0] }
allow(retriever3).to receive(:call) { arg_variables[1] } allow(retriever3).to receive(:call) { arg_variables[1] }
allow(Psenv::Environment).to receive(:create) { environment } allow(Psenv::Environment).to receive(:new) { environment }
allow(environment).to receive(:apply) allow(environment).to receive(:apply)
allow(environment).to receive(:apply!) allow(environment).to receive(:apply!)
@ -43,7 +43,7 @@ RSpec.describe Psenv do
it "creates the environment with the correct variables" do it "creates the environment with the correct variables" do
expect(Psenv::Environment). expect(Psenv::Environment).
to have_received(:create).with(env_variables) to have_received(:new).with(env_variables)
end end
it "applies the environment" do it "applies the environment" do
@ -62,7 +62,7 @@ RSpec.describe Psenv do
it "creates the environment with the correct variables" do it "creates the environment with the correct variables" do
expect(Psenv::Environment). expect(Psenv::Environment).
to have_received(:create).with(*arg_variables) to have_received(:new).with(*arg_variables)
end end
it "apples the environment" do it "apples the environment" do
@ -84,7 +84,7 @@ RSpec.describe Psenv do
it "creates the environment with the correct variables" do it "creates the environment with the correct variables" do
expect(Psenv::Environment). expect(Psenv::Environment).
to have_received(:create).with(env_variables) to have_received(:new).with(env_variables)
end end
it "applies the environment" do it "applies the environment" do
@ -103,7 +103,7 @@ RSpec.describe Psenv do
it "creates the environment with the correct variables" do it "creates the environment with the correct variables" do
expect(Psenv::Environment). expect(Psenv::Environment).
to have_received(:create).with(*arg_variables) to have_received(:new).with(*arg_variables)
end end
it "apples the environment" do it "apples the environment" do