centroid 1.0.0 → 1.1.0.pre.alpha1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f35b492a4ea8767187a77faae2f6b6bd5d5a66b6
4
- data.tar.gz: 9fe53378ae98e8b4175dba7fe6af296e7fd9ad0d
3
+ metadata.gz: 21355e7ed1634fc2fd8998d212c18bed7b14862a
4
+ data.tar.gz: c7a3f4b2e51a39f8f477de85f979d25afc2a4362
5
5
  SHA512:
6
- metadata.gz: 7585bc76ed87101a41c3b46d7febd24f9fdefb4df32ed7bba4b5af60f15d6339f4a90d015d8a14a5aaa32b1d717a94af5acbc1a6ddd8410634fb6a07eaf87de0
7
- data.tar.gz: fc57f08f83b93363132a68c6f866c9f5723834fd400c690d11a68776d826f5250f266eadc9a871e432f1f5fb5a92f3aed8e5ad91143113e054c4c86b7dcfdfda
6
+ metadata.gz: 9fb748e78951b7480633d6be39263e97040f4f7a0d5dbb72e14ac823bdeb68cd09cf3b615961d296d85bf5f45f6566b18fcc515a662b71d66453503dab343c44
7
+ data.tar.gz: 25bf6604d42caaa270a8fbf6cadbf6c4655937bc559dab9f0eaf2ca210432db6cee735ba1d091f3156083bb54f897f21d2a4c369f6353740d58427be5c3b26a2
data/README.md CHANGED
@@ -1,36 +1,25 @@
1
1
  # Centroid - Ruby
2
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 clone the repo, then `gem build centroid.gemspec`, and `gem install -l centroid` from the `ruby/` directory.
8
-
9
- ## Usage
3
+ This document includes information specific to Ruby in Centroid. Refer to the [Centroid document] (../README.md) for general information, including information the JSON configuration file.
10
4
 
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
- ```
5
+ ## Installation
20
6
 
21
- With Centroid's API, Ruby applications can then use the configuration values in this JSON.
7
+ The Centroid Ruby package is hosted at [rubygems.org](https://rubygems.org/gems/centroid). Install Centroid using gem with `gem install centroid` or clone the repo and then `gem build centroid.gemspec` and `gem install -l centroid` from the `ruby/` directory.
22
8
 
23
- ### Ruby API
9
+ ## Config Class with Ruby
24
10
 
25
- The Ruby API is available through the `Centroid::Config` class, an instance of which is used to consume the JSON.
11
+ In Ruby, the `Centroid::Config` class exposes the following:
26
12
 
27
- The `Config` class exposes the `from_file` class method, an initializer, and the instance method `for_environment`.
13
+ + Static `from_file` class method
14
+ + Initializer
15
+ + `for_environment` instance method
16
+ + `has_key?` instance method
28
17
 
29
- #### from_file(filename)
18
+ > *Note:* The examples given in the following sections are based on the JSON configuration file examples in the [Centroid document] (../README.md#examples).
30
19
 
31
- You can create an instance of `Config` from a JSON file using the `Centroid::Config.from_file(filename)` class method.
20
+ ### from_file Class Method
32
21
 
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.
22
+ Using the static `Centroid::Config.from_file(filename)` class method, you can create an instance of `Config` from a JSON file, as in the example below.
34
23
 
35
24
  ```rb
36
25
  # from_file.rb
@@ -38,9 +27,9 @@ config = Centroid::Config.from_file("config.json")
38
27
  server = config.database.server_address # => "my-server.local"
39
28
  ```
40
29
 
41
- ### Config(json)
30
+ ### Config Initializer
42
31
 
43
- Alternatively, you can create an instance of `Config` by passing a JSON string to the `Config` initializer.
32
+ To load a string instead of a file, create an instance of `Config` by passing a JSON string to the `Config` initializer, as in the following example.
44
33
 
45
34
  ```rb
46
35
  # from_string.rb
@@ -49,35 +38,13 @@ config = Centroid::Config(json)
49
38
  server = config.database.server_address # => "my-server.local"
50
39
  ```
51
40
 
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
- ```
41
+ ### for_environment Instance Method
42
+
43
+ In the `Config` instance, you can use the `for_environment` instance method to retrieve the configuration values for an environment.
77
44
 
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.
45
+ If you specify an environment in `for_environment`, Centroid will merge the requested environment's configuration values with the values in *all*. Refer to [Examples in the Centroid document] (../README.md#examples) for information on creating an environment-based JSON configuration file.
79
46
 
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`.
47
+ With the following example, Centroid will merge the configuration for *prod* with the configuration for *all*; the result is then available from a new instance of `Config`.
81
48
 
82
49
  ```rb
83
50
  # for_enviroment.rb
@@ -86,6 +53,13 @@ server = config.some_resource.server # => "resource-prod.local"
86
53
  solution_path = config.keys.ssh # => "path/to/id_rsa.pub"
