yaconfig 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ OWVjZmY5NGMzNGE2NjhmNjk1NDQ3NzJhNTJjMzhjYzYxODZkMjE2OA==
5
+ data.tar.gz: !binary |-
6
+ MTdiYzI5ZTU2MGFlZGQwMTcxNmNlMWE4OWJjNTAyZDYzYmJjNmE2MQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ MGMxOGVmMmJiZGI1NzQxNmJkZDM2ODcwNDhlMGUzNThhMTcxMzNjOGM0Nzhl
10
+ MGM4OTg5MmVlMTNiOTZkMzhiMWZlZjIwNTY5YjAyNWRmOTVmYWU1MjkxYjMz
11
+ MjA2ODNlYjk0YzdhOTdjYWU0NjVlZjA3N2YzMTBlYzdjOWMyMTk=
12
+ data.tar.gz: !binary |-
13
+ NTRhODQ5NTRkZDQzMjU2MTZiNWJhN2UyMGRjYzNjNmRjNDNjOGEyNDk0YWFk
14
+ NjZlMGE1ODc2YmVjMDYzOGY1YjMyZWEzN2U1NzJkZTQ4MTc4MTZmMDFiZGFm
15
+ YjU4ZjlkOWZlM2Y3YzgxODFmMmE1OTZhMjE4MzVjYjk4MzM4MTA=
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in yaconfig.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Peter Torkelson
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,204 @@
1
+ # Yet Another Configuration Gem (yaconfig)
2
+
3
+ Why another one? I wanted a config gem for my little utilities I write for people that was both simple,
4
+ but still provided a file loading interface that would merge configuration files.
5
+
6
+ The config store is mostly a [SymbolTable](https://github.com/mjijackson/symboltable) wrapped with some utilities.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'yaconfig'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install yaconfig
21
+
22
+ And require it:
23
+
24
+ require 'yaconfig'
25
+
26
+ ## Usage
27
+
28
+ In it's most basic useage it's simply a class that acts as a hash
29
+ with some magic to both convert the hash keys into symbols, and to
30
+ dynamically create accessors.
31
+
32
+ conf = Yaconfig::Configuration.new
33
+ conf.something = 'my configuration value'
34
+ conf.email = 'so-and-so@stupid-spammers.com'
35
+
36
+ ### New
37
+
38
+ The new accepts both a hash as an argument to be converted into the
39
+ configuration, and the block style as well.
40
+
41
+ conf = Yaconfig::Configuration.new({:email => 'so-and-so@stupid-spammers.com',
42
+ :otherthing => 'other value'})
43
+
44
+ conf = Yaconfig::Configuration.new { |c|
45
+ c.something = 'my configuration value'
46
+ c.email = 'so-and-so@stupid-spammers.com'
47
+ }
48
+
49
+ ### Access to Data
50
+
51
+ You can use various forms to access the data, both the \[\] hash notation, and the
52
+ quicker dot notation. This is for both setting and reading.
53
+
54
+ puts conf.email
55
+ puts conf[:email]
56
+
57
+ #### Nesting
58
+
59
+ The class allows nesting, so you can do the following:
60
+
61
+ conf.datacenter = {}
62
+ conf.datacenter.address = '555 Wheee Street'
63
+ conf.datacenter.phone_number = '555-555-5555'
64
+
65
+ Note the first line. I experimented with auti-creating the middle elements of these, such
66
+ as the datacenter here, but that caused non-existant elements to no longer return `nil`.
67
+ So as a limitation before you can assign to a sub container, you must initialize it.
68
+
69
+ ### Loading Files
70
+
71
+ One of the important features of the gem is the ability to load and merge configurations.
72
+ It supports two formats: ruby and json.
73
+
74
+ #### Ruby
75
+
76
+ Ruby configuration files are simply evaluated with a `config` object accessable to the file.
77
+ It is not really sandboxed at all. So don't use this if you don't trust the source of your
78
+ config files.
79
+
80
+ In your program:
81
+
82
+ conf.load_config(
83
+ "./%basename%.conf",
84
+ "%basedir%/../etc/%basename%.conf",
85
+ "%basedir%/../etc/%basename%/%basename%.conf", # debian style
86
+ "/etc/%basename%.conf",
87
+ "/etc/%basename%/%basename%.conf",
88
+ "~/.%basename%.rc"
89
+ )
90
+
91
+ Substitutions:
92
+ * %basename% - This is the command the user called, minus the extension .rb if it exists.
93
+ * %basedir% - This is the directory the command was in.
94
+ * ~ - User's home directory.
95
+
96
+ This will attempt to load each of the files in order, and the results get merged. So if you
97
+ want ~/.myapp.rc to have final say, you list it last.
98
+
99
+ The config file can do anything ruby can do, for better or worse. An example:
100
+
101
+ config.movie = 'The Sound of Music'
102
+ config.list = 1..10
103
+
104
+ If the config file does not exist, it's skipped, so it's ok to list anything you might want.
105
+
106
+ #### JSON
107
+
108
+ Good for automatically generated configs, or if you don't trust the ruby someone might stick in
109
+ their config.
110
+
111
+ From raw JSON in a string:
112
+
113
+ conf.configure_json(json_data)
114
+
115
+ From files:
116
+
117
+ conf.load_config_json(
118
+ "./%basename%.json",
119
+ "%basedir%/../etc/%basename%.json",
120
+ "%basedir%/../etc/%basename%/%basename%.json", # debian style
121
+ "/etc/%basename%.json",
122
+ "/etc/%basename%/%basename%.json",
123
+ "~/.%basename%.rc"
124
+ )
125
+
126
+ #### Blocks
127
+
128
+ If you want to pass it another block to merge in, you can use:
129
+
130
+ conf.configure {
131
+ # ...
132
+ }
133
+
134
+ Just like with new. See above.
135
+
136
+ #### Hases
137
+
138
+ You can feed it additional hashes with `merge!`
139
+
140
+ conf.merge!({:whatever => 'something'})
141
+
142
+ ### Other stuff
143
+
144
+ I included a short cut to pretty-print in json format the config for debugging
145
+ use. You can use it with:
146
+
147
+ puts conf.to_json_pretty
148
+
149
+ If you don't like the way I come up with the base-name, you can set it manually with:
150
+
151
+ conf.basename = 'myProgram'
152
+
153
+ Naturally you can check to see what it generated on it's own with:
154
+
155
+ puts conf.basename
156
+
157
+ You can set the verbosity of the gem with:
158
+
159
+ conf.verbose = 1
160
+
161
+ It expects `nil`, `false`, or a integer. 1 causes it to tell you what config files it
162
+ loaded. 2 causes it to also tell you which ones it could not load.
163
+
164
+ ### Suggested Usage
165
+
166
+ Not that I want to tell you how to code, but this is how I use it:
167
+
168
+ # I like to assign it to a constant to make it stand out, and accessable
169
+ CFG = Yaconfig::Configuration.new { |c|
170
+ # Create any sub containers that you expect your users to use.
171
+ c.datacenter = {}
172
+ c.user_info = {}
173
+
174
+ # Set any defaults.
175
+ c.email = 'no-reply@no-such-domain.com'
176
+ }
177
+
178
+ Then load your user's configs as shown above.
179
+
180
+ If you are parsing options with something like Slop or whatever floats your boat, it
181
+ becomes trivial to add a config switch:
182
+
183
+ CFG.load_config(opts[:config]) if opts[:config]
184
+
185
+ ## Todo
186
+
187
+ * Fix any bugs that are found.
188
+ * Do proper in-line documentation in the code.
189
+
190
+ ## Contributing
191
+ I'm not scared of feedback. Usually. I only bite a little. :)
192
+
193
+ ### By Suggesting Improvments and Reporting Bugs
194
+
195
+ Use the github issue tracker. No promises on how fast I'll deal with either. I have
196
+ a job and all that. :)
197
+
198
+ ### By Writing Code
199
+
200
+ 1. Fork it
201
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
202
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
203
+ 4. Push to the branch (`git push origin my-new-feature`)
204
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require "rake/version_task"
4
+ Rake::VersionTask.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.5
data/lib/yaconfig.rb ADDED
@@ -0,0 +1,107 @@
1
+ require "yaconfig/version"
2
+
3
+ require 'symboltable'
4
+ require 'json'
5
+
6
+ module Yaconfig
7
+ class Configuration < SymbolTable
8
+
9
+ # Legacy module to recieve constants
10
+ attr_accessor :base_name, :verbose
11
+
12
+ # Setup @data. Accept an optional hash of config.
13
+ def initialize(data=nil, &block)
14
+ # Base name of the program, minus .rb if any.
15
+ @base_name = File::basename($0)
16
+ @base_name = @base_name[0..-4] if @base_name =~ /\.rb$/
17
+
18
+ # Off by default
19
+ @verbose = false
20
+
21
+ # List of loaded config files
22
+ @configs = []
23
+
24
+ # Install any passed data into the SymbolTable
25
+ r = super(data)
26
+
27
+ # Do a block style config, if any
28
+ block.call(self) if block_given?
29
+
30
+ # Return what super gave us.
31
+ return r
32
+ end
33
+
34
+ # On the fly creation of sub containers.
35
+ #
36
+ # downside is it kills config.x.y #=> nil
37
+ # so I have left this out until I can magically figure out I'm on the
38
+ # left side of the expression.
39
+
40
+ # def method_missing(method, *args, &block)
41
+ # rv = super
42
+ # if !rv
43
+ # store(method, {})
44
+ # super
45
+ # end
46
+ # end
47
+
48
+ # Use a block-style configure setup.
49
+ #
50
+ # config.configure do |c|
51
+ # c.whatever = a_value
52
+ # end
53
+ def configure(&block)
54
+ raise "No block given to configure." if !block_given?
55
+
56
+ block.call(self)
57
+ end
58
+
59
+ # Search a list of files, and load any that exist. Provides
60
+ # the object as 'config'
61
+ def load_config(*args)
62
+ args.flatten.each{ |f| load_config_file(f) }
63
+ end
64
+
65
+ # JSON support
66
+ def configure_json(json_text)
67
+ self.merge!(JSON.parse(json_text))
68
+ end
69
+
70
+ def to_json_pretty()
71
+ JSON.pretty_generate(self)
72
+ end
73
+
74
+ def load_config_json(*args)
75
+ args.flatten.each{ |f|
76
+ configure_json(File.new(f, 'r').read) if File.exists?(f)
77
+ }
78
+ end
79
+
80
+ private # ----------------------------------
81
+
82
+ # expand path
83
+ def expand_path(file)
84
+ file.gsub!(/\%basedir\%/, File.dirname(File.expand_path($0)))
85
+ file.gsub!(/\%basename\%/, @base_name)
86
+ File.expand_path(file)
87
+ end
88
+
89
+ # Used by load_config
90
+ def load_config_file(filename)
91
+ config = self
92
+
93
+ filename = expand_path(filename)
94
+
95
+ return if @configs.include?(filename)
96
+
97
+ if File.exists?(filename)
98
+ puts "Loading config from: #{filename}" if @verbose
99
+ eval File.new(filename, "r").read
100
+ @configs << filename
101
+ else
102
+ puts "Config file does not exist: #{filename}" if @verbose && @verbose >= 2
103
+ end
104
+ end
105
+
106
+ end
107
+ end
@@ -0,0 +1,6 @@
1
+ require 'version'
2
+
3
+ module Yaconfig
4
+ is_versioned
5
+ #VERSION = '0.0.1'
6
+ end
data/yaconfig.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/yaconfig/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Peter Torkelson"]
6
+ gem.email = ["peter.torkelson@gmail.com"]
7
+ gem.description = %q{Yet Another Configuration gem.}
8
+ gem.summary = %q{Handles both configuration storage and loading.}
9
+ gem.homepage = "https://github.com/WhiteFire-Sondergaard/yaconfig"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "yaconfig"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Yaconfig::VERSION
17
+
18
+ gem.add_dependency('symboltable', '>= 1.0.2')
19
+ gem.add_dependency('version', '>= 1.0.0')
20
+ gem.add_dependency('json', '>= 1.7.5')
21
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yaconfig
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.5
5
+ platform: ruby
6
+ authors:
7
+ - Peter Torkelson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: symboltable
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 1.0.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: version
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: 1.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: json
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: 1.7.5
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: 1.7.5
55
+ description: Yet Another Configuration gem.
56
+ email:
57
+ - peter.torkelson@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE
65
+ - README.md
66
+ - Rakefile
67
+ - VERSION
68
+ - lib/yaconfig.rb
69
+ - lib/yaconfig/version.rb
70
+ - yaconfig.gemspec
71
+ homepage: https://github.com/WhiteFire-Sondergaard/yaconfig
72
+ licenses: []
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.0.3
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: Handles both configuration storage and loading.
94
+ test_files: []