cogwheels 0.1.1 → 0.2.0

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: c5ee70dfbd9dd92d4281567d1ba1ada1a5bb8a02
4
- data.tar.gz: 0ce0a29503a912eb6d8f239957b001cee86df9a5
3
+ metadata.gz: bb919b6dc0b5705ef61dead9251ea9d7102c9d85
4
+ data.tar.gz: 704d4009e3d5f5264805243f93dfeafda62b6beb
5
5
  SHA512:
6
- metadata.gz: 5ad807c8cb8d9af9bcbaf71aaa19a7a9fa6687694c159f7d1ffc202297f38884aedd82029a49721bb63cca3143afd3dad99e46a595b6f221765bf0019b37e7d4
7
- data.tar.gz: a469ca79dede33cc0b176f29034d49b9e541f2bf598a604e9dfcf9f0f744011713c4768e755246742dc5fd57b24a1969ba984a6b3b0bfec2a5f4aaa39ffbc345
6
+ metadata.gz: 95848c537772c30137a3658bdaaecd1edecbe98075f2f89aaacf5b4307cb2d021acda197f06608855310bfefc593b7f1d1f0873ee425c5b2fad7c4f5a8bfb2fa
7
+ data.tar.gz: d5f115a73e99beb1a8cfb9a30f6a6d3ce25013e157aa230d0da1313cbb8f4b5405e9843614201f1bfbece731a5ed767e0030533a63e47bce04f638d8badde5f7
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2016 Christopher Lutz
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2016 Christopher Lutz
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md CHANGED
@@ -1,7 +1,77 @@
1
- [![Dependency Status](https://gemnasium.com/badges/github.com/chrisblutz/cogwheels.svg)](https://gemnasium.com/github.com/chrisblutz/cogwheels)
2
- [![Linux Build Status](https://travis-ci.org/chrisblutz/cogwheels.svg?branch=master)](https://travis-ci.org/chrisblutz/cogwheels)
3
- [![Inline docs](http://inch-ci.org/github/chrisblutz/cogwheels.svg?branch=master)](http://inch-ci.org/github/chrisblutz/cogwheels)
4
- [![Test Coverage](https://codeclimate.com/github/chrisblutz/cogwheels/badges/coverage.svg)](https://codeclimate.com/github/chrisblutz/cogwheels/coverage)
5
- [![Code Climate](https://codeclimate.com/github/chrisblutz/cogwheels/badges/gpa.svg)](https://codeclimate.com/github/chrisblutz/cogwheels)
6
- # Cogwheels
7
- **Cogwheels** is a work-in-progress configuration-loading library for Ruby.
1
+ [![Gem Version](https://badge.fury.io/rb/cogwheels.svg)](http://badge.fury.io/rb/cogwheels)
2
+ [![Dependency Status](https://gemnasium.com/badges/github.com/chrisblutz/cogwheels.svg)](https://gemnasium.com/github.com/chrisblutz/cogwheels)
3
+ [![Linux Build Status](https://travis-ci.org/chrisblutz/cogwheels.svg?branch=master)](https://travis-ci.org/chrisblutz/cogwheels)
4
+ [![Inline docs](http://inch-ci.org/github/chrisblutz/cogwheels.svg?branch=master)](http://inch-ci.org/github/chrisblutz/cogwheels)
5
+ [![Test Coverage](https://codeclimate.com/github/chrisblutz/cogwheels/badges/coverage.svg)](https://codeclimate.com/github/chrisblutz/cogwheels/coverage)
6
+ [![Code Climate](https://codeclimate.com/github/chrisblutz/cogwheels/badges/gpa.svg)](https://codeclimate.com/github/chrisblutz/cogwheels)
7
+
8
+ # Cogwheels
9
+
10
+ **Cogwheels** is a lightweight configuration-loading library for Ruby.
11
+
12
+ ## Installation
13
+
14
+ To install, use the standard RubyGems syntax:
15
+ ```sh
16
+ $ gem install cogwheels
17
+ ```
18
+ Or if necessary, you can add it to your `Gemfile`:
19
+ ```rb
20
+ gem 'cogwheels'
21
+ ```
22
+ As **Cogwheels** is not yet at version `1.0`, it is recommended to use version locking:
23
+ ```rb
24
+ gem 'cogwheels', '~> 0.2'
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ Assuming you have already installed the `cogwheels` gem or added it to your `Gemfile`, the first step
30
+ is to require it:
31
+ ```rb
32
+ require 'cogwheels'
33
+ ```
34
+ You can load the configuration file into a `Cogwheels::Configuration` object using the following:
35
+ ```rb
36
+ config = Cogwheels.load('.../file.yml')
37
+ ```
38
+ This `config` object uses methods similar to those in a `Hash` object:
39
+ ```rb
40
+ config[:key] # Retrieves the value for a key
41
+
42
+ config.key?(:key) # Checks if a value exists for a key
43
+
44
+ config[:key] = 'value' # Sets the value for a key
45
+ ```
46
+ However, the basic retrieval method (`config[:key]`) also takes a default value as an optional parameter:
47
+ ```rb
48
+ config[:key, 'default'] # Retrieves the value for a key, specifying a default value
49
+ ```
50
+ If for any reason you need the original `Hash` object, you can call `config.hash` to retrieve it.
51
+
52
+ ### Mutability
53
+
54
+ By default, `Configuration` instances are *mutable*, which means their values can be altered at any time. However, you can lock editing on a configuration tree by passing a second parameter to the `load` method:
55
+ ```rb
56
+ config = Cogwheels.load('.../file.yml', false)
57
+ ```
58
+ Now, whenever an edit is attempted (such as `config[:key] = 'value'`), an `ImmutableConfigurationError` will be raised.
59
+
60
+ ### Symbols As Keys
61
+
62
+ If needed, `Configuration` objects can be forced to convert *all* of their keys to symbols:
63
+ ```rb
64
+ config.to_symbol_keys
65
+ ```
66
+ So if this file is passed in:
67
+ ```yml
68
+ Test: value
69
+ ```
70
+ the value can be retrieved using:
71
+ ```rb
72
+ config[:Test]
73
+ ```
74
+
75
+ ## Copyright
76
+
77
+ Copyright (c) 2016 Christopher Lutz. See [LICENSE.txt](LICENSE.txt) for further details.
@@ -1,39 +1,39 @@
1
- $LOAD_PATH.unshift(File.dirname(File.realpath(__FILE__)) + '/../lib')
2
-
3
- # Internal dependencies
4
- require 'cogwheels/version'
5
-
6
- # Cogwheels allows YAML configuration files to be loaded into Configuration
7
- # instances for ease of use
8
- #
9
- # Cogwheels is licensed under the MIT license:
10
- #
11
- # Copyright (c) 2016 Christopher Lutz
12
- #
13
- # Permission is hereby granted, free of charge, to any person obtaining a copy
14
- # of this software and associated documentation files (the "Software"), to deal
15
- # in the Software without restriction, including without limitation the rights
16
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
- # copies of the Software, and to permit persons to whom the Software is
18
- # furnished to do so, subject to the following conditions:
19
- #
20
- # The above copyright notice and this permission notice shall be included in all
21
- # copies or substantial portions of the Software.
22
- #
23
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29
- # SOFTWARE.
30
- module Cogwheels
31
- autoload :Configuration, 'cogwheels/configuration'
32
- autoload :Loader, 'cogwheels/loader'
33
-
34
- module_function
35
-
36
- def load(src, mutable = true)
1
+ $LOAD_PATH.unshift(File.dirname(File.realpath(__FILE__)) + '/../lib')
2
+
3
+ # Internal dependencies
4
+ require 'cogwheels/version'
5
+
6
+ # Cogwheels allows YAML configuration files to be loaded into Configuration
7
+ # instances for ease of use
8
+ #
9
+ # Cogwheels is licensed under the MIT license:
10
+ #
11
+ # Copyright (c) 2016 Christopher Lutz
12
+ #
13
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
14
+ # of this software and associated documentation files (the "Software"), to deal
15
+ # in the Software without restriction, including without limitation the rights
16
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
+ # copies of the Software, and to permit persons to whom the Software is
18
+ # furnished to do so, subject to the following conditions:
19
+ #
20
+ # The above copyright notice and this permission notice shall be included in all
21
+ # copies or substantial portions of the Software.
22
+ #
23
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29
+ # SOFTWARE.
30
+ module Cogwheels
31
+ autoload :Configuration, 'cogwheels/configuration'
32
+ autoload :Loader, 'cogwheels/loader'
33
+
34
+ module_function
35
+
36
+ def load(src, mutable = true)
37
37
  Cogwheels::Loader.load(src, mutable)
38
- end
39
- end
38
+ end
39
+ end
@@ -7,12 +7,20 @@ module Cogwheels
7
7
  attr_reader :mutable
8
8
 
9
9
  def initialize(hash, mutable = true)
10
- @hash = hash
10
+ @hash = {}
11
+ hash.each do |key, value|
12
+ if value.is_a?(Hash)
13
+ value_config = Configuration.new(value, mutable)
14
+ @hash[key] = value_config
15
+ else
16
+ @hash[key] = value
17
+ end
18
+ end
11
19
  @mutable = mutable
12
20
  end
13
21
 
14
- def [](key)
15
- @hash[key]
22
+ def [](key, default = nil)
23
+ @hash[key] ||= default
16
24
  end
17
25
 
18
26
  def []=(key, value)
@@ -25,12 +33,40 @@ A modification was attempted on an immutable Configuration instance.
25
33
  @hash[key] = value if @mutable
26
34
  end
27
35
 
36
+ def key?(key)
37
+ @hash.key?(key)
38
+ end
39
+
40
+ def keys
41
+ @hash.keys
42
+ end
43
+
44
+ attr_reader :hash
45
+
46
+ def to_symbol_keys
47
+ @new_hash = {}
48
+ @hash.each do |key, value|
49
+ key = key.to_sym
50
+ @new_hash[key] = if value.is_a?(Cogwheels::Configuration)
51
+ value.to_symbol_keys
52
+ else
53
+ value
54
+ end
55
+ end
56
+ @hash = @new_hash
57
+ self
58
+ end
59
+
28
60
  def to_s
29
61
  @hash.to_s
30
62
  end
31
63
 
32
64
  def inspect
33
- "Cogwheels::Configuration =>\n#{@hash}"
65
+ "Cogwheels::Configuration (mutable: #{mutable}) =>\n#{@hash}"
66
+ end
67
+
68
+ def ==(other)
69
+ hash == other.hash && mutable == other.mutable
34
70
  end
35
71
  end
36
72
  end
@@ -1,3 +1,3 @@
1
1
  module Cogwheels
2
- VERSION = '0.1.1'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cogwheels
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christopher Lutz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-14 00:00:00.000000000 Z
11
+ date: 2016-11-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: safe_yaml
@@ -24,8 +24,10 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.0'
27
- description: Cogwheels is a library to load configurations from YAML files into easy-to-use
28
- hash-like objects
27
+ description: 'Cogwheels is a lightweight library to load configurations from YAML
28
+ files into easy-to-use hash-like objects
29
+
30
+ '
29
31
  email:
30
32
  - lutzblox@gmail.com
31
33
  executables: []
@@ -50,7 +52,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
50
52
  requirements:
51
53
  - - ">="
52
54
  - !ruby/object:Gem::Version
53
- version: '1.9'
55
+ version: '2.0'
54
56
  required_rubygems_version: !ruby/object:Gem::Requirement
55
57
  requirements:
56
58
  - - ">="
@@ -58,8 +60,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
58
60
  version: '0'
59
61
  requirements: []
60
62
  rubyforge_project:
61
- rubygems_version: 2.6.6
63
+ rubygems_version: 2.5.1
62
64
  signing_key:
63
65
  specification_version: 4
64
- summary: A Ruby configuration-loading library
66
+ summary: A lightweight Ruby configuration-loading library
65
67
  test_files: []