sting 0.1.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ddebedec4cecec6db3c92d487c60b6a4b841a5aeb93ad668ef101a12ece92334
4
- data.tar.gz: 401bc1ff2e7c00631bca054a957fc28475094c85ec6b368f207ebe1185100855
3
+ metadata.gz: 91a794768cc4e13515dd2cc4867a7f29a4a346c415625b8498378977023cfeea
4
+ data.tar.gz: 7cd20416e9154d42e1d5e53b2fe5aa8a872bd586cf2ade1b51c7ac3f9c3d3899
5
5
  SHA512:
6
- metadata.gz: 6076bc7d1301e66d30074d06ac3601aabd9461ba71e68362af647e081714305c402cea41b1d88276ec5d6d6e0cd466dfdbd8067ee41e7f5e6bd8a6f7d7ad1e33
7
- data.tar.gz: ff0ff3133cae18cedbf06106a66ded80dacabc3ad184f35fe11893d2e5ae994a32093d149c5cc9ec2437fd6e13d1ab7f76ed559f94c17a894d06feed53510dbb
6
+ metadata.gz: 3a9b1e2ec851db2d9bcbcbd75b0cb009a5d6160ee6627d91ad7dcaee9f2df73f1de48f1ef2067894a3d735e746e8a2058662040475da5ce8df476f4dfdbecf43
7
+ data.tar.gz: 1179d98777ef5f5ef6a8d9657698be963bd47b20ab8f7ab6a4d992c25aa77d4e21d487b5980a51bb5a9bc872506b6dfb63b0d6d873508b76d0bfa77ae8f0521d
data/README.md CHANGED
@@ -2,9 +2,8 @@ Sting - Minimal Settings Library
2
2
  ==================================================
3
3
 
