Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Refactor & optimize ENV accessors #7

Merged
merged 6 commits into from
Feb 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/nenv/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,21 @@ def _create_env_accessor(klass, meth, &block)

def _create_env_writer(klass, meth, &block)
env_name = nil
dumper = nil
klass.send(:define_method, meth) do |raw_value|
env_name ||= _namespaced_sanitize(meth)
ENV[env_name] = Dumper.new.dump(raw_value, &block)
dumper ||= Dumper.setup(&block)
ENV[env_name] = dumper.(raw_value)
end
end

def _create_env_reader(klass, meth, &block)
env_name = nil
loader = nil
klass.send(:define_method, meth) do
env_name ||= _namespaced_sanitize(meth)
Loader.new(meth).load(ENV[env_name], &block)
loader ||= Loader.setup(meth, &block)
loader.(ENV[env_name])
end
end

Expand Down
13 changes: 9 additions & 4 deletions lib/nenv/environment/dumper.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
module Nenv
class Environment
class Dumper
def dump(raw_value, &callback)
return callback.call(raw_value) if callback
raw_value.nil? ? nil : raw_value.to_s
module Dumper
require 'nenv/environment/dumper/default'

def self.setup(&callback)
if callback
callback
else
Default
end
end
end
end
Expand Down
9 changes: 9 additions & 0 deletions lib/nenv/environment/dumper/default.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Nenv
class Environment
module Dumper::Default
def self.call(raw_value)
raw_value.nil? ? nil : raw_value.to_s
end
end
end
end
31 changes: 11 additions & 20 deletions lib/nenv/environment/loader.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
module Nenv
class Environment
class Loader
def initialize(meth)
@bool = meth.to_s.end_with?('?')
end

def load(raw_value, &callback)
return callback.call(raw_value) if callback
@bool ? _to_bool(raw_value) : raw_value
end

private
module Loader
require 'nenv/environment/loader/predicate'
require 'nenv/environment/loader/default'

def _to_bool(raw_value)
case raw_value
when nil
nil
when ''
fail ArgumentError, "Can't convert empty string into Bool"
when '0', 'false', 'n', 'no', 'NO', 'FALSE'
false
def self.setup(meth, &callback)
if callback
callback
else
true
if meth.to_s.end_with? '?'
Predicate
else
Default
end
end
end
end
Expand Down
9 changes: 9 additions & 0 deletions lib/nenv/environment/loader/default.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Nenv
class Environment
module Loader::Default
def self.call(raw_value)
raw_value
end
end
end
end
18 changes: 18 additions & 0 deletions lib/nenv/environment/loader/predicate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Nenv
class Environment
module Loader::Predicate
def self.call(raw_value)
case raw_value
when nil
nil
when ''
fail ArgumentError, "Can't convert empty string into Bool"
when '0', 'false', 'n', 'no', 'NO', 'FALSE'
false
else
true
end
end
end
end
end
4 changes: 2 additions & 2 deletions spec/lib/nenv/environment/dumper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'nenv/environment/dumper'

RSpec.describe Nenv::Environment::Dumper do
subject { described_class.new.dump(value) }
subject { described_class.setup.(value) }

context "with \"abc\"" do
let(:value) { 'abc' }
Expand All @@ -22,7 +22,7 @@

context 'with a block' do
subject do
described_class.new.dump(value) { |data| YAML.dump(data) }
described_class.setup { |data| YAML.dump(data) }.(value)
end

context 'with a yaml string' do
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/nenv/environment/loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

RSpec.describe Nenv::Environment::Loader do
context 'with no block' do
subject { described_class.new(meth).load(value) }
subject { described_class.setup(meth).(value) }

context 'with a normal method' do
let(:meth) { :foo }
Expand Down Expand Up @@ -49,7 +49,7 @@

context 'with a block' do
subject do
described_class.new(:foo).load(value) { |data| YAML.load(data) }
described_class.setup(:foo) { |data| YAML.load(data) }.(value)
end
context 'with a yaml string' do
let(:value) { "--- foo\n...\n" }
Expand Down
Loading