From a6f8135a17412c837d64417bd2c27a732e396b29 Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Mon, 9 Apr 2018 18:08:14 -0400 Subject: [PATCH] Refactor Environment to not inherit from hash --- Gemfile.lock | 12 ++++++------ lib/psenv.rb | 4 ++-- lib/psenv/environment.rb | 16 +++++++++------- lib/psenv/version.rb | 2 +- spec/environment_spec.rb | 17 ++++------------- spec/psenv_spec.rb | 10 +++++----- 6 files changed, 27 insertions(+), 34 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 14d856d..8ddcf6d 100644 --- a/Gemfile.lock +++ b/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) diff --git a/lib/psenv.rb b/lib/psenv.rb index 5f639f8..c91dab9 100644 --- a/lib/psenv.rb +++ b/lib/psenv.rb @@ -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) diff --git a/lib/psenv/environment.rb b/lib/psenv/environment.rb index ad2e01c..79bf446 100644 --- a/lib/psenv/environment.rb +++ b/lib/psenv/environment.rb @@ -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 diff --git a/lib/psenv/version.rb b/lib/psenv/version.rb index 6596160..885eecb 100644 --- a/lib/psenv/version.rb +++ b/lib/psenv/version.rb @@ -1,3 +1,3 @@ module Psenv - VERSION = "0.1.0".freeze + VERSION = "0.2.0".freeze end diff --git a/spec/environment_spec.rb b/spec/environment_spec.rb index 2b29040..7c15f84 100644 --- a/spec/environment_spec.rb +++ b/spec/environment_spec.rb @@ -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 diff --git a/spec/psenv_spec.rb b/spec/psenv_spec.rb index 5e34705..6b30950 100644 --- a/spec/psenv_spec.rb +++ b/spec/psenv_spec.rb @@ -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