Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickTulskie committed Sep 11, 2012
0 parents commit cf20812
Show file tree
Hide file tree
Showing 13 changed files with 296 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
*.gem
*.rbc
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
doc/
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in configster.gemspec
gemspec
22 changes: 22 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2012 Patrick Tulskie

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
82 changes: 82 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Configster

Configster is a tidy little configuration management utility for your application.

You probably haven't heard of it.

## Installation

Add this line to your application's Gemfile:

gem 'configster'

And then execute:

$ bundle

Or install it yourself as:

$ gem install configster

## Usage

For most applications, you'll want to load Configster before you start loading the rest of your application. For example:

require 'configster'
Configster.load_configster!(File.join('path', 'to', 'my_config.yml'))

You can also pass a raw hash of stuff into `load_configster!` as long as it's in the format of `"ClassName" => { 'variable' => 'whatever' }`

Then, just include Configster in any classes you want:

class KonfiguredKlass
include Configster
end

Your class now has access to a `configster` and `raw_configster` function where you can query bits and pieces of your configuration.

## Example

Say you're writing a sweet application that uses [HTTParty](https://github.com/jnunemaker/httparty) to fetch data from Twitter. You want to share this application with the world, but you don't want to hard code your credentials. Here's how you might handle that:

First, make yourself a nice little configuration file and save it to `~/my_sweet_app_credentials.yml`:

MySweetApp:
username: SweetUsername
password: SweetPassword123
Then, you'll write your application like this:

require 'configster'
Configster.load_configster!('~/my_sweet_app_credentials.yml')

class MySweetApp

include Configster
include HTTParty
def initialize
@auth = {
:username => configster.username,
:password => configster.password
}
end
def timeline(which = :friends, options = { })
options.merge!(:basic_auth => @auth)
self.class.get("/statuses/#{which}_timeline.json", options)
end
end

Now you can share your application without hard coding and/or sharing your credentials.


## Contributing

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Write specs for your changes to make sure I don't break your features in the future.
4. Commit your changes (`git commit -am 'Add some feature'`)
5. Push to the branch (`git push origin my-new-feature`)
6. Create new Pull Request
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require "bundler/gem_tasks"
22 changes: 22 additions & 0 deletions configster.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- encoding: utf-8 -*-
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'configster/version'

Gem::Specification.new do |gem|
gem.name = "configster"
gem.version = Configster::VERSION
gem.authors = ["Patrick Tulskie"]
gem.email = ["patricktulskie@gmail.com"]
gem.description = %q{TODO: Write a gem description}
gem.summary = %q{TODO: Write a gem summary}
gem.homepage = ""

gem.files = `git ls-files`.split($/)
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.require_paths = ["lib"]

gem.add_development_dependency('rspec')

end
5 changes: 5 additions & 0 deletions lib/configster.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'yaml'
require 'ostruct'

require "configster/version"
require "configster/configster"
52 changes: 52 additions & 0 deletions lib/configster/configster.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
module Configster

# =========================================
# = Class Methods for the Receiving Class =
# =========================================
module ClassMethods
def configster
@configster ||= Configster.configuration_for(self)
end

def raw_configster
Configster.raw_configuration_for(self)
end
end

def self.included(base)
base.extend(ClassMethods)
end

# ============================================
# = Instance Methods for the Receiving Class =
# ============================================
def configster
self.class.configster
end

def raw_configster
self.class.raw_configster
end

# ===========================
# = Core Configster Methods =
# ===========================
def self.load_configster!(configster_config)
@configster_config = case configster_config
when Hash
configster_config
when String
YAML.load_file(configster_config)
end
end

def self.configuration_for(klass)
klass_config = @configster_config[klass.to_s]
klass_config ? OpenStruct.new(klass_config).freeze : nil
end

def self.raw_configuration_for(klass)
@configster_config[klass.to_s]
end

end
3 changes: 3 additions & 0 deletions lib/configster/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Configster
VERSION = "0.0.1"
end
12 changes: 12 additions & 0 deletions spec/configster_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require "spec_helper"

describe Configster do

subject { Configster }
it { should respond_to(:configuration_for) }

it "should be able to load the configuration for a specific class" do
Configster.configuration_for(KonfiguredKlass).should_not be_nil
end

end
8 changes: 8 additions & 0 deletions spec/configurations/test_configuration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
KonfiguredKlass:
user_name: configster
password: c0nfigster!
tag_line: "You've probably never heard of it."
AnotherKlass:
user_name: another
password: weakpassword
tag_line: "Whatever, bro."
31 changes: 31 additions & 0 deletions spec/konfigured_klass_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require "spec_helper"

describe KonfiguredKlass do

before(:all) do
@test_klass = KonfiguredKlass.new
end

subject { @test_klass }
it { should respond_to(:configster) }

it "should be able to fetch attributes from the configuration" do
@test_klass.configster.should respond_to(:user_name)
@test_klass.configster.should respond_to(:password)
@test_klass.configster.should respond_to(:tag_line)
end

it "should be frozen" do
@test_klass.configster.should be_frozen
end

it "should not pollute other configurations" do
another_klass = AnotherKlass.new
@test_klass.configster.user_name.should_not == another_klass.configster.user_name
end

it "should be able to load a raw configuration" do
@test_klass.raw_configster.should be_a(Hash)
end

end
37 changes: 37 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# This file is copied to ~/spec when you run 'ruby script/generate rspec'
# from the project root directory.

require 'rubygems'
require 'rspec'
require 'configster'

$spec_root = File.dirname(__FILE__)

RSpec.configure do |config|
old_verbose, $VERBOSE = $VERBOSE, nil

def puts(s)
file = File.basename(caller.first)
super("puts() from #{file}: #{s}")
end

def print(s)
file = File.basename(caller.first)
super("print() from #{file}: #{s}")
end

def p(s)
file = File.basename(caller.first)
super("p() from #{file}: #{s}")
end
end

Configster.load_configster!(File.join($spec_root, 'configurations', 'test_configuration.yml'))

class KonfiguredKlass
include Configster
end

class AnotherKlass
include Configster
end

0 comments on commit cf20812

Please sign in to comment.