app_config 0.2.4 → 0.3.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.
- data/README +28 -5
 - data/lib/app_config.rb +11 -12
 - data/lib/app_config/base.rb +39 -26
 - data/lib/app_config/storage/sqlite.rb +2 -0
 - data/lib/core_ext/hashish.rb +8 -0
 - metadata +2 -2
 
    
        data/README
    CHANGED
    
    | 
         @@ -23,6 +23,8 @@ Use it like so: 
     | 
|
| 
       23 
23 
     | 
    
         
             
              AppConfig.setup do |config|
         
     | 
| 
       24 
24 
     | 
    
         
             
                config[:storage_method] = :yaml
         
     | 
| 
       25 
25 
     | 
    
         
             
                config[:path] = '/path/to/app_config.yml'
         
     | 
| 
      
 26 
     | 
    
         
            +
                # ..or..
         
     | 
| 
      
 27 
     | 
    
         
            +
                config[:uri] = 'yaml://path/to/app_config.yml'
         
     | 
| 
       26 
28 
     | 
    
         
             
              end
         
     | 
| 
       27 
29 
     | 
    
         | 
| 
       28 
30 
     | 
    
         
             
              # Later on...
         
     | 
| 
         @@ -36,13 +38,34 @@ Want SQLite3?  No problem! 
     | 
|
| 
       36 
38 
     | 
    
         
             
              AppConfig.setup do |config|
         
     | 
| 
       37 
39 
     | 
    
         
             
                config[:storage_method] = :sqlite
         
     | 
| 
       38 
40 
     | 
    
         
             
                config[:database] = '/path/to/database.sqlite3'
         
     | 
| 
       39 
     | 
    
         
            -
                 
     | 
| 
      
 41 
     | 
    
         
            +
                # ..or..
         
     | 
| 
      
 42 
     | 
    
         
            +
                config[:uri] = 'sqlite://path/to/database.sqlite3'
         
     | 
| 
      
 43 
     | 
    
         
            +
             
     | 
| 
      
 44 
     | 
    
         
            +
                config[:table] = 'app_config' # defaults to 'app_config'
         
     | 
| 
       40 
45 
     | 
    
         
             
              end
         
     | 
| 
       41 
46 
     | 
    
         | 
| 
       42 
47 
     | 
    
         
             
              AppConfig[:column]  # => 'value'
         
     | 
| 
       43 
48 
     | 
    
         | 
| 
       44 
     | 
    
         
            -
            You can also pass a <tt>:uri</tt> option, which will automatically determine
         
     | 
| 
       45 
     | 
    
         
            -
            the options for the storage method.  So in the block (or passed in hash):
         
     | 
| 
       46 
49 
     | 
    
         | 
| 
       47 
     | 
    
         
            -
             
     | 
| 
       48 
     | 
    
         
            -
             
     | 
| 
      
 50 
     | 
    
         
            +
            == Rails Mode
         
     | 
| 
      
 51 
     | 
    
         
            +
             
     | 
| 
      
 52 
     | 
    
         
            +
            As of version 0.3.1, there's a 'Rails mode' where you can organize
         
     | 
| 
      
 53 
     | 
    
         
            +
            the config file sort of like Rails' database config.
         
     | 
| 
      
 54 
     | 
    
         
            +
             
     | 
| 
      
 55 
     | 
    
         
            +
            *NOTE:* This might get renamed to a more Rack-ish 'environment' mode.
         
     | 
| 
      
 56 
     | 
    
         
            +
             
     | 
| 
      
 57 
     | 
    
         
            +
              development:
         
     | 
| 
      
 58 
     | 
    
         
            +
                name: 'Dev Mode'
         
     | 
| 
      
 59 
     | 
    
         
            +
             
     | 
| 
      
 60 
     | 
    
         
            +
              production:
         
     | 
| 
      
 61 
     | 
    
         
            +
                name: 'Production Mode'
         
     | 
| 
      
 62 
     | 
    
         
            +
             
     | 
