centroid 0.1.0.beta

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 442b6a1b3ee53454896410af8438283629427be9
4
+ data.tar.gz: 43a912b02506d5c43ec2c1a0cb8ae21014645d3e
5
+ SHA512:
6
+ metadata.gz: f0c33f1d345b44f5923382210ead37c2e7c0d151dc48c839a002c0d6fe32e607c43cc7833d93c862ed3eacd58ec5135233057c7e425a95067efdfad4d87c0727
7
+ data.tar.gz: 45c336c21d6e93275cbac3704bc42f0f47e18b9814325d06f5c91888e8f80629af7785b8a224e7940f5c790cc4a18921df9efe2345e72e94f434aea60ba233bd
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/README.md ADDED
@@ -0,0 +1,91 @@
1
+ # Centroid - Ruby
2
+
3
+ ## Installation
4
+
5
+ The Centroid Ruby package is hosted at [rubygems.org](https://rubygems.org/gems/centroid).
6
+
7
+ You can install Centroid using gem with `gem install centroid`, or you can download, unpack, then `bundle install` and `rake install` from the `/ruby` directory.
8
+
9
+ ## Usage
10
+
11
+ Begin by declaring your application's configuration values in JSON. The following example stores the database's server address.
12
+
13
+ ```json
14
+ {
15
+ "database": {
16
+ "serverAddress": "my-server.local"
17
+ }
18
+ }
19
+ ```
20
+
21
+ With Centroid's API, Ruby applications can then use the configuration values in this JSON.
22
+
23
+ ### Ruby API
24
+
25
+ The Ruby API is available through the `Centroid::Config` class, an instance of which is used to consume the JSON.
26
+
27
+ The `Config` class exposes the `from_file` class method, an initializer, and the instance method `for_environment`.
28
+
29
+ #### from_file(filename)
30
+
31
+ You can create an instance of `Config` from a JSON file using the `Centroid::Config.from_file(filename)` class method.
32
+
33
+ In the example below, note the `serverAddress` configuration value is retrieved using the snake_case `server_address` method even though the value is specified as camelCase in the JSON.
34
+
35
+ ```rb
36
+ # from_file.rb
37
+ config = Centroid::Config.from_file("config.json")
38
+ server = config.database.server_address # => "my-server.local"
39
+ ```
40
+
41
+ ### Config(json)
42
+
43
+ Alternatively, you can create an instance of `Config` by passing a JSON string to the `Config` initializer.
44
+
45
+ ```rb
46
+ # from_string.rb
47
+ json = '{ "database": { "serverAddress": "my-server.local" } }'
48
+ config = Centroid::Config(json)
49
+ server = config.database.server_address # => "my-server.local"
50
+ ```
51
+
52
+ ### for_enviroment(environment)
53
+
54
+ Typically, real-world applications have different configuration values depending on the environment. For example, you might have *dev* and *prod* environments that use different servers, user accounts, etc. However, applications usually have other configuration values that are the same across all environments. Centroid makes it easy to retrieve all the configuration values you need for a specific environment.
55
+
56
+ In environment-based configuration, the top-level objects in the JSON represent the various environments. Place the configuration values that are the same across all environments within the *all* environment.
57
+
58
+ ```json
59
+ {
60
+ "dev": {
61
+ "someResource": {
62
+ "server": "resource-dev.local"
63
+ }
64
+ },
65
+ "prod": {
66
+ "someResource": {
67
+ "server": "resource-prod.local"
68
+ }
69
+ },
70
+ "all": {
71
+ "keys": {
72
+ "ssh": "path/to/id_rsa.pub"
73
+ }
74
+ }
75
+ }
76
+ ```
77
+
78
+ Then, in the `Config` instance, use the instance method `for_environment` to retrieve the environment-based configuration. Centroid merges the requested environment's configuration values with the *all* environment configuration values.
79
+
80
+ In the following example, the configuration for `prod` is merged with the configuration from `all`; the result is then available from a new instance of `Config`.
81
+
82
+ ```rb
83
+ # for_enviroment.rb
84
+ config = Centroid::Config.from_file("config.json").for_environment("prod")
85
+ server = config.some_resource.server # => "resource-prod.local"
86
+ solution_path = config.keys.ssh # => "path/to/id_rsa.pub"
87
+ ```
88
+
89
+ ## Contributing
90
+
91
+ See the [contributing section of the main README](../README.md#contributing)
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
data/centroid.gemspec ADDED
@@ -0,0 +1,11 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'centroid'
3
+ s.version = '0.1.0.beta'
4
+ s.summary = 'A centralizaed paradigm to configuration management.'
5
+ s.description = 'Centroid is a tool for loading configuration values declared in JSON, and accessing those configuration values using object properties.'
6
+ s.author = 'Resource Data, Inc'
7
+ s.email = 'oss@resdat.com'
8
+ s.files = `git ls-files`.split("\n")
9
+ s.homepage = 'https://github.com/ResourceDataInc/Centroid'
10
+ s.license = 'MIT'
11
+ end
data/lib/centroid.rb ADDED
@@ -0,0 +1,63 @@
1
+ require "json"
2
+
3
+ module Centroid
4
+ class Config
5
+ attr_reader :raw_config
6
+
7
+ def initialize(config)
8
+ if config.is_a?(Hash)
9
+ @raw_config = config
10
+ else
11
+ @raw_config = JSON.parse(config)
12
+ end
13
+ end
14
+
15
+ def method_missing(attrib, *)
16
+ self[attrib]
17
+ end
18
+
19
+ def [](key)
20
+ value = find_value(key, raw_config)
21
+
22
+ raise Exception.new("Key not found in collection.") if value.nil?
23
+
24
+ if value.is_a?(Hash)
25
+ Config.new(value)
26
+ else
27
+ value
28
+ end
29
+ end
30
+
31
+ def to_s
32
+ JSON.dump(raw_config)
33
+ end
34
+
35
+ def for_environment(env)
36
+ env_json = raw_config[env]
37
+ all_key = actual_key("all", raw_config)
38
+ if all_key.nil?
39
+ Config.new(env_json)
40
+ else
41
+ all_json = raw_config[all_key]
42
+ Config.new(all_json.merge(env_json))
43
+ end
44
+ end
45
+
46
+ def self.from_file(filename)
47
+ str_json = File.read(filename)
48
+ self.new(str_json)
49
+ end
50
+
51
+ def normalised_key(unnormalised_key)
52
+ unnormalised_key.to_s.gsub("_", "").downcase
53
+ end
54
+
55
+ def find_value(key, hashtable)
56
+ hashtable[actual_key(key, hashtable)]
57
+ end
58
+
59
+ def actual_key(key, hashtable)
60
+ hashtable.keys.find { |k| normalised_key(key) == normalised_key(k) }
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,71 @@
1
+ require_relative "../lib/centroid"
2
+ require "test/unit"
3
+ require "json"
4
+
5
+ class ConfigTests < Test::Unit::TestCase
6
+ def json_config
7
+ '{"Environment":{"TheKey":"TheValue"}}'
8
+ end
9
+
10
+ def shared_file_path
11
+ 'config.json'
12
+ end
13
+
14
+ def test_create_from_string
15
+ config = Centroid::Config.new(json_config)
16
+ assert_equal(config.environment.the_key, "TheValue")
17
+ end
18
+
19
+ def test_create_from_file
20
+ config = Centroid::Config.from_file(shared_file_path)
21
+ assert_equal(config.dev.database.server, "sqldev01.centroid.local")
22
+ end
23
+
24
+ def test_raises_if_key_not_found
25
+ config = Centroid::Config.new(json_config)
26
+ assert_raise Exception do
27
+ config = config.does_not_exist
28
+ end
29
+ end
30
+
31
+ def test_readable_using_snake_case_property
32
+ config = Centroid::Config.new(json_config)
33
+ assert_equal(config.environment.the_key, "TheValue")
34
+ end
35
+
36
+ def test_environment_specific_config_is_included
37
+ config = Centroid::Config.new(json_config)
38
+ environment_config = config.for_environment("Environment")
39
+ assert_equal(environment_config.the_key, "TheValue")
40
+ end
41
+
42
+ def test_shared_config_is_included
43
+ config = Centroid::Config.from_file(shared_file_path)
44
+ config = config.for_environment("Dev")
45
+ assert_equal(config.ci.repo, "https://github.com/ResourceDataInc/Centroid")
46
+ end
47
+
48
+ def test_to_string_returns_json
49
+ config = Centroid::Config.new(json_config)
50
+ assert_equal(config.to_s, json_config)
51
+ end
52
+
53
+ def test_iterating_raw_config
54
+ config = Centroid::Config.from_file(shared_file_path)
55
+ keyCount = 0
56
+ config.raw_config.each { |k| keyCount += 1 }
57
+ assert_equal(keyCount, 4)
58
+ end
59
+
60
+ def test_modifying_raw_config
61
+ config = Centroid::Config.new(json_config)
62
+ config.raw_config["Environment"]["TheKey"] = "NotTheValue"
63
+ assert_equal(config.environment.the_key, "NotTheValue")
64
+ end
65
+
66
+ def test_environment_specific_config_overrides_all
67
+ config = Centroid::Config.new('{"Prod": {"Shared": "production!"}, "All": {"Shared": "none"}}')
68
+ config = config.for_environment("Prod")
69
+ assert_equal(config.shared, "production!")
70
+ end
71
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: centroid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0.beta
5
+ platform: ruby
6
+ authors:
7
+ - Resource Data, Inc
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-19 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Centroid is a tool for loading configuration values declared in JSON,
14
+ and accessing those configuration values using object properties.
15
+ email: oss@resdat.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - Gemfile
21
+ - README.md
22
+ - Rakefile
23
+ - centroid.gemspec
24
+ - lib/centroid.rb
25
+ - test/centroid_test.rb
26
+ homepage: https://github.com/ResourceDataInc/Centroid
27
+ licenses:
28
+ - MIT
29
+ metadata: {}
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - '>'
42
+ - !ruby/object:Gem::Version
43
+ version: 1.3.1
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 2.2.2
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: A centralizaed paradigm to configuration management.
50
+ test_files: []