87
54
  ```
88
55
 
89
- ## Contributing
56
+ ### has_key? Instance Method
90
57
 
91
- See the [contributing section of the main README](../README.md#contributing)
58
+ In a `Config` instance, you can use the `has_key?` method to determine if a key exists. `has_key?` is also aliased as `include?`. These methods use the same case and underscore rules as is used for value lookups.
59
+
60
+ ```rb
61
+ json = '{ "database": { "serverAddress": "my-server.local" } }'
62
+ config = Centroid::Config(json)
63
+ config.has_key?(:database) # => true
64
+ config.include?("DoesNotExist") # => false
65
+ ```
data/centroid.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'centroid'
3
- s.version = '1.0.0'
3
+ s.version = '1.1.0-alpha1'
4
4
  s.summary = 'A centralizaed paradigm to configuration management.'
5
5
  s.description = 'Centroid is a tool for loading configuration values declared in JSON, and accessing those configuration values using object properties.'
6
6
  s.author = 'Resource Data, Inc'
data/lib/centroid.rb CHANGED
@@ -17,10 +17,14 @@ module Centroid
17
17
  self[attrib]
18
18
  end
19
19
 
20
+ def respond_to_missing?(method_name, include_all)
21
+ has_key?(method_name)
22
+ end
23
+
20
24
  def [](key)
21
- value = find_value(key, raw_config)
25
+ raise KeyError.new("Centroid::Config instance does not contain key: #{key}") unless has_key?(key)
22
26
 
23
- raise KeyError.new("Centroid::Config instance does not contain key: #{key}") if value.nil?
27
+ value = find_value(key)
24
28
 
25
29
  if value.is_a?(Hash)
26
30
  Config.new(value)
@@ -29,18 +33,24 @@ module Centroid
29
33
  end
30
34
  end
31
35
 
36
+ def has_key?(key)
37
+ !!actual_key(key)
38
+ end
39
+
40
+ alias_method :include?, :has_key?
41
+
32
42
  def to_s
33
43
  JSON.dump(raw_config)
34
44
  end
35
45
 
36
46
  def for_environment(env)
37
47
  env_json = raw_config[env]
38
- all_key = actual_key("all", raw_config)
48
+ all_key = actual_key("all")
39
49
  if all_key.nil?
40
50
  Config.new(env_json)
41
51
  else
42
52
  all_json = raw_config[all_key]
43
- Config.new(all_json.merge(env_json))
53
+ Config.new(deep_merge(all_json, env_json))
44
54
  end
45
55
  end
46
56
 
@@ -55,12 +65,12 @@ module Centroid
55
65
  unnormalised_key.to_s.gsub("_", "").downcase
56
66
  end
57
67
 
58
- def find_value(key, hashtable)
59
- hashtable[actual_key(key, hashtable)]
68
+ def find_value(key)
69
+ raw_config[actual_key(key)]
60
70
  end
61
71
 
62
- def actual_key(key, hashtable)
63
- hashtable.keys.find { |k| normalize_key(key) == normalize_key(k) }
72
+ def actual_key(key)
73
+ raw_config.keys.find { |k| normalize_key(key) == normalize_key(k) }
64
74
  end
65
75
 
66
76
  def validate_unique_keys!
@@ -72,5 +82,20 @@ module Centroid
72
82
  keys = dups.values.flat_map { |d| d.map { |e| e[:key] } }
73
83
  raise KeyError, "Centroid::Config instance contains duplicate keys: #{keys.join(', ')}"
74
84
  end
85
+
86
+ def deep_merge(left, right)
87
+ return right if not right.is_a?(Hash)
88
+
89
+ right.each_pair do |k, rv|
90
+ lv = left[k]
91
+ left[k] = if lv.is_a?(Hash) && rv.is_a?(Hash)
92
+ deep_merge(lv, rv)
93
+ else
94
+ rv
95
+ end
96
+ end
97
+
98
+ left
99
+ end
75
100
  end
76
101
  end
@@ -84,4 +84,33 @@ class ConfigTests < Test::Unit::TestCase
84
84
  config = config.for_environment("Prod")
85
85
  assert_equal(config.shared, "production!")
86
86
  end
87
+
88
+ def test_all_environment_is_not_case_sensitive
89
+ config = Centroid::Config.new('{"Prod": {"Shared": "production!"}, "All": {"Shared": "none", "AllOnly": "works"}}')
90
+ config = config.for_environment("Prod")
91
+ assert_equal(config.all_only, "works")
92
+
93
+ config = Centroid::Config.new('{"Prod": {"Shared": "production!"}, "all": {"Shared": "none", "AllOnly": "works"}}')
94
+ config = config.for_environment("Prod")
95
+ assert_equal(config.all_only, "works")
96
+ end
97
+
98
+ def test_supports_deep_merge
99
+ config = Centroid::Config.new('{"Prod": {"Database": {"Server": "prod-sql"}}, "All": {"Database": {"MigrationsPath": "path/to/migrations"}}}')
100
+ config = config.for_environment("Prod")
101
+ assert_equal(config.database.server, "prod-sql")
102
+ assert_equal(config.database.migrations_path, "path/to/migrations")
103
+ end
104
+
105
+ def test_has_key
106
+ config = Centroid::Config.new(json_config)
107
+ assert(config.has_key?("environment"))
108
+ assert(!config.has_key?("does_not_exist"))
109
+ end
110
+
111
+ def test_respond_to
112
+ config = Centroid::Config.new(json_config)
113
+ assert(config.respond_to?(:environment))
114
+ assert(!config.respond_to?(:does_not_exist))
115
+ end
87
116
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: centroid
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0.pre.alpha1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Resource Data, Inc
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-20 00:00:00.000000000 Z
11
+ date: 2014-06-30 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Centroid is a tool for loading configuration values declared in JSON,
14
14
  and accessing those configuration values using object properties.
@@ -36,12 +36,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
36
36
  version: '0'
37
37
  required_rubygems_version: !ruby/object:Gem::Requirement
38
38
  requirements:
39
- - - ">="
39
+ - - ">"
40
40
  - !ruby/object:Gem::Version
41
- version: '0'
41
+ version: 1.3.1
42
42
  requirements: []
43
43
  rubyforge_project:
44
- rubygems_version: 2.2.0
44
+ rubygems_version: 2.3.0
45
45
  signing_key:
46
46
  specification_version: 4
47
47
  summary: A centralizaed paradigm to configuration management.