4
4
  [![Gem Version](https://badge.fury.io/rb/sting.svg)](https://badge.fury.io/rb/sting)
5
- [![Build Status](https://travis-ci.com/DannyBen/sting.svg?branch=master)](https://travis-ci.com/DannyBen/sting)
5
+ [![Build Status](https://github.com/DannyBen/sting/workflows/Test/badge.svg)](https://github.com/DannyBen/sting/actions?query=workflow%3ATest)
6
6
  [![Maintainability](https://api.codeclimate.com/v1/badges/c8afe395a8f2cf290fec/maintainability)](https://codeclimate.com/github/DannyBen/sting/maintainability)
7
- [![Test Coverage](https://api.codeclimate.com/v1/badges/c8afe395a8f2cf290fec/test_coverage)](https://codeclimate.com/github/DannyBen/sting/test_coverage)
8
7
 
9
8
  ---
10
9
 
@@ -15,31 +14,150 @@ Sting is a minimal, lightweight, multi-YAML settings library.
15
14
  Installation
16
15
  --------------------------------------------------
17
16
 
18
- $ gem install sting
17
+ ```shell
18
+ $ gem install sting
19
+ ```
20
+
21
+
22
+ Features
23
+ --------------------------------------------------
19
24
 
25
+ - [Aggressively minimalistic][1].
26
+ - Settings are accessible through a globally available class.
27
+ - Can be used either as a singleton class or as an instance.
28
+ - Load and merge one or more YAML files or hashes.
29
+ - Settings objects are standard ruby hashes, arrays and basic types.
30
+ - Ability to update settings at runtime.
31
+ - [Ability to extend](#extending-yaml-files) (import) other YAML files.
32
+ - ERB code in the YAML files will be evaluated.
33
+
34
+
35
+ Nonfeatures
36
+ --------------------------------------------------
37
+
38
+ - No dot notation access to nested values - Use `Settings.server['host']`
39
+ instead of `Settings.server.host`.
40
+ - No special generators for Rails.
41
+ [Usage with rails is still trivial](#using-with-rails).
20
42
 
21
43
 
22
44
  Usage
23
45
  --------------------------------------------------
24
46
 
47
+ ### Using as a singleton class
48
+
25
49
  ```ruby
26
50
  require 'sting'
27
51
 
28
- # If you want to use a different name than Sting
52
+ # If you want to use a different name than Sting (optional)
29
53
  Settings = Sting
30
54
 
31
- # Load some YAML files
55
+ # Load some YAML files. If the provided filename does not end with '.yml' or
56
+ # '.yaml', we will add '.yml' to it.
57
+ Settings << 'one'
58
+ Settings << 'two.yml'
59
+
60
+ # Adding additional files can also be done with `#push`. These two are the
61
+ # same.
32
62
  Settings << 'one'
33
- Settings << 'two'
63
+ Settings.push 'one'
64
+
65
+ # Merge with another options hash
66
+ Settings << { port: 3000, host: 'localhost' }
34
67
 
35
68
  # Access values
36
- p Settings.hello
37
- p Settings['hello']
38
- p Settings[:hello]
69
+ p Settings.host
70
+ p Settings['host']
71
+ p Settings[:host]
72
+
73
+ # Access nested values
74
+ p Settings.server['host']
39
75
 
40
- # Access all values
41
- p Settings.all
76
+ # Access nested values safely (get nil if any of the keys does not exist)
77
+ p Settings[:server, :host]
78
+ p Settings[:server, :production, :host]
79
+
80
+ # Get the hash of all values
81
+ p Settings.settings
42
82
 
43
83
  # Check if a key is defined
44
84
  p Settings.has_key? :hello
85
+
86
+ # Access value, but raise an exception if it does not exist
87
+ p Settings.host!
88
+
89
+ # Access boolean values, or check if a key exists
90
+ p Settings.host?
91
+
92
+ # Update a value (in memory only, not in file)
93
+ Settings.port = 3000
94
+
95
+ # Reset (erase) all values
96
+ Settings.reset!
97
+ ```
98
+
99
+ ### Using as an instance
100
+
101
+ All the above operations are also available to instances of `Sting`.
102
+
103
+ ```ruby
104
+ require 'sting'
105
+
106
+ # Create an instance.
107
+ config = Sting.new
108
+
109
+ # Or, create an instance, and provide the first source file to it.
110
+ config = Sting.new 'settings'
111
+
112
+ # Load additional YAML files.
113
+ config << 'local_settings'
45
114
  ```
115
+
116
+
117
+ ### Extending YAML files
118
+
119
+ Sting uses [ExtendedYAML], which means you can use the `extends` key to load
120
+ one ormore YAML files into your loaded configuration files:
121
+
122
+ ```yaml
123
+ # Extend a single file (extension is optional)
124
+ extends: some-other-file.yml
125
+
126
+ # Extend multiple files
127
+ extends:
128
+ - some-file
129
+ - another-file
130
+ ```
131
+
132
+
133
+ Using with Rails
134
+ --------------------------------------------------
135
+
136
+ You can use this however you wish in Rails. This is the recommended
137
+ implementation:
138
+
139
+ Add sting to your Gemfile:
140
+
141
+ ```ruby
142
+ gem 'sting'
143
+ ```
144
+
145
+ Create an initializer:
146
+
147
+ ```ruby
148
+ # config/initializers/settings.rb
149
+ Settings = Sting
150
+ Settings << "#{Rails.root}/config/settings"
151
+ Settings << "#{Rails.root}/config/settings/#{Rails.env}"
152
+ ```
153
+
154
+ Create four config files:
155
+
156
+ - config/**settings**.yml
157
+ - config/settings/**development**.yml
158
+ - config/settings/**production**.yml
159
+ - config/settings/**test**.yml
160
+
161
+
162
+ [1]: https://github.com/DannyBen/sting/blob/master/lib/sting/sting_operations.rb
163
+ [2]: https://github.com/DannyBen/extended_yaml
@@ -1,61 +1,11 @@
1
1
  require 'yaml'
2
2
  require 'erb'
3
+ require 'sting/sting_operations'
3
4
 
4
5
  class Sting
5
- class << self
6
- def <<(path)
7
- path = "#{path}.yml" unless path =~ /\.ya?ml$/
8
- content = File.read path
9
- content = YAML.load(ERB.new(content).result)
10
- settings.merge! content if content
11
- end
12
-
13
- def [](key)
14
- settings[key.to_s]
15
- end
16
-
17
- def method_missing(name, *args, &blk)
18
- name = name.to_s
19
- return settings[name] if has_key? name
20
-
21
- suffix = nil
22
-
23
- if name.end_with? *['=', '!', '?']
24
- suffix = name[-1]
25
- name = name[0..-2]
26
- end
27
-
28
- case suffix
29
- when "="
30
- settings[name] = args.first
31
-
32
- when "?"
33
- !!settings[name]
6
+ include StingOperations
34
7
 
35
- when "!"
36
- if has_key? name
37
- return settings[name]
38
- else
39
- raise ArgumentError, "Key '#{name}' does not exist"
40
- end
41
-
42
- else
43
- nil
44
-
45
- end
46
-
47
- end
48
-
49
- def settings
50
- @settings ||= {}
51
- end
52
-
53
- def reset!
54
- @settings = nil
55
- end
56
-
57
- def has_key?(key)
58
- settings.has_key? key.to_s
59
- end
8
+ class << self
9
+ include StingOperations
60
10
  end
61
11
  end
@@ -0,0 +1,77 @@
1
+ require 'extended_yaml'
2
+
3
+ class Sting
4
+ module StingOperations
5
+ def initialize(source = nil)
6
+ self << source if source
7
+ end
8
+
9
+ def <<(source)
10
+ if source.is_a? Hash
11
+ content = source.collect{ |k,v| [k.to_s, v] }.to_h
12
+
13
+ elsif source.include? '*'
14
+ Dir["#{source}"].each { |file| push file }
15
+
16
+ elsif File.directory? source
17
+ Dir["#{source}/*.yml"].each { |file| push file }
18
+
19
+ else
20
+ source = "#{source}.yml" unless source =~ /\.ya?ml$/
21
+ content = ExtendedYAML.load source
22
+ end
23
+
24
+ settings.merge! content if content
25
+ end
26
+ alias_method :push, :<<
27
+
28
+ def [](*keys)
29
+ settings.dig(*keys.map(&:to_s))
30
+ end
31
+
32
+ def method_missing(name, *args, &blk)
33
+ name = name.to_s
34
+ return settings[name] if has_key? name
35
+
36
+ suffix = nil
37
+
38
+ if name.end_with?('=', '!', '?')
39
+ suffix = name[-1]
40
+ name = name[0..-2]
41
+ end
42
+
43
+ case suffix
44
+ when "="
45
+ settings[name] = args.first
46
+
47
+ when "?"
48
+ !!settings[name]
49
+
50
+ when "!"
51
+ if has_key? name
52
+ return settings[name]
53
+ else
54
+ raise ArgumentError, "Key '#{name}' does not exist"
55
+ end
56
+
57
+ else
58
+ nil
59
+
60
+ end
61
+
62
+ end
63
+
64
+ def settings
65
+ @settings ||= {}
66
+ end
67
+ alias to_h settings
68
+
69
+ def reset!
70
+ @settings = nil
71
+ end
72
+
73
+ def has_key?(key)
74
+ settings.has_key? key.to_s
75
+ end
76
+ end
77
+ end
@@ -1,3 +1,3 @@
1
1
  class Sting
2
- VERSION = '0.1.0'
2
+ VERSION = '0.5.0'
3
3
  end
metadata CHANGED
@@ -1,99 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sting
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Ben Shitrit
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-06-28 00:00:00.000000000 Z
11
+ date: 2020-07-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: byebug
14
+ name: extended_yaml
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '10.0'
20
- type: :development
19
+ version: '0.2'
20
+ type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '10.0'
27
- - !ruby/object:Gem::Dependency
28
- name: rspec
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '3.6'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '3.6'
41
- - !ruby/object:Gem::Dependency
42
- name: rspec_fixtures
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: '0.3'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: '0.3'
55
- - !ruby/object:Gem::Dependency
56
- name: runfile
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - "~>"
60
- - !ruby/object:Gem::Version
61
- version: '0.10'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - "~>"
67
- - !ruby/object:Gem::Version
68
- version: '0.10'
69
- - !ruby/object:Gem::Dependency
70
- name: runfile-tasks
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - "~>"
74
- - !ruby/object:Gem::Version
75
- version: '0.4'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - "~>"
81
- - !ruby/object:Gem::Version
82
- version: '0.4'
83
- - !ruby/object:Gem::Dependency
84
- name: simplecov
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - "~>"
88
- - !ruby/object:Gem::Version
89
- version: '0.15'
90
- type: :development
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - "~>"
95
- - !ruby/object:Gem::Version
96
- version: '0.15'
26
+ version: '0.2'
97
27
  description: Minimal, lightweight, multi-YAML settings
98
28
  email: db@dannyben.com
99
29
  executables: []
@@ -102,6 +32,7 @@ extra_rdoc_files: []
102
32
  files:
103
33
  - README.md
104
34
  - lib/sting.rb
35
+ - lib/sting/sting_operations.rb
105
36
  - lib/sting/version.rb
106
37
  homepage: https://github.com/dannyben/sting
107
38
  licenses:
@@ -122,8 +53,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
122
53
  - !ruby/object:Gem::Version
123
54
  version: '0'
124
55
  requirements: []
125
- rubyforge_project:
126
- rubygems_version: 2.7.6
56
+ rubygems_version: 3.1.2
127
57
  signing_key:
128
58
  specification_version: 4
129
59
  summary: Minimal, lightweight, multi-YAML settings