| 
      
 63 
     | 
    
         
            +
            Then set the <tt>:rails</tt> option to true (it's false by default).
         
     | 
| 
      
 64 
     | 
    
         
            +
             
     | 
| 
      
 65 
     | 
    
         
            +
              AppConfig.setup do |config|
         
     | 
| 
      
 66 
     | 
    
         
            +
                config[:rails] = true
         
     | 
| 
      
 67 
     | 
    
         
            +
                config[:uri] = 'yaml://path/to/app_config.yml'
         
     | 
| 
      
 68 
     | 
    
         
            +
              end
         
     | 
| 
      
 69 
     | 
    
         
            +
             
     | 
| 
      
 70 
     | 
    
         
            +
              # If Rails.env is 'production'
         
     | 
| 
      
 71 
     | 
    
         
            +
              AppConfig[:name] = 'Production Mode'
         
     | 
    
        data/lib/app_config.rb
    CHANGED
    
    | 
         @@ -1,37 +1,32 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            $LOAD_PATH.unshift File.dirname(__FILE__)
         
     | 
| 
       2 
2 
     | 
    
         | 
| 
       3 
3 
     | 
    
         
             
            # TODO: Only load deps if needed (no gems unless required).
         
     | 
| 
       4 
     | 
    
         
            -
             
     | 
| 
      
 4 
     | 
    
         
            +
            dependencies = %w{ sqlite3 yaml uri }
         
     | 
| 
       5 
5 
     | 
    
         | 
| 
       6 
6 
     | 
    
         
             
            begin
         
     | 
| 
       7 
     | 
    
         
            -
               
     | 
| 
      
 7 
     | 
    
         
            +
              dependencies.each { |lib| require lib }
         
     | 
| 
       8 
8 
     | 
    
         
             
            rescue LoadError
         
     | 
| 
       9 
9 
     | 
    
         
             
              require 'rubygems'
         
     | 
| 
       10 
     | 
    
         
            -
               
     | 
| 
      
 10 
     | 
    
         
            +
              dependencies.each { |lib| require lib }
         
     | 
| 
       11 
11 
     | 
    
         
             
            end
         
     | 
| 
       12 
12 
     | 
    
         | 
| 
      
 13 
     | 
    
         
            +
            # AppConfig stuff.
         
     | 
| 
       13 
14 
     | 
    
         
             
            require 'core_ext/hashish'
         
     | 
| 
       14 
15 
     | 
    
         | 
| 
       15 
     | 
    
         
            -
            # TODO: Move these to their own file.
         
     | 
| 
       16 
     | 
    
         
            -
            class NotConfigured < Exception
         
     | 
| 
       17 
     | 
    
         
            -
              def to_s; "Must call 'AppConfig.setup' to setup storage!"; end
         
     | 
| 
       18 
     | 
    
         
            -
            end
         
     | 
| 
       19 
     | 
    
         
            -
            class UnknownStorageMethod < Exception; end
         
     | 
| 
       20 
     | 
    
         
            -
             
     | 
| 
       21 
16 
     | 
    
         
             
            module AppConfig
         
     | 
| 
       22 
     | 
    
         
            -
              VERSION = '0. 
     | 
| 
      
 17 
     | 
    
         
            +
              VERSION = '0.3.1'
         
     | 
| 
       23 
18 
     | 
    
         | 
| 
       24 
19 
     | 
    
         
             
              autoload :Base, 'app_config/base'
         
     | 
| 
      
 20 
     | 
    
         
            +
              autoload :Error, 'app_config/error'
         
     | 
| 
       25 
21 
     | 
    
         
             
              autoload :Storage, 'app_config/storage'
         
     | 
| 
       26 
22 
     | 
    
         | 
| 
       27 
23 
     | 
    
         
             
              # Returns the AppConfig version string.
         
     | 
| 
       28 
24 
     | 
    
         
             
              def self.to_version
         
     | 
| 
       29 
     | 
    
         
            -
                "#{self. 
     | 
| 
      
 25 
     | 
    
         
            +
                "#{self.name} v#{VERSION}"
         
     | 
| 
       30 
26 
     | 
    
         
             
              end
         
     | 
| 
       31 
27 
     | 
    
         | 
| 
       32 
28 
     | 
    
         
             
              # Access the configured <tt>key</tt>'s value.
         
     | 
| 
       33 
29 
     | 
    
         
             
              def self.[](key)
         
     | 
| 
       34 
     | 
    
         
            -
                raise NotConfigured unless defined?(@@storage)
         
     | 
| 
       35 
30 
     | 
    
         
             
                @@storage[key]
         
     | 
| 
       36 
31 
     | 
    
         
             
              end
         
     | 
| 
       37 
32 
     | 
    
         | 
| 
         @@ -41,4 +36,8 @@ module AppConfig 
     | 
|
| 
       41 
36 
     | 
    
         
             
                @@storage = AppConfig::Base.new(options, &block)
         
     | 
| 
       42 
37 
     | 
    
         
             
              end
         
     | 
| 
       43 
38 
     | 
    
         | 
| 
      
 39 
     | 
    
         
            +
              def self.to_hash
         
     | 
| 
      
 40 
     | 
    
         
            +
                @@storage.to_hash
         
     | 
| 
      
 41 
     | 
    
         
            +
              end
         
     | 
| 
      
 42 
     | 
    
         
            +
             
     | 
| 
       44 
43 
     | 
    
         
             
            end # AppConfig
         
     | 
    
        data/lib/app_config/base.rb
    CHANGED
    
    | 
         @@ -20,42 +20,55 @@ module AppConfig 
     | 
|
| 
       20 
20 
     | 
    
         
             
                def initialize(options = {}, &block)
         
     | 
| 
       21 
21 
     | 
    
         
             
                  @options = DEFAULTS.merge(options)
         
     | 
| 
       22 
22 
     | 
    
         
             
                  yield @options if block_given?
         
     | 
| 
      
 23 
     | 
    
         
            +
             
     | 
| 
       23 
24 
     | 
    
         
             
                  determine_storage_method if @options[:uri]
         
     | 
| 
       24 
25 
     | 
    
         
             
                  @storage = initialize_storage
         
     | 
| 
       25 
26 
     | 
    
         
             
                end
         
     | 
| 
       26 
27 
     | 
    
         | 
| 
       27 
28 
     | 
    
         
             
                # Access the <tt>key</tt>'s value in @storage.
         
     | 
| 
       28 
29 
     | 
    
         
             
                def [](key)
         
     | 
| 
       29 
     | 
    
         
            -
                   
     | 
| 
      
 30 
     | 
    
         
            +
                  storage[key]
         
     | 
| 
      
 31 
     | 
    
         
            +
                end
         
     | 
| 
      
 32 
     | 
    
         
            +
             
     | 
| 
      
 33 
     | 
    
         
            +
                def storage
         
     | 
| 
      
 34 
     | 
    
         
            +
                  rails_enabled? ? @storage[::Rails.env] : @storage
         
     | 
| 
       30 
35 
     | 
    
         
             
                end
         
     | 
| 
       31 
36 
     | 
    
         | 
| 
       32 
     | 
    
         
            -
             
     | 
| 
       33 
     | 
    
         
            -
             
     | 
| 
       34 
     | 
    
         
            -
              # Sets the storage_method depending on the URI given.
         
     | 
| 
       35 
     | 
    
         
            -
              def determine_storage_method
         
     | 
| 
       36 
     | 
    
         
            -
                uri = URI.parse(@options.delete(:uri))
         
     | 
| 
       37 
     | 
    
         
            -
                case uri.scheme
         
     | 
| 
       38 
     | 
    
         
            -
                when 'sqlite'
         
     | 
| 
       39 
     | 
    
         
            -
                  @options[:storage_method] = :sqlite
         
     | 
| 
       40 
     | 
    
         
            -
                  @options[:database] = uri.path
         
     | 
| 
       41 
     | 
    
         
            -
                when 'yaml'
         
     | 
| 
       42 
     | 
    
         
            -
                  @options[:storage_method] = :yaml
         
     | 
| 
       43 
     | 
    
         
            -
                  @options[:path] = uri.path
         
     | 
| 
      
 37 
     | 
    
         
            +
                def to_hash
         
     | 
| 
      
 38 
     | 
    
         
            +
                  storage.to_hash
         
     | 
| 
       44 
39 
     | 
    
         
             
                end
         
     | 
| 
       45 
     | 
    
         
            -
             
     | 
| 
       46 
     | 
    
         
            -
             
     | 
| 
       47 
     | 
    
         
            -
             
     | 
| 
       48 
     | 
    
         
            -
             
     | 
| 
       49 
     | 
    
         
            -
             
     | 
| 
       50 
     | 
    
         
            -
             
     | 
| 
       51 
     | 
    
         
            -
             
     | 
| 
       52 
     | 
    
         
            -
                   
     | 
| 
       53 
     | 
    
         
            -
             
     | 
| 
       54 
     | 
    
         
            -
             
     | 
| 
       55 
     | 
    
         
            -
             
     | 
| 
       56 
     | 
    
         
            -
             
     | 
| 
      
 40 
     | 
    
         
            +
             
     | 
| 
      
 41 
     | 
    
         
            +
                private
         
     | 
| 
      
 42 
     | 
    
         
            +
             
     | 
| 
      
 43 
     | 
    
         
            +
                # Sets the storage_method depending on the URI given.
         
     | 
| 
      
 44 
     | 
    
         
            +
                def determine_storage_method
         
     | 
| 
      
 45 
     | 
    
         
            +
                  uri = URI.parse(@options.delete(:uri))
         
     | 
| 
      
 46 
     | 
    
         
            +
                  case uri.scheme
         
     | 
| 
      
 47 
     | 
    
         
            +
                  when 'sqlite'
         
     | 
| 
      
 48 
     | 
    
         
            +
                    @options[:storage_method] = :sqlite
         
     | 
| 
      
 49 
     | 
    
         
            +
                    @options[:database] = uri.path
         
     | 
| 
      
 50 
     | 
    
         
            +
                  when 'yaml'
         
     | 
| 
      
 51 
     | 
    
         
            +
                    @options[:storage_method] = :yaml
         
     | 
| 
      
 52 
     | 
    
         
            +
                    @options[:path] = uri.path
         
     | 
| 
      
 53 
     | 
    
         
            +
                  end
         
     | 
| 
      
 54 
     | 
    
         
            +
                end
         
     | 
| 
      
 55 
     | 
    
         
            +
             
     | 
| 
      
 56 
     | 
    
         
            +
                # This decides how to load the data, based on the +storage_method+.
         
     | 
| 
      
 57 
     | 
    
         
            +
                # TODO: Maybe purge AppConfig options (ie, those not related to the user-end).
         
     | 
| 
      
 58 
     | 
    
         
            +
                def initialize_storage
         
     | 
| 
      
 59 
     | 
    
         
            +
                  case @options[:storage_method]
         
     | 
| 
      
 60 
     | 
    
         
            +
                  when :sqlite
         
     | 
| 
      
 61 
     | 
    
         
            +
                    AppConfig::Storage::Sqlite.load(@options)
         
     | 
| 
      
 62 
     | 
    
         
            +
                  when :yaml
         
     | 
| 
      
 63 
     | 
    
         
            +
                    AppConfig::Storage::YAML.load(@options)
         
     | 
| 
      
 64 
     | 
    
         
            +
                  else
         
     | 
| 
      
 65 
     | 
    
         
            +
                    raise Error::UnknownStorageMethod
         
     | 
| 
      
 66 
     | 
    
         
            +
                  end
         
     | 
| 
      
 67 
     | 
    
         
            +
                end
         
     | 
| 
      
 68 
     | 
    
         
            +
             
     | 
| 
      
 69 
     | 
    
         
            +
                def rails_enabled?
         
     | 
| 
      
 70 
     | 
    
         
            +
                  @options[:rails] || false
         
     | 
| 
       57 
71 
     | 
    
         
             
                end
         
     | 
| 
       58 
     | 
    
         
            -
              end
         
     | 
| 
       59 
72 
     | 
    
         | 
| 
       60 
73 
     | 
    
         
             
              end # Base
         
     | 
| 
       61 
74 
     | 
    
         
             
            end # AppConfig
         
     | 
| 
         @@ -37,6 +37,7 @@ module AppConfig 
     | 
|
| 
       37 
37 
     | 
    
         
             
                    values(columns(table))
         
     | 
| 
       38 
38 
     | 
    
         
             
                  end
         
     | 
| 
       39 
39 
     | 
    
         | 
| 
      
 40 
     | 
    
         
            +
                  # Return the values for a given +columns+ (as a Hashish).
         
     | 
| 
       40 
41 
     | 
    
         
             
                  def values(columns)
         
     | 
| 
       41 
42 
     | 
    
         
             
                    data = Hashish.new
         
     | 
| 
       42 
43 
     | 
    
         
             
                    query = "SELECT #{columns.join(', ')} FROM #{@options[:table]}"
         
     | 
| 
         @@ -46,6 +47,7 @@ module AppConfig 
     | 
|
| 
       46 
47 
     | 
    
         
             
                    data
         
     | 
| 
       47 
48 
     | 
    
         
             
                  end
         
     | 
| 
       48 
49 
     | 
    
         | 
| 
      
 50 
     | 
    
         
            +
                  # Return the column names of a given +table+ (as an Array).
         
     | 
| 
       49 
51 
     | 
    
         
             
                  def columns(table)
         
     | 
| 
       50 
52 
     | 
    
         
             
                    columns = table.split(', ')
         
     | 
| 
       51 
53 
     | 
    
         
             
                    # Trip the first element, since it's the SQL CREATE statement.
         
     | 
    
        data/lib/core_ext/hashish.rb
    CHANGED
    
    
    
        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. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.3.1
         
     | 
| 
       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- 
     | 
| 
      
 12 
     | 
    
         
            +
            date: 2009-10-01 00:00:00 -05:00
         
     | 
| 
       13 
13 
     | 
    
         
             
            default_executable: 
         
     | 
| 
       14 
14 
     | 
    
         
             
            dependencies: 
         
     | 
| 
       15 
15 
     | 
    
         
             
            - !ruby/object:Gem::Dependency 
         
     |