Refactor Environment to not inherit from hash
This commit is contained in:
parent
2891c0d168
commit
a6f8135a17
6 changed files with 27 additions and 34 deletions
12
Gemfile.lock
12
Gemfile.lock
|
@ -1,10 +1,10 @@
|
|||
PATH
|
||||
remote: .
|
||||
specs:
|
||||
psenv (0.1.0)
|
||||
psenv (0.2.0)
|
||||
aws-sdk-ssm (~> 1)
|
||||
psenv-rails (0.1.0)
|
||||
psenv (= 0.1.0)
|
||||
psenv-rails (0.2.0)
|
||||
psenv (= 0.2.0)
|
||||
railties (>= 3.2, < 5.2)
|
||||
|
||||
GEM
|
||||
|
@ -31,8 +31,8 @@ GEM
|
|||
addressable (2.5.2)
|
||||
public_suffix (>= 2.0.2, < 4.0)
|
||||
ast (2.4.0)
|
||||
aws-partitions (1.72.0)
|
||||
aws-sdk-core (3.17.1)
|
||||
aws-partitions (1.74.0)
|
||||
aws-sdk-core (3.18.0)
|
||||
aws-partitions (~> 1.0)
|
||||
aws-sigv4 (~> 1.0)
|
||||
jmespath (~> 1.0)
|
||||
|
@ -65,7 +65,7 @@ GEM
|
|||
powerpack (0.1.1)
|
||||
public_suffix (3.0.2)
|
||||
rack (2.0.4)
|
||||
rack-test (0.8.3)
|
||||
rack-test (1.0.0)
|
||||
rack (>= 1.0, < 3)
|
||||
rails-dom-testing (2.0.3)
|
||||
activesupport (>= 4.2.0)
|
||||
|
|
|
@ -9,12 +9,12 @@ module Psenv
|
|||
|
||||
def load(*paths)
|
||||
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
|
||||
|
||||
def overload(*paths)
|
||||
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
|
||||
|
||||
def retrieve_variables(path)
|
||||
|
|
|
@ -1,15 +1,17 @@
|
|||
module Psenv
|
||||
class Environment < Hash
|
||||
class Environment
|
||||
def initialize(*variables)
|
||||
@variables = variables.reverse.reduce({}, :merge)
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
def apply!
|
||||
each { |k, v| ENV.store(k.to_s, v) }
|
||||
end
|
||||
|
||||
def self.create(*variables)
|
||||
Environment[variables.reverse.reduce({}, :merge)]
|
||||
@variables.each { |k, v| ENV.store(k.to_s, v) }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
module Psenv
|
||||
VERSION = "0.1.0".freeze
|
||||
VERSION = "0.2.0".freeze
|
||||
end
|
||||
|
|
|
@ -4,27 +4,18 @@ RSpec.describe Psenv::Environment do
|
|||
let(:environment1) { { A: "1", B: "1" } }
|
||||
let(:environment2) { { B: "2", C: "2" } }
|
||||
|
||||
context ".create" do
|
||||
subject { Psenv::Environment.create(environment1, environment2) }
|
||||
context ".new" do
|
||||
subject { Psenv::Environment.new(environment1, environment2) }
|
||||
|
||||
it "returns an environment object" do
|
||||
expect(subject).to be_kind_of(Psenv::Environment)
|
||||
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
|
||||
|
||||
context "#apply" do
|
||||
before(:each) do
|
||||
ENV.store("A", "0")
|
||||
environment = Psenv::Environment.create(environment1, environment2)
|
||||
environment = Psenv::Environment.new(environment1, environment2)
|
||||
environment.apply
|
||||
end
|
||||
|
||||
|
@ -44,7 +35,7 @@ RSpec.describe Psenv::Environment do
|
|||
context "#apply!" do
|
||||
before(:each) do
|
||||
ENV.store("A", "0")
|
||||
environment = Psenv::Environment.create(environment1, environment2)
|
||||
environment = Psenv::Environment.new(environment1, environment2)
|
||||
environment.apply!
|
||||
end
|
||||
|
||||
|
|
|
@ -19,7 +19,7 @@ RSpec.describe Psenv do
|
|||
allow(retriever1).to receive(:call) { env_variables }
|
||||
allow(retriever2).to receive(:call) { arg_variables[0] }
|
||||
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!)
|
||||
|
||||
|
@ -43,7 +43,7 @@ RSpec.describe Psenv do
|
|||
|
||||
it "creates the environment with the correct variables" do
|
||||
expect(Psenv::Environment).
|
||||
to have_received(:create).with(env_variables)
|
||||
to have_received(:new).with(env_variables)
|
||||
end
|
||||
|
||||
it "applies the environment" do
|
||||
|
@ -62,7 +62,7 @@ RSpec.describe Psenv do
|
|||
|
||||
it "creates the environment with the correct variables" do
|
||||
expect(Psenv::Environment).
|
||||
to have_received(:create).with(*arg_variables)
|
||||
to have_received(:new).with(*arg_variables)
|
||||
end
|
||||
|
||||
it "apples the environment" do
|
||||
|
@ -84,7 +84,7 @@ RSpec.describe Psenv do
|
|||
|
||||
it "creates the environment with the correct variables" do
|
||||
expect(Psenv::Environment).
|
||||
to have_received(:create).with(env_variables)
|
||||
to have_received(:new).with(env_variables)
|
||||
end
|
||||
|
||||
it "applies the environment" do
|
||||
|
@ -103,7 +103,7 @@ RSpec.describe Psenv do
|
|||
|
||||
it "creates the environment with the correct variables" do
|
||||
expect(Psenv::Environment).
|
||||
to have_received(:create).with(*arg_variables)
|
||||
to have_received(:new).with(*arg_variables)
|
||||
end
|
||||
|
||||
it "apples the environment" do
|
||||
|
|
Loading…
Reference in a new issue