conjuror 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0aa02f3cede8bfff756c334f16221bc9201156cf
4
+ data.tar.gz: 9117723bfe0d7f5f56006c880014457f899ee052
5
+ SHA512:
6
+ metadata.gz: a31d33259b0beb8b739339c880c3dfa7de39a0933c1c4c5b23460f21520fcdc33250834b65d62c4dbc71d5dc91256177eb110349ae213d6b1fc003604e6a2245
7
+ data.tar.gz: f6a29d64b9db00fc53793b9c88fda9fca8fd0287e8c7401f2b8724c140f1d3a12e6657e14b5a520b9e2fa3d53bc76f3c9316f5826a6647b6844fa00fe1c8540b
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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.0
4
+ - 2.0.0
5
+ - 1.9.3
6
+ - 1.8.7
7
+ - ruby-head
8
+ - jruby-18mode
9
+ - jruby-19mode
10
+ - jruby-head
11
+ - ree
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in conjuror.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Nikhil Gupta
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,58 @@
1
+ # Conjuror
2
+
3
+ Easily conjure configuration settings for your standalone ruby applications.
4
+ `Conjuror` creates configuration file for projects at a centralized place in the
5
+ user's home directory, and allow each project to have a unique and separate
6
+ configuration directory of itself.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'conjuror'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install conjuror
21
+
22
+ ## Usage
23
+
24
+ Let's say, that you are developing a gem with the name `awesome`, and you want
25
+ a configuration file for that project, well, you can do something like:
26
+
27
+ settings = Conjuror.new "awesome", "filename"
28
+ settings["opt"] = "value" # setting an option saves the file automatically
29
+ settings.path #=> ~/.conjuror/awesome/filename.yml
30
+
31
+ The above will create the `~/.conjuror/awesome` directory for you, along with
32
+ your configuration file (named `filename.yml`) for you. Second argument is
33
+ optional, and will create a file named `conf.yml` if you do not specify it.
34
+
35
+ Now, `Conjuror` uses the first file specified in the `initializer` as the
36
+ default, and starts with it. To create a new setting, you can do:
37
+
38
+ settings["option"] = "value" # saved instantly to the YAML config file
39
+ settings["option"] #=> value # easily access your settings
40
+ settings.option #=> value # allows access using 'dot' syntax
41
+
42
+ Note that, settings are persisted to the local configuration file on disk nearly
43
+ instantly.
44
+
45
+ You are, also, able to switch to different projects or configuration files,
46
+ easily like this:
47
+
48
+ settings.use "anotherproject" # switch to 'anotherproject#conf'
49
+ settings.use "awesome", "filename" # switch to 'awesome#filename'
50
+ settings.file = 'configuration' # switch to 'awesome#configuration'
51
+
52
+ ## Contributing
53
+
54
+ 1. Fork it
55
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
56
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
57
+ 4. Push to the branch (`git push origin my-new-feature`)
58
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/conjuror.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'conjuror/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "conjuror"
8
+ spec.version = Conjuror::VERSION
9
+ spec.authors = ["Nikhil Gupta"]
10
+ spec.email = ["me@nikhgupta.com"]
11
+ spec.description = %q{Easy Configuration for Standalone Ruby Scripts.}
12
+ spec.summary = %q{Easy Configuration for Standalone Ruby Scripts.}
13
+ spec.homepage = "http://github.com/nikhgupta/conjuror"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,72 @@
1
+ module Conjuror
2
+ class Settings
3
+
4
+ include Singleton
5
+
6
+ attr_reader :name, :file, :data
7
+
8
+ # Returns an instance of the class itself.
9
+ #
10
+ def use name = nil, file = nil
11
+ self.name = name
12
+ self.file = file
13
+ self
14
+ end
15
+
16
+ def file= file
17
+ @file = (file ? file.to_s : "conf")
18
+ @data = (YAML.load_file(self.path) rescue {})
19
+ end
20
+
21
+ def path
22
+ raise Conjuror::NoConfigurationLoadedError unless @name && @file
23
+ File.join(self.directory, @file + ".yml")
24
+ end
25
+
26
+ # def reload
27
+ # self.use @name, @file
28
+ # end
29
+
30
+ def [] key
31
+ @data[key]
32
+ end
33
+
34
+ # ensures that every new setting is first persisted to the file.
35
+ def []= key, value
36
+ @data[key] = value
37
+ save_configuration
38
+ value
39
+ end
40
+
41
+ def directory
42
+ self.class.directory_for(@name)
43
+ end
44
+
45
+ def method_missing *args
46
+ return @data[args.first.to_s] if @data && config_defined?(args.first.to_s)
47
+ return @data[args.first.to_sym] if @data && config_defined?(args.first.to_sym)
48
+ raise NoMethodError, "Found no such method."
49
+ end
50
+
51
+ def self.directory_for(name)
52
+ File.join(ENV['HOME'], ".conjuror", name.to_s.gsub(/^\.+/, ''))
53
+ end
54
+
55
+ private
56
+
57
+ def name= name
58
+ @name = name
59
+ FileUtils.mkdir_p self.directory unless File.directory? self.directory
60
+ end
61
+
62
+ def save_configuration
63
+ File.open(self.path, "w") { |f| f.puts @data.to_yaml }
64
+ @data
65
+ end
66
+
67
+ def config_defined? key
68
+ @data.has_key? key
69
+ end
70
+
71
+ end
72
+ end
@@ -0,0 +1,3 @@
1
+ module Conjuror
2
+ VERSION = "0.1.0"
3
+ end
data/lib/conjuror.rb ADDED
@@ -0,0 +1,19 @@
1
+ require 'yaml'
2
+ require 'fileutils'
3
+ require 'singleton'
4
+ require 'rubygems'
5
+ require "conjuror/version"
6
+ require "conjuror/settings"
7
+
8
+ module Conjuror
9
+
10
+ class NoConfigurationLoadedError < StandardError
11
+ def initialize message = "You must call 'use' to load the appropriate configuration."
12
+ super
13
+ end
14
+ end
15
+
16
+ def self.new name = nil, file = nil
17
+ Settings.instance.use name, file
18
+ end
19
+ end
@@ -0,0 +1,121 @@
1
+ require 'spec_helper'
2
+
3
+ describe Conjuror do
4
+
5
+ before(:each) do
6
+ [ "conjuror", "randomname" ].each do |name|
7
+ FileUtils.rm_rf Conjuror::Settings.directory_for(name)
8
+ end
9
+ end
10
+
11
+ it 'has a version number' do
12
+ expect(Conjuror::VERSION).not_to be_nil
13
+ end
14
+
15
+ describe "#new" do
16
+ subject { Conjuror.new }
17
+
18
+ it 'creates an instance of Settings class' do
19
+ result = subject.is_a?(Conjuror::Settings)
20
+ expect(result).to be_true
21
+ end
22
+
23
+ it 'does not create another instance of itself' do
24
+ result = subject === Conjuror.new
25
+ expect(result).to be_true
26
+ end
27
+ end
28
+ end
29
+
30
+ describe Conjuror::Settings do
31
+
32
+ before(:each) do
33
+ [ "conjuror", "randomname" ].each do |name|
34
+ FileUtils.rm_rf Conjuror::Settings.directory_for(name)
35
+ end
36
+ Singleton.__init__(Conjuror::Settings)
37
+ end
38
+
39
+ subject(:settings) { Conjuror::Settings.instance }
40
+
41
+ describe "#new" do
42
+ it 'raises an error when an attempt to create an instance is made' do
43
+ expect { Conjuror::Settings.new }.to raise_error(NoMethodError)
44
+ end
45
+ end
46
+
47
+ describe "#path" do
48
+ it 'raises an error when configuration was accessed but not loaded' do
49
+ expect { settings.path }.to raise_error
50
+ end
51
+ end
52
+
53
+ describe "#use" do
54
+ it 'creates a configuration directory when properly loaded' do
55
+ directory = settings.use("conjuror").directory
56
+
57
+ expect(File.basename(directory)).to eq("conjuror")
58
+ expect(directory).to eq(File.expand_path("~/.conjuror/conjuror"))
59
+ expect(File.directory?(directory)).to be_true
60
+ end
61
+
62
+ it 'chooses a default configuration file if none specified' do
63
+ settings.use("conjuror")
64
+ settings["option"] = "value" # triggers the saving of config file
65
+
66
+ expect(settings.file).to eq("conf")
67
+ expect(File.file?(settings.path)).to be_true
68
+ end
69
+
70
+ it "returns an instance of itself" do
71
+ result = settings.use("conjuror") === settings
72
+ expect(result).to be_true
73
+ end
74
+ end
75
+
76
+ describe "#[], #[]=" do
77
+ it "allows easy access and creation of new settings" do
78
+ settings.use("conjuror")
79
+ expect(settings["option"]).to be_nil
80
+
81
+ settings["option"] = "value"
82
+ expect(settings["option"]).to eq("value")
83
+ expect(settings.option).to eq("value")
84
+ end
85
+
86
+ it 'saves the new settings to the disk' do
87
+ settings.use("conjuror")
88
+ settings["option"] = "value"
89
+
90
+ result = YAML.load_file(settings.path)["option"]
91
+ expect(result).to eq("value")
92
+ end
93
+ end
94
+
95
+ describe "#file=" do
96
+ it "allows switching to a different configuration files and projects" do
97
+ settings.use "conjuror" # use: conjuror, conf
98
+ settings["option"] = "value1"
99
+ settings.file = "otherfile" # use: conjuror, otherfile
100
+ settings["option"] = "value2"
101
+
102
+ settings.use "randomname" # use: randomname, conf
103
+ settings["option"] = "value3"
104
+ settings.file = "otherfile" # use: randomname, otherfile
105
+ settings["option"] = "value4"
106
+
107
+ settings.file = "conf" # use: randomname, "conf"
108
+ expect(settings.option).to eq("value3")
109
+
110
+ settings.use "conjuror", "otherfile" # use: conjuror, otherfile
111
+ expect(settings.option).to eq("value2")
112
+
113
+ settings.file = nil # use: conjuror, "conf"
114
+ expect(settings.option).to eq("value1")
115
+
116
+ settings.use "randomname", "otherfile" # use: randomname, otherfile
117
+ expect(settings.option).to eq("value4")
118
+ end
119
+ end
120
+
121
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'conjuror'
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: conjuror
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nikhil Gupta
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-07 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '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
+ description: Easy Configuration for Standalone Ruby Scripts.
56
+ email:
57
+ - me@nikhgupta.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .rspec
64
+ - .travis.yml
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - conjuror.gemspec
70
+ - lib/conjuror.rb
71
+ - lib/conjuror/settings.rb
72
+ - lib/conjuror/version.rb
73
+ - spec/conjuror_spec.rb
74
+ - spec/spec_helper.rb
75
+ homepage: http://github.com/nikhgupta/conjuror
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.0.3
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Easy Configuration for Standalone Ruby Scripts.
99
+ test_files:
100
+ - spec/conjuror_spec.rb
101
+ - spec/spec_helper.rb