Skip to content

Commit

Permalink
Merge pull request #5 from palkan/feature/allow-extensions
Browse files Browse the repository at this point in the history
Make it possible to extend config
  • Loading branch information
palkan authored Oct 6, 2017
2 parents a486672 + 2c9c372 commit 2bc8ebf
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 11 deletions.
8 changes: 7 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ AllCops:
- 'vendor/**/*'
DisplayCopNames: true
StyleGuideCopsOnly: false
TargetRubyVersion: 2.3
TargetRubyVersion: 2.2

Style/Documentation:
Exclude:
Expand All @@ -36,3 +36,9 @@ Metrics/BlockLength:

Metrics/LineLength:
Max: 100

Bundler/OrderedGems:
Enabled: false

Style/MutableConstant:
Enabled: false
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ source 'https://rubygems.org'

# Specify your gem's dependencies in anyway_config.gemspec
gem 'sqlite3'
gem 'pry-byebug'
gemspec

local_gemfile = "#{File.dirname(__FILE__)}/Gemfile.local"
Expand Down
1 change: 0 additions & 1 deletion anyway_config.gemspec
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# coding: utf-8
# frozen_string_literal: true

lib = File.expand_path('../lib', __FILE__)
Expand Down
15 changes: 11 additions & 4 deletions lib/anyway/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@ class << self
attr_reader :defaults, :config_attributes

def attr_config(*args, **hargs)
@defaults = hargs.deep_dup
defaults.stringify_keys!
@config_attributes = args + defaults.keys
attr_accessor(*@config_attributes)
@defaults ||= {}
@config_attributes ||= []

new_defaults = hargs.deep_dup
new_defaults.stringify_keys!
defaults.merge! new_defaults

new_keys = (args + new_defaults.keys) - config_attributes
@config_attributes += new_keys
attr_accessor(*new_keys)
end

def config_name(val = nil)
Expand All @@ -44,6 +50,7 @@ def for(name)

def initialize(config_name = nil, do_load = true)
@config_name = config_name || self.class.config_name
raise ArgumentError, "Config name is missing" unless @config_name
load if do_load
end

Expand Down
2 changes: 1 addition & 1 deletion lib/anyway/ext/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def deep_merge!(other_hash)
end

def stringify_keys!
keys.each do |key|
keys.each do |key| # rubocop: disable Performance/HashEachMethods
value = delete(key)
value.stringify_keys! if value.is_a?(::Hash)
self[key.to_s] = value
Expand Down
39 changes: 36 additions & 3 deletions spec/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
let(:conf) { CoolConfig.new }
let(:test_conf) { Anyway::TestConfig.new }

describe "config with name" do
context "config with name" do
before(:each) do
ENV.delete_if { |var| var =~ /^(cool|anyway)_/i }
end
Expand Down Expand Up @@ -87,7 +87,7 @@
end
end

describe "config for name" do
context "config for name" do
before(:each) do
ENV.delete_if { |var| var =~ /^myapp_/i }
end
Expand All @@ -105,12 +105,45 @@
end
end

describe "config without defaults" do
context "config without defaults" do
let(:conf) { SmallConfig.new }

it "works" do
expect(conf.meta).to be_nil
expect(conf.data).to be_nil
end
end

context "when name is missing" do
let(:config) do
Class.new(described_class)
end

it "raises ArgumentError" do
expect { config.new }.to raise_error(ArgumentError)
end
end

context "extending config" do
let(:config) do
Class.new(described_class) do
config_name 'testo'
attr_config :test, debug: false
end
end

it "adds new params" do
old_config = config.new

expect(old_config.debug).to eq false
expect(old_config.test).to be_nil

config.attr_config new_param: 'a'

new_config = config.new
expect(new_config.debug).to eq false
expect(new_config.test).to be_nil
expect(new_config.new_param).to eq 'a'
end
end
end
2 changes: 1 addition & 1 deletion spec/config_spec_norails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@

let(:conf) { empty_config_class.new }

specify { expect(conf.config_name).to be_nil }
specify { expect { conf.config_name }.to raise_error(ArgumentError) }
end

context "loading from default path" do
Expand Down

0 comments on commit 2bc8ebf

Please sign in to comment.