Skip to content

Commit

Permalink
Add overwrite_arrays option
Browse files Browse the repository at this point in the history
This is an extraction from
#103 where @dtaniwaki had
added this option, but had done it prior to the deep_merge extraction.

This depends on danielsdeleo/deep_merge#21

Paired with @carbonin <https://github.com/carbonin> on this.
  • Loading branch information
Fryguy committed Apr 1, 2016
1 parent 813e504 commit 23ce4b8
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 7 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ You can customize `Config` only once, preferably during application initializati
Config.setup do |config|
config.const_name = 'Settings'
config.knockout_prefix = nil
config.overwrite_arrays = false
end
```

Expand All @@ -271,6 +272,8 @@ Inheritance customization (check [Deep Merge](https://github.com/danielsdeleo/de

* `knockout_prefix` - ability to remove elements of the array set in earlier loaded settings file. Default: `nil`

* `overwrite_arrays` - ability to replace an entire array set in earlier loaded settings file. Default: `false`

## Working with Heroku

Heroku uses ENV object to store sensitive settings which are like the local files described above. You cannot upload
Expand Down
3 changes: 2 additions & 1 deletion lib/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ module Config
@@use_env = false

# deep_merge options
mattr_accessor :knockout_prefix
mattr_accessor :knockout_prefix, :overwrite_arrays
@@knockout_prefix = nil
@@overwrite_arrays = false

def self.setup
yield self if @@_ran_once == false
Expand Down
13 changes: 10 additions & 3 deletions lib/config/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ def reload_env!
key.to_s.split('.').reverse.each do |element|
hash = {element => hash}
end
DeepMerge.deep_merge!(hash, conf, :preserve_unmergeables => false)
DeepMerge.deep_merge!(hash,
conf,
preserve_unmergeables: false,
overwrite_arrays: Config.overwrite_arrays)
end

merge!(conf[Config.const_name] || {})
Expand All @@ -57,7 +60,8 @@ def reload!
DeepMerge.deep_merge!(source_conf,
conf,
preserve_unmergeables: false,
knockout_prefix: Config.knockout_prefix)
knockout_prefix: Config.knockout_prefix,
overwrite_arrays: Config.overwrite_arrays)
end
end

Expand Down Expand Up @@ -101,7 +105,10 @@ def to_json(*args)

def merge!(hash)
current = to_hash
DeepMerge.deep_merge!(hash.dup, current)
DeepMerge.deep_merge!(hash.dup,
current,
preserve_unmergeables: false,
overwrite_arrays: Config.overwrite_arrays)
marshal_load(__convert(current).marshal_dump)
self
end
Expand Down
32 changes: 32 additions & 0 deletions spec/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -358,5 +358,37 @@
end
end
end

context 'using overwrite_arrays' do
context 'in configuration phase' do
it 'should be able to assign a different overwrite_arrays value' do
Config.reset
Config.overwrite_arrays = true

expect(Config.overwrite_arrays).to eq(true)
end

it 'should have the default overwrite_arrays value equal false' do
Config.reset

expect(Config.overwrite_arrays).to eq(false)
end
end

context 'overwriting' do
let(:config) do
Config.overwrite_arrays = true
Config.load_files(["#{fixture_path}/overwrite_arrays/config1.yml",
"#{fixture_path}/overwrite_arrays/config2.yml",
"#{fixture_path}/overwrite_arrays/config3.yml"])
end

it 'should remove elements from settings' do
expect(config.array1).to eq(['item4', 'item5', 'item6'])
expect(config.array2.inner).to eq(['item4', 'item5', 'item6'])
expect(config.array3).to eq([])
end
end
end
end
end
17 changes: 17 additions & 0 deletions spec/fixtures/overwrite_arrays/config1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
array1:
- item1
- item2
- item3
- item4

array2:
inner:
- item1
- item2
- item3
- item4

array3:
- item1
- item2
- item3
12 changes: 12 additions & 0 deletions spec/fixtures/overwrite_arrays/config2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
array1:
- item2
- item4
- item5

array2:
inner:
- item2
- item4
- item5

array3: []
10 changes: 10 additions & 0 deletions spec/fixtures/overwrite_arrays/config3.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
array1:
- item4
- item5
- item6

array2:
inner:
- item4
- item5
- item6
7 changes: 4 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@

# Extend Config module with ability to reset configuration to the default values
def self.reset
self.const_name = 'Settings'
self.use_env = false
self.knockout_prefix = nil
self.const_name = 'Settings'
self.use_env = false
self.knockout_prefix = nil
self.overwrite_arrays = false
end
end
end
Expand Down

0 comments on commit 23ce4b8

Please sign in to comment.