configly 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 88518ba064e0d9c3ce7c18929d93a5ad371954ee9b176a12f63aba62e8ab6f77
4
+ data.tar.gz: 0c53aa36ac509b6794f030c6e35663dc619af2d7ca8ce94437ec3b2a1d127fc1
5
+ SHA512:
6
+ metadata.gz: a50da64eadde0eef9a2bc2d2e8ba3fae64dfa4326bc36533d6ae6c0932796b5026fd28270634de7799f3c9e821a4b34f0775eea8c7baf15b165c6b32be228f16
7
+ data.tar.gz: e30f2c68e9f938c0fc54ea0b962e6f68b614ccddb0750a2b56617dc2f0bc2b1f3811a6f128a2c04da93029e1f4e0e44ab33e938526bf80498c0933aa81da3cf4
@@ -0,0 +1,168 @@
1
+ Configly - Minimal Settings Library
2
+ ==================================================
3
+
4
+ [![Gem Version](https://badge.fury.io/rb/configly.svg)](https://badge.fury.io/rb/configly)
5
+
6
+ Configly is a lightweight ruby Hash object with dot notation access.
7
+
8
+ It is designed for loading and using YAML configuration files.
9
+
10
+ ---
11
+
12
+ Installation
13
+ --------------------------------------------------
14
+
15
+ $ gem install configly
16
+
17
+
18
+ Usage
19
+ --------------------------------------------------
20
+
21
+ ### Initialization
22
+
23
+ Initialize a Configly object from Hash:
24
+
25
+ ```ruby
26
+ # Initialize from hash
27
+ require 'configly'
28
+ hash = {server: {host: 'localhost', port: 3000}}
29
+ configly = hash.to_configly
30
+ ```
31
+
32
+ or by loading one or more YAML files:
33
+
34
+ ```ruby
35
+ # Initialize by merging in YAML files
36
+ config = Configly.new
37
+ config << 'spec/fixtures/settings.yml'
38
+ puts config.imported.settings.also
39
+ #=> work
40
+ ```
41
+
42
+ You can append additional YAML files by using either `#<<` or `#load`.
43
+ The '.yml' extension is optional.
44
+
45
+ In addition, you may load YAML files to nested keys:
46
+
47
+ ```ruby
48
+ # Loading nested YAMLs
49
+ config = Configly.new
50
+ config << 'spec/fixtures/settings'
51
+
52
+ p config.imported.settings
53
+ #=> {:also=>"work"}
54
+
55
+ config.nested.settings.load 'spec/fixtures/settings'
56
+
57
+ p config.nested.settings
58
+ #=> {:imported=>{:settings=>{:also=>"work"}}}
59
+ ```
60
+
61
+ Configly objects inherit from Hash:
62
+
63
+ ```ruby
64
+ puts configly.is_a? Configly #=> true
65
+ puts configly.is_a? Hash #=> true
66
+ ```
67
+
68
+ ### Dot notation read access
69
+
70
+ Read values using dot notation:
71
+
72
+ ```ruby
73
+ # Dot notation access
74
+ puts configly.server.host
75
+ #=> localhost
76
+ ```
77
+
78
+ Reading nonexistent deep values will not raise an error:
79
+
80
+ ```ruby
81
+ # Deep dot notation access
82
+ p configly.some.deeply.nested_value
83
+ #=> {}
84
+ ```
85
+
86
+ To check if a key exists, use `?`
87
+
88
+ ```ruby
89
+ # Check if a key exists
90
+ p configly.some.deeply.nested_value?
91
+ #=> false
92
+
93
+ p configly.server.port?
94
+ #=> true
95
+ ```
96
+
97
+ To get the value or `nil` if it does not exist, use `!`:
98
+
99
+
100
+ ```ruby
101
+ # Get value or nil
102
+ p configly.some.deeply.nested_value!
103
+ #=> nil
104
+
105
+ p configly.server.port!
106
+ #=> 3000
107
+ ```
108
+
109
+
110
+ ### Dot notation write access
111
+
112
+ Writing values is just as easy:
113
+
114
+ ```ruby
115
+ # Dot notation write access
116
+ configly.production.server.port = 4000
117
+ puts configly.production.server.port
118
+ #=> 4000
119
+ ```
120
+
121
+ Arrays with hashes as values, will also work (as the nested hashes will be
122
+ coerced into Configly objects):
123
+
124
+ ```ruby
125
+ # Arrays of hashes
126
+ configly.servers = [
127
+ { host: 'prod1.example.com', port: 3000 },
128
+ { host: 'prod2.example.com', port: 4000 },
129
+ ]
130
+
131
+ puts configly.servers.first.host
132
+ #=> prod1.example.com
133
+
134
+ puts configly.servers.first.is_a? Configly
135
+ #=> true
136
+ ```
137
+
138
+ ### Array-like access
139
+
140
+ Configly allows read/write access using the usual array/hash syntax `#[]` using
141
+ either a string a symbol key:
142
+
143
+ ```ruby
144
+ # Array access
145
+ puts configly.server.port #=> 3000
146
+ puts configly.server[:port] #=> 3000
147
+ puts configly.server['port'] #=> 3000
148
+
149
+ configly.server[:port] = 4000
150
+ puts configly.server.port #=> 4000
151
+ ```
152
+
153
+
154
+ Limitations
155
+ --------------------------------------------------
156
+
157
+ Due to the fact that Configly is inheriting from Hash, and using
158
+ `method_missing` to allow dot notation access, your settings hashes cannot
159
+ use keys that are defined as methods in the Hash object.
160
+
161
+ When this case is identified, a `KeyError` will be raised.
162
+
163
+ ```ruby
164
+ # Reserved keys
165
+ configly.api.key = '53cr3t'
166
+ #=> #<KeyError: Reserved key: key>
167
+ ```
168
+
@@ -0,0 +1,71 @@
1
+ require 'yaml'
2
+ require 'erb'
3
+ require 'configly/extensions/hash'
4
+
5
+ class Configly < Hash
6
+ def []=(key, value)
7
+ raise KeyError.new("Reserved key: #{key}") if respond_to? key
8
+ super key.to_sym, coerce_value(value)
9
+ end
10
+
11
+ def [](key)
12
+ super key.to_sym
13
+ end
14
+
15
+ def <<(source)
16
+ source = "#{source}.yml" unless source =~ /\.ya?ml$/
17
+ content = File.read source
18
+ content = YAML.load(ERB.new(content).result).to_configly
19
+
20
+ merge! content if content
21
+ end
22
+ alias_method :load, :<<
23
+
24
+ def method_missing(method, *args, &block)
25
+ key = method
26
+ string_key = key.to_s
27
+ return self[key] if has_key? key
28
+
29
+ suffix = nil
30
+
31
+ if string_key.end_with? *['=', '!', '?']
32
+ suffix = string_key[-1]
33
+ key = string_key[0..-2].to_sym
34
+ end
35
+
36
+ case suffix
37
+ when "="
38
+ val = args.first
39
+ val = val.to_configly if val.is_a? Hash
40
+ self[key] = val
41
+
42
+ when "?"
43
+ has_key?(key) and !(self[key].is_a?(Configly) and self[key].empty?)
44
+
45
+ when "!"
46
+ (has_key?(key) and !(self[key].is_a?(Configly) and self[key].empty?)) ?
47
+ self[key] : nil
48
+
49
+ else
50
+ self[key] = self.class.new
51
+
52
+ end
53
+
54
+ end
55
+
56
+ protected
57
+
58
+ def coerce_value(value)
59
+ case value
60
+ when Configly
61
+ value
62
+ when Hash
63
+ value = value.to_configly
64
+ when Array
65
+ value.map { |v| coerce_value v }
66
+ else
67
+ value
68
+ end
69
+ end
70
+
71
+ end
@@ -0,0 +1,9 @@
1
+ class Hash
2
+ def to_configly
3
+ return self if self.is_a? Configly
4
+
5
+ configly = Configly.new
6
+ each { |key, val| configly[key] = val }
7
+ configly
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ class Configly < Hash
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: configly
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Danny Ben Shitrit
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-10-14 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Minimal, lightweight, multi-YAML settings
14
+ email: db@dannyben.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - README.md
20
+ - lib/configly.rb
21
+ - lib/configly/extensions/hash.rb
22
+ - lib/configly/version.rb
23
+ homepage: https://github.com/dannyben/configly
24
+ licenses:
25
+ - MIT
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: 2.2.0
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubygems_version: 3.0.4
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: Minimal, lightweight, multi-YAML settings
46
+ test_files: []