Skip to content
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: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,21 @@
* Deep merge support - [#56](https://github.com/ResourceDataInc/Centroid/pull/56)
* Add `__contains__` support - [#50](https://github.com/ResourceDataInc/Centroid/pull/50)

### Fixes

* Fix issue with enumerating over config values causing values to become non-Centroidy - [#61](https://github.com/ResourceDataInc/Centroid/pull/61)

## Ruby

### Features

* Deep merge support - [#56](https://github.com/ResourceDataInc/Centroid/pull/56)
* Add `has_key?` method - [#50](https://github.com/ResourceDataInc/Centroid/pull/50)

### Fixes

* Fix issue with enumerating over config values causing values to become non-Centroidy - [#61](https://github.com/ResourceDataInc/Centroid/pull/61)

# 1.0.0 - 2014/02/20

Initial public release.
14 changes: 13 additions & 1 deletion python/centroid.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,19 @@ def __getitem__(self, key):
return value

def __iter__(self):
return self.raw_config.__iter__()
if type(self.raw_config) is dict:
for key, value in self.raw_config.iteritems():
if type(value) is dict:
yield key, Config(value)
else:
yield key, value

else:
for value in self.raw_config:
if type(value) is dict:
yield Config(value)
else:
yield value

# to string
def __str__(self):
Expand Down
45 changes: 32 additions & 13 deletions python/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ class ConfigTest(unittest.TestCase):

@property
def _json_config(self):
return '{"Environment": {"TheKey": "TheValue"}}'
return '{"theEnvironment": {"theKey": "TheValue"}}'

@property
def _json_config_with_array(self):
return '{"Array": [{"Key": "Value1"}, {"Key": "Value2"}]}'
return '{"theArray": [{"theKey": "Value1"}, {"theKey": "Value2"}]}'

@property
def _shared_file_path(self):
return 'config.json'

def test_create_from_string(self):
config = Config(self._json_config)
self.assertEqual(config.Environment.TheKey, "TheValue")
self.assertEqual(config.the_environment.the_key, "TheValue")

def test_create_from_file(self):
config = Config.from_file(self._shared_file_path)
Expand All @@ -36,11 +36,11 @@ def test_raises_if_duplicate_normalized_keys_exist(self):

def test_readable_using_snake_case_property(self):
config = Config(self._json_config)
self.assertEqual(config.environment.the_key, "TheValue")
self.assertEqual(config.the_environment.the_key, "TheValue")

def test_environment_specific_config_is_included(self):
config = Config(self._json_config)
environment_config = config.for_environment("Environment")
environment_config = config.for_environment("theEnvironment")
self.assertEqual(environment_config.the_key, "TheValue")

def test_shared_config_is_included(self):
Expand All @@ -62,8 +62,8 @@ def test_iterating_raw_config(self):

def test_modifying_raw_config(self):
config = Config(self._json_config)
config.raw_config["Environment"]["TheKey"] = "NotTheValue"
self.assertEqual(config.environment.the_key, "NotTheValue")
config.raw_config["theEnvironment"]["theKey"] = "NotTheValue"
self.assertEqual(config.the_environment.the_key, "NotTheValue")

def test_environment_specific_config_overrides_all(self):
config = Config('{"Prod": {"Shared": "production!"}, "All": {"Shared": "none"}}')
Expand All @@ -72,23 +72,42 @@ def test_environment_specific_config_overrides_all(self):

def test_indexing_json_array(self):
config = Config(self._json_config_with_array)
self.assertEqual(config.array[0].key, "Value1")
self.assertEqual(config.array[1].key, "Value2")
self.assertEqual(config.the_array[0].the_key, "Value1")
self.assertEqual(config.the_array[1].the_key, "Value2")

def test_enumerating_json_array(self):
config = Config(self._json_config_with_array)
itemCount = 0;
for item in config.array:
itemCount = 0
for item in config.the_array:
itemCount += 1
self.assertEqual(itemCount, 2)

def test_enumerating_json_object(self):
config = Config(self._json_config)
itemCount = 0;
itemCount = 0
for item in config:
itemCount += 1
self.assertEqual(itemCount, 1)

def test_enumerated_json_object_values_are_still_shiny(self):
json = """
{
"connections": {
"firstConnection": {
"user": "firstUser",
"password":"secret"
},
"secondConnection": {
"user": "secondUser",
"password":"secret"
}
}
}"""

config = Config(json)
for k, v in config.connections:
self.assertEqual(v.password, "secret")

def test_all_environment_is_not_case_sensitive(self):
config = Config('{"Prod": {"Shared": "production!"}, "All": {"Shared": "none", "AllOnly": "works"}}')
config = config.for_environment("Prod")
Expand All @@ -106,5 +125,5 @@ def test_supports_deep_merge(self):

def test_has_key(self):
config = Config(self._json_config)
self.assertTrue("environment" in config)
self.assertTrue("the_environment" in config)
self.assertTrue("does_not_exist" not in config)
12 changes: 12 additions & 0 deletions ruby/lib/centroid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ def respond_to_missing?(method_name, include_all)
has_key?(method_name)
end

def each
return enum_for :each unless block_given?

raw_config.each do |key, value|
if value.is_a?(Hash)
yield key, Config.new(value)
else
yield key, value
end
end
end

def [](key)
raise KeyError.new("Centroid::Config instance does not contain key: #{key}") unless has_key?(key)

Expand Down
46 changes: 38 additions & 8 deletions ruby/test/centroid_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class ConfigTests < Test::Unit::TestCase
def json_config
'{"Environment":{"TheKey":"TheValue"}}'
'{"theEnvironment":{"theKey":"TheValue"}}'
end

def shared_file_path
Expand All @@ -13,7 +13,7 @@ def shared_file_path

def test_create_from_string
config = Centroid::Config.new(json_config)
assert_equal(config.environment.the_key, "TheValue")
assert_equal(config.the_environment.the_key, "TheValue")
end

def test_create_from_file
Expand Down Expand Up @@ -46,12 +46,12 @@ def test_raises_if_duplicate_normalized_keys_exist

def test_readable_using_snake_case_property
config = Centroid::Config.new(json_config)
assert_equal(config.environment.the_key, "TheValue")
assert_equal(config.the_environment.the_key, "TheValue")
end

def test_environment_specific_config_is_included
config = Centroid::Config.new(json_config)
environment_config = config.for_environment("Environment")
environment_config = config.for_environment("theEnvironment")
assert_equal(environment_config.the_key, "TheValue")
end

Expand All @@ -75,8 +75,8 @@ def test_iterating_raw_config

def test_modifying_raw_config
config = Centroid::Config.new(json_config)
config.raw_config["Environment"]["TheKey"] = "NotTheValue"
assert_equal(config.environment.the_key, "NotTheValue")
config.raw_config["theEnvironment"]["theKey"] = "NotTheValue"
assert_equal(config.the_environment.the_key, "NotTheValue")
end

def test_environment_specific_config_overrides_all
Expand Down Expand Up @@ -104,13 +104,43 @@ def test_supports_deep_merge

def test_has_key
config = Centroid::Config.new(json_config)
assert(config.has_key?("environment"))
assert(config.has_key?("the_environment"))
assert(!config.has_key?("does_not_exist"))
end

def test_respond_to
config = Centroid::Config.new(json_config)
assert(config.respond_to?(:environment))
assert(config.respond_to?(:the_environment))
assert(!config.respond_to?(:does_not_exist))
end

def test_enumerating_json_object
config = Centroid::Config.new(json_config)
itemCount = 0
config.each do |item|
itemCount += 1
end
assert_equal(itemCount, 1)
end

def test_enumerated_json_object_values_are_still_shiny
json = '
{
"connections": {
"firstConnection": {
"user": "firstUser",
"password":"secret"
},
"secondConnection": {
"user": "secondUser",
"password":"secret"
}
}
}'

config = Centroid::Config.new(json)
config.connections.each do |k, v|
assert_equal(v.password, "secret")
end
end
end