app_config 0.2.1 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: app_config
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dale Campbell
@@ -9,51 +9,28 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-03-15 00:00:00 -05:00
12
+ date: 2009-09-27 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
16
- description: An easy to use, customizable library to easily store and retrieve application configuration
16
+ description: An easy to use, customizable library to easily store and retrieve application configuration.
17
17
  email:
18
- - dale@save-state.net
18
+ - oshuma@gmail.com
19
19
  executables: []
20
20
 
21
21
  extensions: []
22
22
 
23
23
  extra_rdoc_files: []
24
24
 
25
- files:
26
- - README
27
- - Rakefile
28
- - lib/app_config/base.rb
29
- - lib/app_config/storage/sqlite.rb
30
- - lib/app_config/storage/yaml.rb
31
- - lib/app_config/storage.rb
32
- - lib/app_config.rb
33
- - lib/core_ext/hashish.rb
34
- - spec/app_config
35
- - spec/app_config/base_spec.rb
36
- - spec/app_config/storage
37
- - spec/app_config/storage/sqlite_spec.rb
38
- - spec/app_config/storage/yaml_spec.rb
39
- - spec/app_config_spec.rb
40
- - spec/core_ext
41
- - spec/core_ext/hashish_spec.rb
42
- - spec/fixtures
43
- - spec/fixtures/app_config.sqlite3
44
- - spec/fixtures/app_config.yml
45
- - spec/rcov.opts
46
- - spec/spec.opts
47
- - spec/spec_helper.rb
25
+ files: []
26
+
48
27
  has_rdoc: true
49
28
  homepage: http://oshuma.github.com/app_config
29
+ licenses: []
30
+
50
31
  post_install_message:
51
- rdoc_options:
52
- - --title
53
- - AppConfig
54
- - --all
55
- - --inline-source
56
- - --line-numbers
32
+ rdoc_options: []
33
+
57
34
  require_paths:
58
35
  - lib
59
36
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -71,9 +48,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
71
48
  requirements: []
72
49
 
73
50
  rubyforge_project: app-config
74
- rubygems_version: 1.3.1
51
+ rubygems_version: 1.3.5
75
52
  signing_key:
76
- specification_version: 2
53
+ specification_version: 3
77
54
  summary: Quick and easy application configuration.
78
55
  test_files: []
79
56
 
data/README DELETED
@@ -1,40 +0,0 @@
1
- = AppConfig
2
-
3
- An easy to use, customizable library to easily store and retrieve application
4
- configuration, API keys or basically anything in 'key => value' pairs.
5
-
6
-
7
- == Usage
8
-
9
- Usage is simple. Just pass either a hash of options, or a block, to
10
- AppConfig.setup. See AppConfig::Base for a list of valid storage methods.
11
-
12
- For example, given this YAML file:
13
-
14
- ---
15
- admin_email: 'admin@example.com'
16
- api_name: 'Supr Webz 2.0'
17
- api_key: 'SUPERAWESOMESERVICE'
18
-
19
- Use it like so:
20
-
21
- require 'app_config'
22
-
23
- AppConfig.setup do |config|
24
- config[:storage_method] = :yaml
25
- config[:path] = '/path/to/app_config.yml'
26
- end
27
-
28
- AppConfig[admin_email] # => 'admin@example.com'
29
- AppConfig[:api_name] # => 'Supr Webz 2.0'
30
- AppConfig[:api_key] # => 'SUPERAWESOMESERVICE'
31
-
32
- Want SQLite3 support? No problem!
33
-
34
- AppConfig.setup do |config|
35
- config[:storage_method] = :sqlite
36
- config[:database] = '/path/to/database.sqlite3'
37
- config[:table] = 'app_config'
38
- end
39
-
40
- AppConfig[:column] # => 'value'
data/Rakefile DELETED
@@ -1,10 +0,0 @@
1
- require 'spec/rake/spectask'
2
-
3
- FileList[File.dirname(__FILE__) + '/tasks/**/*.rake'].each { |task| load task }
4
-
5
- task :default => [:spec]
6
-
7
- desc 'Start an irb session with AppConfig loaded'
8
- task :console do
9
- sh "irb -I ./lib -r 'app_config'"
10
- end
@@ -1,41 +0,0 @@
1
- $LOAD_PATH.unshift File.dirname(__FILE__)
2
-
3
- libs = %w{ sqlite3 yaml }
4
-
5
- begin
6
- libs.each { |lib| require lib }
7
- rescue LoadError
8
- require 'rubygems'
9
- libs.each { |lib| require lib }
10
- end
11
-
12
- require 'core_ext/hashish'
13
-
14
- # TODO: Move these to their own file.
15
- class UnknownStorageMethod < Exception; end
16
-
17
- module AppConfig
18
- VERSION = '0.2.1'
19
-
20
- autoload :Base, 'app_config/base'
21
- autoload :Storage, 'app_config/storage'
22
-
23
- # Returns the AppConfig version string.
24
- def self.to_version
25
- "#{self.class} v#{VERSION}"
26
- end
27
-
28
- # Access the configured <tt>key</tt>'s value.
29
- def self.[](key)
30
- error = "Must call '#{self}.configure' to setup storage!"
31
- raise error if @@storage.nil?
32
- @@storage[key]
33
- end
34
-
35
- # Accepts an +options+ hash or a block.
36
- # See AppConfig::Base for valid storage methods.
37
- def self.setup(options = {}, &block)
38
- @@storage = AppConfig::Base.new(options, &block)
39
- end
40
-
41
- end
@@ -1,46 +0,0 @@
1
- module AppConfig
2
-
3
- # The Base storage class.
4
- # Acts as a wrapper for the different storage methods.
5
- #
6
- # See each storage method's documentation for their specific options.
7
- class Base
8
-
9
- DEFAULTS = {
10
- :storage_method => :yaml
11
- }
12
-
13
- # Accepts either a hash of +options+ or a block (which overrides
14
- # any options passed in the hash).
15
- #
16
- # Valid storage methods:
17
- # * :sqlite
18
- # * :yaml
19
- def initialize(options = {}, &block)
20
- @options = DEFAULTS.merge(options)
21
- yield @options if block_given?
22
- @storage = initialize_storage
23
- end
24
-
25
- # Access the <tt>key</tt>'s value in @storage.
26
- def [](key)
27
- @storage[key]
28
- end
29
-
30
- private
31
-
32
- # This decides how to load the data, based on the +storage_method+.
33
- def initialize_storage
34
- case @options[:storage_method]
35
- when :sqlite
36
- AppConfig::Storage::Sqlite.load(@options)
37
- when :yaml
38
- AppConfig::Storage::Yaml.load(@options)
39
- else
40
- raise UnknownStorageMethod
41
- end
42
- end
43
-
44
- end # Base
45
-
46
- end # AppConfig
@@ -1,6 +0,0 @@
1
- module AppConfig
2
- module Storage
3
- autoload :Sqlite, 'app_config/storage/sqlite'
4
- autoload :Yaml, 'app_config/storage/yaml'
5
- end
6
- end
@@ -1,61 +0,0 @@
1
- module AppConfig
2
- module Storage
3
-
4
- # SQLite3 storage method.
5
- class Sqlite
6
- attr_accessor :data
7
-
8
- DEFAULTS = {
9
- :database => File.expand_path(File.join(ENV['HOME'], '.app_config.sqlite3')),
10
- :table => 'app_config'
11
- }
12
-
13
- # Loads @data with the SQLite3 database located at +path+.
14
- # @data will be the Hashish that is accessed like AppConfig[:key].
15
- #
16
- # Defaults to $HOME/.app_config.sqlite3
17
- def initialize(options)
18
- @options = DEFAULTS.merge(options)
19
- @db = SQLite3::Database.open(@options[:database])
20
- @data = load_from_database
21
- end
22
-
23
- # Creates a new Sqlite storage with the given +path+ and returns the data.
24
- def self.load(path)
25
- new(path).data
26
- end
27
-
28
- private
29
-
30
- # Returns a Hashish that looks something like {:column => value}.
31
- def load_from_database
32
- query = ["SELECT * FROM sqlite_master",
33
- "WHERE type == 'table' AND name == '#{@options[:table]}'",
34
- "AND name != 'sqlite_sequence'"].join(' ')
35
- table = @db.get_first_row(query).last
36
- get_columns_from(table)
37
- end
38
-
39
- # Gets the column names for +table+.
40
- def get_columns_from(table)
41
- columns = table.split(', ')
42
- # Trip the first element, since it's the SQL CREATE statement.
43
- columns = columns[1, columns.size]
44
- # Yes, Ruby is 'elegant', but this is fucking disgusting. There *must* be a better way.
45
- columns.map! {|c| c.split('"').reject {|e| e.empty? }.reject {|e| e.include? '('}}.flatten!
46
- values_for(columns)
47
- end
48
-
49
- # Returns a Hashish mapping of the +columns+ and their values.
50
- def values_for(columns)
51
- data = Hashish.new
52
- query = "SELECT #{columns.join(', ')} FROM #{@options[:table]}"
53
- @db.get_first_row(query).each_with_index do |value, index|
54
- data[columns[index]] = value
55
- end
56
- data
57
- end
58
- end # Sqlite
59
-
60
- end # Storage
61
- end # AppConfig
@@ -1,29 +0,0 @@
1
- module AppConfig
2
- module Storage
3
-
4
- # YAML storage method.
5
- class Yaml
6
- attr_reader :data
7
-
8
- DEFAULTS = {
9
- :path => File.expand_path(File.join(ENV['HOME'], '.app_config.yml'))
10
- }
11
-
12
- # Loads @data with the YAML file located at +path+.
13
- # @data will be the Hashish that is accessed with AppConfig[:key].
14
- #
15
- # Defaults to $HOME/.app_config.yml
16
- def initialize(options)
17
- path = options[:path] || DEFAULTS[:path]
18
- @data = Hashish.new(YAML.load_file(path))
19
- end
20
-
21
- # Creates a new Yaml storage with the given +path+ and returns the data.
22
- def self.load(path)
23
- new(path).data
24
- end
25
-
26
- end # Yaml
27
-
28
- end # Storage
29
- end # AppConfig
@@ -1,124 +0,0 @@
1
- # Stolen from Rails Active Support 2.3.0 and renamed to Hashish.
2
- #
3
- # This class has dubious semantics and we only have it so that
4
- # people can write params[:key] instead of params['key']
5
- # and they get the same value for both keys.
6
- class Hashish < Hash
7
- def initialize(constructor = {})
8
- if constructor.is_a?(Hash)
9
- super()
10
- update(constructor)
11
- else
12
- super(constructor)
13
- end
14
- end
15
-
16
- def default(key = nil)
17
- if key.is_a?(Symbol) && include?(key = key.to_s)
18
- self[key]
19
- else
20
- super
21
- end
22
- end
23
-
24
- alias_method :regular_writer, :[]= unless method_defined?(:regular_writer)
25
- alias_method :regular_update, :update unless method_defined?(:regular_update)
26
-
27
- # Assigns a new value to the hash:
28
- #
29
- # hash = HashWithIndifferentAccess.new
30
- # hash[:key] = "value"
31
- #
32
- def []=(key, value)
33
- regular_writer(convert_key(key), convert_value(value))
34
- end
35
-
36
- # Updates the instantized hash with values from the second:
37
- #
38
- # hash_1 = HashWithIndifferentAccess.new
39
- # hash_1[:key] = "value"
40
- #
41
- # hash_2 = HashWithIndifferentAccess.new
42
- # hash_2[:key] = "New Value!"
43
- #
44
- # hash_1.update(hash_2) # => {"key"=>"New Value!"}
45
- #
46
- def update(other_hash)
47
- other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) }
48
- self
49
- end
50
-
51
- alias_method :merge!, :update
52
-
53
- # Checks the hash for a key matching the argument passed in:
54
- #
55
- # hash = HashWithIndifferentAccess.new
56
- # hash["key"] = "value"
57
- # hash.key? :key # => true
58
- # hash.key? "key" # => true
59
- #
60
- def key?(key)
61
- super(convert_key(key))
62
- end
63
-
64
- alias_method :include?, :key?
65
- alias_method :has_key?, :key?
66
- alias_method :member?, :key?
67
-
68
- # Fetches the value for the specified key, same as doing hash[key]
69
- def fetch(key, *extras)
70
- super(convert_key(key), *extras)
71
- end
72
-
73
- # Returns an array of the values at the specified indices:
74
- #
75
- # hash = HashWithIndifferentAccess.new
76
- # hash[:a] = "x"
77
- # hash[:b] = "y"
78
- # hash.values_at("a", "b") # => ["x", "y"]
79
- #
80
- def values_at(*indices)
81
- indices.collect {|key| self[convert_key(key)]}
82
- end
83
-
84
- # Returns an exact copy of the hash.
85
- def dup
86
- HashWithIndifferentAccess.new(self)
87
- end
88
-
89
- # Merges the instantized and the specified hashes together, giving precedence to the values from the second hash
90
- # Does not overwrite the existing hash.
91
- def merge(hash)
92
- self.dup.update(hash)
93
- end
94
-
95
- # Removes a specified key from the hash.
96
- def delete(key)
97
- super(convert_key(key))
98
- end
99
-
100
- def stringify_keys!; self end
101
- def symbolize_keys!; self end
102
- def to_options!; self end
103
-
104
- # Convert to a Hash with String keys.
105
- def to_hash
106
- Hash.new(default).merge(self)
107
- end
108
-
109
- protected
110
- def convert_key(key)
111
- key.kind_of?(Symbol) ? key.to_s : key
112
- end
113
-
114
- def convert_value(value)
115
- case value
116
- when Hash
117
- value.with_indifferent_access
118
- when Array
119
- value.collect { |e| e.is_a?(Hash) ? e.with_indifferent_access : e }
120
- else
121
- value
122
- end
123
- end
124
- end
@@ -1,19 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
-
3
- describe Base do
4
-
5
- it 'should raise UnknownStorageMethod' do
6
- lambda do
7
- Base.new(:storage_method => 'not_a_real_storage_method')
8
- end.should raise_error(UnknownStorageMethod)
9
- end
10
-
11
- it 'should have default options' do
12
- default_path = File.expand_path(File.join(ENV['HOME'], '.app_config.yml'))
13
- # mock up the YAML stuff, so it won't puke
14
- YAML.should_receive(:load_file).with(default_path).and_return({:api => 'key'})
15
- base = Base.new
16
- base.instance_variable_get(:@options)[:storage_method].should == :yaml
17
- end
18
-
19
- end
@@ -1,15 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
-
3
- include AppConfig::Storage
4
- describe Sqlite do
5
- it 'should not find the database' do
6
- lambda do
7
- config_for_sqlite(:database => 'not/a/real/database.sqlite3')
8
- end.should raise_error(SQLite3::CantOpenException)
9
- end
10
-
11
- it 'should have some values' do
12
- config_for_sqlite
13
- AppConfig[:api_key].should_not be_nil
14
- end
15
- end
@@ -1,17 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
-
3
- include AppConfig::Storage
4
- describe Yaml do
5
-
6
- it 'should have some values' do
7
- config_for_yaml
8
- AppConfig[:api_key].should_not be_nil
9
- end
10
-
11
- it 'should raise file not found' do
12
- lambda do
13
- config_for_yaml(:path => 'not/a/real/file.yml')
14
- end.should raise_error(Errno::ENOENT)
15
- end
16
-
17
- end
@@ -1,7 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
-
3
- describe AppConfig do
4
- it 'should have a version' do
5
- AppConfig.to_version.should_not be_nil
6
- end
7
- end
@@ -1,18 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
-
3
- describe Hashish do
4
- before(:each) do
5
- @strings = { 'key' => 'value', 'four' => 20 }
6
- @symbols = { :key => 'value', :four => 20 }
7
- end
8
-
9
- it 'should not give a fuck about symbols' do
10
- hashish = Hashish.new(@strings)
11
- hashish[:key].should == 'value'
12
- end
13
-
14
- it 'should not give a fuck about strings' do
15
- hashish = Hashish.new(@symbols)
16
- hashish['key'].should == 'value'
17
- end
18
- end
@@ -1,4 +0,0 @@
1
- ---
2
- admin_email: 'admin@example.com'
3
- api_name: 'Supr Webz 2.0'
4
- api_key: 'SUPERAWESOMESERVICE'
@@ -1 +0,0 @@
1
- --exclude "spec/*,gems/*"
@@ -1,4 +0,0 @@
1
- --colour
2
- --format progress
3
- --loadby mtime
4
- --reverse
@@ -1,37 +0,0 @@
1
- require 'spec'
2
-
3
- require File.expand_path(File.dirname(__FILE__) + '/../lib/app_config')
4
-
5
- Spec::Runner.configure do |config|
6
- include AppConfig
7
- end
8
-
9
- # Returns the full path to the +name+ fixture file.
10
- def fixture(name)
11
- File.expand_path(File.join(File.dirname(__FILE__), 'fixtures', name))
12
- end
13
-
14
- # AppConfig.configure wrapper. Accepts a hash of +options+.
15
- def config_for(options)
16
- AppConfig.setup(options)
17
- end
18
-
19
- # Setup Yaml options and pass to config_for().
20
- def config_for_yaml(opts = {})
21
- path = opts[:path] || fixture('app_config.yml')
22
- yaml = {
23
- :storage_method => :yaml,
24
- :path => path,
25
- }
26
- config_for(yaml.merge(opts))
27
- end
28
-
29
- # Setup Sqlite options and pass to config_for().
30
- def config_for_sqlite(opts = {})
31
- database = opts[:database] || fixture('app_config.sqlite3')
32
- sqlite = {
33
- :storage_method => :sqlite,
34
- :database => database
35
- }
36
- config_for(sqlite.merge(opts))
37
- end