confoog 0.1.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2db750038a9c837b96abd1ac2354511befcc4e3c
4
+ data.tar.gz: 1ec4068f5ca4dec69f7a44c65efe70c81fabc700
5
+ SHA512:
6
+ metadata.gz: d83773371f01b4b96c294eca30170e66172ac0fa1d1c0ed56ae225aac0d1717ea227fa06d6e29803277db289bdf9b2f5ff32a839b16928478b1ee44dfda13738
7
+ data.tar.gz: 7d05a59b962bbc9cfedd100a9626bb2ea8f4a1c2b080aaa22373fa23133269c7419eeb0855e0107ff0fd9d2745ab3f9982d48e5ad56fa641a6d7a311ec88df52
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .token
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in confoog.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Seapagan
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,143 @@
1
+ # Confoog
2
+ [![Build Status](https://travis-ci.org/seapagan/confoog.svg)](https://travis-ci.org/seapagan/confoog)
3
+ [![Dependency Status](https://gemnasium.com/seapagan/confoog.svg)](https://gemnasium.com/seapagan/confoog)
4
+ [![Coverage Status](https://coveralls.io/repos/seapagan/confoog/badge.svg?branch=master&service=github)](https://coveralls.io/github/seapagan/confoog?branch=master)
5
+ [![Code Climate](https://codeclimate.com/github/seapagan/confoog/badges/gpa.svg)](https://codeclimate.com/github/seapagan/confoog)
6
+ [![Inline docs](http://inch-ci.org/github/seapagan/confoog.svg?branch=master)](http://inch-ci.org/github/seapagan/confoog)
7
+
8
+ A simple Gem to add YAML configuration files to your Ruby script / Gem.
9
+
10
+ __*A WORK IN PROGRESS, Not really ready for production use. The API may be subject to changes before hitting version 1.0.0*__
11
+
12
+ This will add a class that takes care of all your configuration needs for Ruby scripts and Gems.
13
+
14
+ ## Installation
15
+
16
+ Add this line to your application's Gemfile:
17
+
18
+ ```ruby
19
+ gem 'confoog'
20
+ ```
21
+
22
+ And then execute:
23
+
24
+ $ bundle
25
+
26
+ Or install it yourself as:
27
+
28
+ $ gem install confoog
29
+
30
+ ## Usage
31
+ Currently Confoog will not allow 'nested' configuration types, however each variable can be an array or hash so multiple settings can be recorded for each variable and accessed (for a hash) by `settings[variable][hash_key]` or array using `settings[array].each`. In other words, treat the return from `settings[var]` as the type it contains. See examples below.
32
+ ```ruby
33
+ require 'confoog'
34
+
35
+ settings = Confoog::Settings.new
36
+ settings[:var] = value
37
+ settings[:array] = [1, 2, 3, 4]
38
+ settings[42] = "Meaning of life"
39
+ settings[:urls] = ["https://www.mywebsite.com", "https://www.anothersite.com/a/page.html"]
40
+
41
+ settings[:urls].each do |url|
42
+ puts url
43
+ end
44
+ # https://www.mywebsite.com
45
+ # https://www.anothersite.com/a/page.html
46
+ # => ["https://www.mywebsite.com", "https://www.anothersite.com/a/page.html"]
47
+
48
+ settings[:dont_exist]
49
+ # => nil
50
+
51
+ a_variable = 50
52
+ settings[a_variable] = {:one => "for the money", :two => "for the show", :three => "to get ready"}
53
+ settings[50]
54
+ # => {:one=>"for the money", :two=>"for the show", :three=>"to get ready"}
55
+ settings[50][:two]
56
+ # => "for the show"
57
+
58
+ settings.save # save all current parameters to the YAML file
59
+
60
+ settings.load # load the settings from YAML file.
61
+ ```
62
+ Confoog will take several parameters on creation, to specify the default config file and location. For example :
63
+ ```ruby
64
+ settings = Confoog::Settings.new(location: '/home/myuser', filename: '.foo-settings')
65
+ ```
66
+ There are other optional flags or variables that can be passed on creation:
67
+ ```ruby
68
+ # Should a missing configuration file be created or not
69
+ create_file: true | false
70
+
71
+ # Specify an optional prefix before any error messages
72
+ prefix: 'My Application'
73
+
74
+ # Should we avoid outputting errors to the console? (ie in a GUI app)
75
+ quiet: true | false
76
+ ```
77
+ If these are not specified, Confoog will use the following defaults :
78
+ ```ruby
79
+ location: '~/'
80
+ filename: '.confoog'
81
+ create_file: false
82
+ prefix: 'Configuration'
83
+ quiet: false
84
+ ```
85
+
86
+ Confoog will set the following error constants which will be returned in the `.status['errors']` variable as needed :
87
+ ```ruby
88
+ ERR_NO_ERROR = 0 # no error condition, command was succesfull
89
+ ERR_FILE_NOT_EXIST = 1 # specified configuration file does not exist
90
+ ERR_CANT_CHANGE = 2 # directory and file can only be specified through `.new()`
91
+ ERR_CANT_CREATE_FILE = 4 # cannot create the requested configuration file
92
+ ERR_NOT_WRITING_EMPTY_FILE = 8 # not attempting to save an empty configuration
93
+ ERR_CANT_SAVE_CONFIGURATION = 16 # Failed to save the configuration file
94
+ ERR_NOT_LOADING_EMPTY_FILE = 32 # not atempting to load an empty config file
95
+
96
+ INFO_FILE_CREATED = 256 # Information - specified file was created
97
+ INFO_FILE_LOADED = 512 # Information - Config file was loaded successfully
98
+ ```
99
+ These are generally to do with existence and creation of configuration files.
100
+
101
+ ## To Do
102
+
103
+ Thoughts in no particular order.
104
+
105
+ - Restrict configuration variables to a specified subset, or to only those that already exist in the YAML file.
106
+ - A better way of dealing with multi-level variables - i.e. nested arrays, hashes etc.
107
+ - option to save config file after any config variables are changed, not just explicitly with `Confoog::Settings.save`
108
+
109
+ ## Development
110
+
111
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests (or simply `rake`). You can also run `bin/console` for an interactive prompt that will allow you to experiment.
112
+
113
+ To install this gem onto your local machine, run `bundle exec rake install`.
114
+
115
+ ## Contributing
116
+
117
+ 1. Fork it
118
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
119
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
120
+ 4. Push to the branch (`git push origin my-new-feature`)
121
+ 5. Create new Pull Request
122
+
123
+ ## Versioning
124
+
125
+ This library aims to adhere to [Semantic Versioning 2.0.0][semver]. Violations
126
+ of this scheme should be reported as bugs. Specifically, if a minor or patch
127
+ version is released that breaks backward compatibility, that version should be
128
+ immediately yanked and/or a new version should be immediately released that
129
+ restores compatibility. Breaking changes to the public API will only be
130
+ introduced with new major versions. As a result of this policy, you can (and
131
+ should) specify a dependency on this gem using the [Pessimistic Version
132
+ Constraint][pvc] with two digits of precision. For example:
133
+
134
+ spec.add_dependency 'confoog', '~> 1.0'
135
+
136
+ Of course, currently we have not even reached version 1, so leave off the version requirement completely. Expect any and all of the API and interface to change!
137
+
138
+ [semver]: http://semver.org/
139
+ [pvc]: http://guides.rubygems.org/patterns/#pessimistic-version-constraint
140
+
141
+ ## License
142
+
143
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+ require 'rubocop/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ RuboCop::RakeTask.new do |task|
7
+ task.options << 'lib'
8
+ end
9
+
10
+ task default: [:rubocop, :spec]
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'confoog'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ require 'pry'
11
+ Pry.start
12
+
13
+ # require "irb"
14
+ # IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/confoog.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ # rubocop:disable LineLength
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'confoog/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'confoog'
9
+ spec.version = Confoog::VERSION
10
+ spec.authors = ['Seapagan']
11
+ spec.email = ['seapagan@gmail.com']
12
+
13
+ spec.summary = 'A simple Gem to add YAML configuration files to your Ruby script / Gem'
14
+ spec.description = 'A WORK IN PROGRESS, Nowhere near ready for use, and if fact currently will neither load nor save the configuration to file!'
15
+ spec.homepage = 'https://github.com/seapagan/confoog'
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ spec.bindir = 'exe'
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.10'
24
+ spec.add_development_dependency 'rake', '~> 10.0'
25
+ spec.add_development_dependency 'rspec'
26
+ spec.add_development_dependency 'pry'
27
+ spec.add_development_dependency 'fakefs'
28
+ spec.add_development_dependency 'coveralls'
29
+ spec.add_development_dependency 'rubocop'
30
+ end
@@ -0,0 +1,4 @@
1
+ # Define the Gem version string
2
+ module Confoog
3
+ VERSION = '0.1.1'
4
+ end
data/lib/confoog.rb ADDED
@@ -0,0 +1,169 @@
1
+ require 'confoog/version'
2
+ require 'yaml'
3
+
4
+ # Overall module.
5
+ # Contains Class Confoog::Settings
6
+ module Confoog
7
+ DEFAULT_CONFIG = '.confoog'
8
+
9
+ # Error messages to be returned
10
+ ERR_NO_ERROR = 0
11
+ ERR_FILE_NOT_EXIST = 1
12
+ ERR_CANT_CHANGE = 2
13
+ ERR_CANT_CREATE_FILE = 4
14
+ ERR_NOT_WRITING_EMPTY_FILE = 8
15
+ ERR_CANT_SAVE_CONFIGURATION = 16
16
+ ERR_NOT_LOADING_EMPTY_FILE = 32
17
+
18
+ # Info messages to be returned
19
+ INFO_FILE_CREATED = 256
20
+ INFO_FILE_LOADED = 512
21
+
22
+ OUTPUT_SEVERITY = {
23
+ ERR: 'Error',
24
+ WARN: 'Warning',
25
+ INFO: 'Information'
26
+ }
27
+
28
+ DEFAULT_OPTIONS = {
29
+ create_file: false,
30
+ quiet: false,
31
+ prefix: 'Configuration',
32
+ location: '~/',
33
+ filename: DEFAULT_CONFIG
34
+ }
35
+
36
+ # Class : Settings.
37
+ # Provide an encapsulated class to access a YAML configuration file.
38
+ # Readers :
39
+ # .filename = read the config filename for this instance
40
+ # .location = read the config directory for this instance
41
+ # .status = Hash containing assorted status variables, read only.
42
+ class Settings
43
+ attr_reader :filename, :location, :status
44
+
45
+ def initialize(options = {})
46
+ # merge default options to avoid ambiguity
47
+ @options = DEFAULT_OPTIONS.merge(options)
48
+ # set all other unset options to return false instead of Nul.
49
+ @options.default = false
50
+
51
+ @status = {}
52
+ @location = @options[:location]
53
+ @filename = @options[:filename]
54
+
55
+ @config = {}
56
+
57
+ # clear the error condition as default.
58
+ status_set(errors: ERR_NO_ERROR)
59
+ # make sure the file exists or can be created...
60
+ check_exists(options)
61
+ end
62
+
63
+ def quiet
64
+ @options[:quiet]
65
+ end
66
+
67
+ def quiet=(quiet)
68
+ @options[:quiet] = quiet
69
+ end
70
+
71
+ def save
72
+ if @config.count > 0
73
+ save_to_yaml
74
+ else
75
+ console_output("Not saving empty configuration data to #{config_path}",
76
+ OUTPUT_SEVERITY[:WARN])
77
+ status_set(errors: ERR_NOT_WRITING_EMPTY_FILE)
78
+ end
79
+ end
80
+
81
+ def load
82
+ @config = YAML.load_file(config_path)
83
+ status_set(errors: INFO_FILE_LOADED)
84
+ if @config == false
85
+ console_output("Configuration file #{config_path} is empty!",
86
+ OUTPUT_SEVERITY[:WARN])
87
+ status_set(errors: ERR_NOT_LOADING_EMPTY_FILE)
88
+ end
89
+ rescue
90
+ console_output("Cannot load configuration data from #{config_path}",
91
+ OUTPUT_SEVERITY[:ERR])
92
+ end
93
+
94
+ def location=(*)
95
+ # dummy method currently to stop changing location by caller once created,
96
+ # but not raise error.
97
+ # - Return an error flag in the ':status' variable.
98
+ status_set(errors: ERR_CANT_CHANGE)
99
+ console_output('Cannot change file location after creation',
100
+ OUTPUT_SEVERITY[:WARN])
101
+ end
102
+
103
+ def filename=(*)
104
+ # dummy method currently to stop changing filename by caller once created,
105
+ # but not raise error.
106
+ # - Return an error flag in the ':status' variable.
107
+ status_set(errors: ERR_CANT_CHANGE)
108
+ console_output('Cannot change filename after creation',
109
+ OUTPUT_SEVERITY[:WARN])
110
+ end
111
+
112
+ def [](key)
113
+ @config[key]
114
+ end
115
+
116
+ def []=(key, value)
117
+ @config[key] = value
118
+ end
119
+
120
+ def config_path
121
+ File.expand_path(File.join(@location, @filename))
122
+ end
123
+
124
+ private
125
+
126
+ def console_output(message, severity)
127
+ return unless @options[:quiet] == false
128
+ $stderr.puts "#{@options[:prefix]} : #{severity} - #{message}"
129
+ end
130
+
131
+ def save_to_yaml
132
+ file = File.open(config_path, 'w')
133
+ file.write(@config.to_yaml)
134
+ file.close
135
+ rescue
136
+ console_output("Cannot save configuration data to #{config_path}",
137
+ OUTPUT_SEVERITY[:ERR])
138
+ end
139
+
140
+ def create_new_file
141
+ File.new(config_path, 'w').close
142
+ status_set(config_exists: true, errors: INFO_FILE_CREATED)
143
+ rescue
144
+ status_set(config_exists: false, errors: ERR_CANT_CREATE_FILE)
145
+ console_output('Cannot create the specified Configuration file!',
146
+ OUTPUT_SEVERITY[:ERR])
147
+ end
148
+
149
+ def status_set(status)
150
+ status.each do |key, value|
151
+ @status[key] = value
152
+ end
153
+ end
154
+
155
+ def check_exists(options)
156
+ status_set(config_exists: true)
157
+ return if File.exist?(config_path)
158
+
159
+ # file does not exist so we create if requested otherwise error out
160
+ if options[:create_file] == true
161
+ create_new_file
162
+ else
163
+ status_set(config_exists: false, errors: ERR_FILE_NOT_EXIST)
164
+ console_output('The specified Configuration file does not exist.',
165
+ OUTPUT_SEVERITY[:ERR])
166
+ end
167
+ end
168
+ end
169
+ end
metadata ADDED
@@ -0,0 +1,156 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: confoog
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Seapagan
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-09-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: fakefs
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: coveralls
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: A WORK IN PROGRESS, Nowhere near ready for use, and if fact currently
112
+ will neither load nor save the configuration to file!
113
+ email:
114
+ - seapagan@gmail.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".gitignore"
120
+ - ".rspec"
121
+ - ".travis.yml"
122
+ - Gemfile
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - bin/console
127
+ - bin/setup
128
+ - confoog.gemspec
129
+ - lib/confoog.rb
130
+ - lib/confoog/version.rb
131
+ homepage: https://github.com/seapagan/confoog
132
+ licenses:
133
+ - MIT
134
+ metadata: {}
135
+ post_install_message:
136
+ rdoc_options: []
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ requirements: []
150
+ rubyforge_project:
151
+ rubygems_version: 2.4.8
152
+ signing_key:
153
+ specification_version: 4
154
+ summary: A simple Gem to add YAML configuration files to your Ruby script / Gem
155
+ test_files: []
156
+ has_rdoc: