app_config 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -7,7 +7,7 @@ configuration, API keys or basically anything in 'key => value' pairs.
7
7
  == Usage
8
8
 
9
9
  Usage is simple. Just pass either a hash of options, or a block, to
10
- AppConfig.configure. See AppConfig::Base for a list of valid options.
10
+ AppConfig.setup. See AppConfig::Base for a list of valid storage methods.
11
11
 
12
12
  For example, given this YAML file:
13
13
 
@@ -20,9 +20,9 @@ Use it like so:
20
20
 
21
21
  require 'app_config'
22
22
 
23
- AppConfig.configure do |config|
24
- config.storage_method = :yaml
25
- config.path = '/path/to/app_config.yml'
23
+ AppConfig.setup do |config|
24
+ config[:storage_method] = :yaml
25
+ config[:path] = '/path/to/app_config.yml'
26
26
  end
27
27
 
28
28
  AppConfig[admin_email] # => 'admin@example.com'
@@ -31,9 +31,9 @@ Use it like so:
31
31
 
32
32
  Want SQLite3 support? No problem!
33
33
 
34
- AppConfig.configure do |config|
35
- config.storage_method = :sqlite
36
- config.database = '/path/to/database.sqlite3'
34
+ AppConfig.setup do |config|
35
+ config[:storage_method] = :sqlite
36
+ config[:database] = '/path/to/database.sqlite3'
37
37
  end
38
38
 
39
39
  AppConfig[:column] # => 'value'
@@ -1,15 +1,26 @@
1
1
  $LOAD_PATH.unshift File.dirname(__FILE__)
2
2
 
3
- require 'yaml'
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
4
11
 
5
12
  require 'core_ext/hashish'
6
13
 
14
+ # TODO: Move these to their own file.
15
+ class UnknownStorageMethod < Exception; end
16
+
7
17
  module AppConfig
8
- VERSION = '0.1.0'
18
+ VERSION = '0.2.0'
9
19
 
10
20
  autoload :Base, 'app_config/base'
11
21
  autoload :Storage, 'app_config/storage'
12
22
 
23
+ # Returns the AppConfig version string.
13
24
  def self.to_version
14
25
  "#{self.class} v#{VERSION}"
15
26
  end
@@ -21,9 +32,9 @@ module AppConfig
21
32
  @@storage[key]
22
33
  end
23
34
 
24
- # Accepts an +options+ hash or pass a block.
25
- # See AppConfig::Base for valid options.
26
- def self.configure(options = {}, &block)
35
+ # Accepts an +options+ hash or a block.
36
+ # See AppConfig::Base for valid storage methods.
37
+ def self.setup(options = {}, &block)
27
38
  @@storage = AppConfig::Base.new(options, &block)
28
39
  end
29
40
 
@@ -6,11 +6,8 @@ module AppConfig
6
6
  # See each storage method's documentation for their specific options.
7
7
  class Base
8
8
 
9
- attr_accessor :storage_method, :path
10
-
11
9
  DEFAULTS = {
12
- :storage_method => :yaml,
13
- :path => File.expand_path(File.join(ENV['HOME'], '.app_config.yml'))
10
+ :storage_method => :yaml
14
11
  }
15
12
 
16
13
  # Accepts either a hash of +options+ or a block (which overrides
@@ -20,10 +17,8 @@ module AppConfig
20
17
  # * :sqlite
21
18
  # * :yaml
22
19
  def initialize(options = {}, &block)
23
- DEFAULTS.merge(options).each_pair do |key, value|
24
- self.send("#{ key }=", value)
25
- end
26
- yield self if block_given?
20
+ @options = DEFAULTS.merge(options)
21
+ yield @options if block_given?
27
22
  @storage = initialize_storage
28
23
  end
29
24
 
@@ -36,13 +31,13 @@ module AppConfig
36
31
 
37
32
  # This decides how to load the data, based on the +storage_method+.
38
33
  def initialize_storage
39
- case storage_method
34
+ case @options[:storage_method]
40
35
  when :sqlite
41
- # TODO: Initialize SQLite3 storage.
36
+ AppConfig::Storage::Sqlite.load(@options)
42
37
  when :yaml
43
- AppConfig::Storage::Yaml.load(path)
38
+ AppConfig::Storage::Yaml.load(@options)
44
39
  else
45
- raise 'Unknown storage method.'
40
+ raise UnknownStorageMethod
46
41
  end
47
42
  end
48
43
 
@@ -1,5 +1,6 @@
1
1
  module AppConfig
2
2
  module Storage
3
+ autoload :Sqlite, 'app_config/storage/sqlite'
3
4
  autoload :Yaml, 'app_config/storage/yaml'
4
5
  end
5
- end
6
+ end
@@ -0,0 +1,61 @@
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
@@ -5,10 +5,16 @@ module AppConfig
5
5
  class Yaml
6
6
  attr_reader :data
7
7
 
8
+ DEFAULTS = {
9
+ :path => File.expand_path(File.join(ENV['HOME'], '.app_config.yml'))
10
+ }
11
+
8
12
  # Loads @data with the YAML file located at +path+.
13
+ # @data will be the Hashish that is accessed with AppConfig[:key].
9
14
  #
10
15
  # Defaults to $HOME/.app_config.yml
11
- def initialize(path)
16
+ def initialize(options)
17
+ path = options[:path] || DEFAULTS[:path]
12
18
  @data = Hashish.new(YAML.load_file(path))
13
19
  end
14
20
 
@@ -2,10 +2,10 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
3
  describe Base do
4
4
 
5
- it 'should raise error on unknown option' do
5
+ it 'should raise UnknownStorageMethod' do
6
6
  lambda do
7
- Base.new(:unknown => 'option')
8
- end.should raise_error(NoMethodError)
7
+ Base.new(:storage_method => 'not_a_real_storage_method')
8
+ end.should raise_error(UnknownStorageMethod)
9
9
  end
10
10
 
11
11
  it 'should have default options' do
@@ -13,8 +13,7 @@ describe Base do
13
13
  # mock up the YAML stuff, so it won't puke
14
14
  YAML.should_receive(:load_file).with(default_path).and_return({:api => 'key'})
15
15
  base = Base.new
16
- base.storage_method.should == :yaml
17
- base.path.should == default_path
16
+ base.instance_variable_get(:@options)[:storage_method].should == :yaml
18
17
  end
19
18
 
20
19
  end
@@ -0,0 +1,15 @@
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
@@ -6,12 +6,32 @@ Spec::Runner.configure do |config|
6
6
  include AppConfig
7
7
  end
8
8
 
9
- FIXTURE = File.expand_path(File.dirname(__FILE__) + '/fixtures/app_config.yml')
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().
10
20
  def config_for_yaml(opts = {})
11
- path = opts[:path] || FIXTURE
12
- @yaml = {
21
+ path = opts[:path] || fixture('app_config.yml')
22
+ yaml = {
13
23
  :storage_method => :yaml,
14
24
  :path => path,
15
25
  }
16
- AppConfig.configure(@yaml.merge(opts))
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))
17
37
  end
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.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dale Campbell
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-03-13 00:00:00 -05:00
12
+ date: 2009-03-15 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -26,6 +26,7 @@ files:
26
26
  - README
27
27
  - Rakefile
28
28
  - lib/app_config/base.rb
29
+ - lib/app_config/storage/sqlite.rb
29
30
  - lib/app_config/storage/yaml.rb
30
31
  - lib/app_config/storage.rb
31
32
  - lib/app_config.rb
@@ -33,20 +34,26 @@ files:
33
34
  - spec/app_config
34
35
  - spec/app_config/base_spec.rb
35
36
  - spec/app_config/storage
37
+ - spec/app_config/storage/sqlite_spec.rb
36
38
  - spec/app_config/storage/yaml_spec.rb
37
39
  - spec/app_config_spec.rb
38
40
  - spec/core_ext
39
41
  - spec/core_ext/hashish_spec.rb
40
42
  - spec/fixtures
43
+ - spec/fixtures/app_config.sqlite3
41
44
  - spec/fixtures/app_config.yml
42
45
  - spec/rcov.opts
43
46
  - spec/spec.opts
44
47
  - spec/spec_helper.rb
45
- has_rdoc: false
48
+ has_rdoc: true
46
49
  homepage: http://oshuma.github.com/app_config
47
50
  post_install_message:
48
- rdoc_options: []
49
-
51
+ rdoc_options:
52
+ - --title "AppConfig"
53
+ - --all
54
+ - --inline-source
55
+ - --line-numbers
56
+ - --main README
50
57
  require_paths:
51
58
  - lib
52
59
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -63,7 +70,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
63
70
  version:
64
71
  requirements: []
65
72
 
66
- rubyforge_project:
73
+ rubyforge_project: app-config
67
74
  rubygems_version: 1.3.1
68
75
  signing_key:
69
76
  specification_version: 2