konfa 0.0.2 → 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.
- data/lib/konfa.rb +58 -29
- metadata +19 -40
    
        data/lib/konfa.rb
    CHANGED
    
    | @@ -3,25 +3,59 @@ require 'yaml' | |
| 3 3 |  | 
| 4 4 | 
             
            module Konfa
         | 
| 5 5 | 
             
              class Base
         | 
| 6 | 
            +
                class << self
         | 
| 7 | 
            +
                  protected
         | 
| 6 8 |  | 
| 7 | 
            -
             | 
| 8 | 
            -
             | 
| 9 | 
            +
                  #
         | 
| 10 | 
            +
                  # The following methods are not a part of the public API. You may sublcass
         | 
| 11 | 
            +
                  # them, but remember that unless you scope them as protected or private,
         | 
| 12 | 
            +
                  # they will then be public
         | 
| 13 | 
            +
                  #
         | 
| 9 14 |  | 
| 10 | 
            -
             | 
| 15 | 
            +
                  attr_writer :configuration
         | 
| 11 16 |  | 
| 12 | 
            -
                  def  | 
| 13 | 
            -
                     | 
| 14 | 
            -
             | 
| 17 | 
            +
                  def configuration
         | 
| 18 | 
            +
                    @configuration ||= self.allowed_variables
         | 
| 19 | 
            +
                  end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                  def truthy?(value)
         | 
| 22 | 
            +
                    (!value.nil? && value =~ /^\s*(?:true|1|yes|on)\s*$/i) ? true : false
         | 
| 23 | 
            +
                  end
         | 
| 24 | 
            +
             | 
| 25 | 
            +
                  def store(key, value)
         | 
| 26 | 
            +
                    key = key.to_sym
         | 
| 27 | 
            +
                    raise UnsupportedVariableError.new(key) unless self.configuration.has_key? key
         | 
| 28 | 
            +
                    self.configuration[key] = value.to_s
         | 
| 15 29 | 
             
                  end
         | 
| 16 30 |  | 
| 17 | 
            -
                   | 
| 18 | 
            -
             | 
| 19 | 
            -
             | 
| 31 | 
            +
                  public
         | 
| 32 | 
            +
             | 
| 33 | 
            +
             | 
| 34 | 
            +
                  #
         | 
| 35 | 
            +
                  # The following methods should be overridden and used to configure the
         | 
| 36 | 
            +
                  # configuration class
         | 
| 37 | 
            +
                  #
         | 
| 38 | 
            +
             | 
| 39 | 
            +
                  def env_variable_prefix
         | 
| 40 | 
            +
                    'APP_'
         | 
| 41 | 
            +
                  end
         | 
| 42 | 
            +
             | 
| 43 | 
            +
                  def allowed_variables
         | 
| 44 | 
            +
                    {}
         | 
| 45 | 
            +
                  end
         | 
| 46 | 
            +
             | 
| 47 | 
            +
             | 
| 48 | 
            +
                  #
         | 
| 49 | 
            +
                  # The following methods provides the interface to this class
         | 
| 50 | 
            +
                  #
         | 
| 51 | 
            +
             | 
| 52 | 
            +
                  def get(variable)
         | 
| 53 | 
            +
                    raise UnsupportedVariableError.new(variable) unless self.configuration.has_key? variable
         | 
| 54 | 
            +
                    self.configuration[variable]
         | 
| 20 55 | 
             
                  end
         | 
| 21 56 |  | 
| 22 57 | 
             
                  def true?(variable)
         | 
| 23 | 
            -
                     | 
| 24 | 
            -
                    (!v.nil? && v =~ /^\s*(?:true|1|yes|on)\s*$/i) ? true : false
         | 
| 58 | 
            +
                    self.truthy?(self.get(variable))
         | 
| 25 59 | 
             
                  end
         | 
| 26 60 |  | 
| 27 61 | 
             
                  def false?(variable)
         | 
| @@ -29,15 +63,11 @@ module Konfa | |
| 29 63 | 
             
                  end
         | 
| 30 64 |  | 
| 31 65 | 
             
                  def variables
         | 
| 32 | 
            -
                     | 
| 66 | 
            +
                    self.configuration.keys
         | 
| 33 67 | 
             
                  end
         | 
| 34 68 |  | 
| 35 69 | 
             
                  def dump
         | 
| 36 | 
            -
                     | 
| 37 | 
            -
                  end
         | 
| 38 | 
            -
             | 
| 39 | 
            -
                  def env_variable_prefix
         | 
| 40 | 
            -
                    @@env_variable_prefix
         | 
| 70 | 
            +
                    self.configuration.dup
         | 
| 41 71 | 
             
                  end
         | 
| 42 72 |  | 
| 43 73 | 
             
                  def initialize_from_yaml(path)
         | 
| @@ -47,23 +77,22 @@ module Konfa | |
| 47 77 |  | 
| 48 78 | 
             
                    yaml_data = YAML.load_file(path)
         | 
| 49 79 | 
             
                    yaml_data.each do |variable, value|
         | 
| 50 | 
            -
                       | 
| 51 | 
            -
                      set(variable.to_sym, value.to_s)
         | 
| 80 | 
            +
                      self.store(variable, value)
         | 
| 52 81 | 
             
                    end
         | 
| 82 | 
            +
             | 
| 53 83 | 
             
                    after_initialize
         | 
| 54 84 | 
             
                    dump
         | 
| 55 85 | 
             
                  end
         | 
| 56 86 |  | 
| 57 | 
            -
                  def initialize_from_env | 
| 87 | 
            +
                  def initialize_from_env
         | 
| 88 | 
            +
                    conf_prefix = self.env_variable_prefix.upcase
         | 
| 89 | 
            +
             | 
| 58 90 | 
             
                    ENV.keys.reject { |key|
         | 
| 59 | 
            -
                      key !~ /^#{ | 
| 91 | 
            +
                      key !~ /^#{conf_prefix}/ # Ignore everything that doesn't match the prefix
         | 
| 60 92 | 
             
                    }.each { |key|
         | 
| 61 | 
            -
                      variable = key[ | 
| 62 | 
            -
                      unless @@variables.has_key? variable
         | 
| 63 | 
            -
                        raise UnsupportedVariableError.new(variable)
         | 
| 64 | 
            -
                      end
         | 
| 93 | 
            +
                      variable = key[conf_prefix.size..-1].downcase
         | 
| 65 94 |  | 
| 66 | 
            -
                       | 
| 95 | 
            +
                      self.store(variable, ENV[key])
         | 
| 67 96 | 
             
                    }
         | 
| 68 97 | 
             
                    after_initialize
         | 
| 69 98 | 
             
                    dump
         | 
| @@ -73,13 +102,13 @@ module Konfa | |
| 73 102 | 
             
                  end
         | 
| 74 103 |  | 
| 75 104 | 
             
                  def with_config(overrides={})
         | 
| 76 | 
            -
                     | 
| 77 | 
            -
                    overrides.each_pair {|k,v| self. | 
| 105 | 
            +
                    original_config = dump
         | 
| 106 | 
            +
                    overrides.each_pair {|k,v| self.store(k, v) }
         | 
| 78 107 |  | 
| 79 108 | 
             
                    begin
         | 
| 80 109 | 
             
                      result = yield
         | 
| 81 110 | 
             
                    ensure
         | 
| 82 | 
            -
                       | 
| 111 | 
            +
                      self.configuration = original_config
         | 
| 83 112 | 
             
                    end
         | 
| 84 113 |  | 
| 85 114 | 
             
                    result
         | 
    
        metadata
    CHANGED
    
    | @@ -1,68 +1,47 @@ | |
| 1 | 
            -
            --- !ruby/object:Gem::Specification | 
| 1 | 
            +
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: konfa
         | 
| 3 | 
            -
            version: !ruby/object:Gem::Version | 
| 4 | 
            -
               | 
| 3 | 
            +
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            +
              version: 0.1.0
         | 
| 5 5 | 
             
              prerelease: 
         | 
| 6 | 
            -
              segments: 
         | 
| 7 | 
            -
              - 0
         | 
| 8 | 
            -
              - 0
         | 
| 9 | 
            -
              - 2
         | 
| 10 | 
            -
              version: 0.0.2
         | 
| 11 6 | 
             
            platform: ruby
         | 
| 12 | 
            -
            authors: | 
| 7 | 
            +
            authors:
         | 
| 13 8 | 
             
            - Gunnar Hansson
         | 
| 14 9 | 
             
            - Avidity
         | 
| 15 10 | 
             
            autorequire: 
         | 
| 16 11 | 
             
            bindir: bin
         | 
| 17 12 | 
             
            cert_chain: []
         | 
| 18 | 
            -
             | 
| 19 | 
            -
            date: 2013-09-20 00:00:00 +02:00
         | 
| 20 | 
            -
            default_executable: 
         | 
| 13 | 
            +
            date: 2014-03-24 00:00:00.000000000 Z
         | 
| 21 14 | 
             
            dependencies: []
         | 
| 22 | 
            -
             | 
| 23 15 | 
             
            description: Helps you avoid common pitfalls when dealing with app config
         | 
| 24 16 | 
             
            email: code@avidiy.se
         | 
| 25 17 | 
             
            executables: []
         | 
| 26 | 
            -
             | 
| 27 18 | 
             
            extensions: []
         | 
| 28 | 
            -
             | 
| 29 19 | 
             
            extra_rdoc_files: []
         | 
| 30 | 
            -
             | 
| 31 | 
            -
            files: 
         | 
| 20 | 
            +
            files:
         | 
| 32 21 | 
             
            - lib/konfa.rb
         | 
| 33 | 
            -
            has_rdoc: true
         | 
| 34 22 | 
             
            homepage: http://github.com/avidity/konfa
         | 
| 35 | 
            -
            licenses: | 
| 23 | 
            +
            licenses:
         | 
| 36 24 | 
             
            - MIT
         | 
| 37 25 | 
             
            post_install_message: 
         | 
| 38 26 | 
             
            rdoc_options: []
         | 
| 39 | 
            -
             | 
| 40 | 
            -
            require_paths: 
         | 
| 27 | 
            +
            require_paths:
         | 
| 41 28 | 
             
            - lib
         | 
| 42 | 
            -
            required_ruby_version: !ruby/object:Gem::Requirement | 
| 29 | 
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         | 
| 43 30 | 
             
              none: false
         | 
| 44 | 
            -
              requirements: | 
| 45 | 
            -
              - -  | 
| 46 | 
            -
                - !ruby/object:Gem::Version | 
| 47 | 
            -
                   | 
| 48 | 
            -
             | 
| 49 | 
            -
                  - 0
         | 
| 50 | 
            -
                  version: "0"
         | 
| 51 | 
            -
            required_rubygems_version: !ruby/object:Gem::Requirement 
         | 
| 31 | 
            +
              requirements:
         | 
| 32 | 
            +
              - - ! '>='
         | 
| 33 | 
            +
                - !ruby/object:Gem::Version
         | 
| 34 | 
            +
                  version: '0'
         | 
| 35 | 
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         | 
| 52 36 | 
             
              none: false
         | 
| 53 | 
            -
              requirements: | 
| 54 | 
            -
              - -  | 
| 55 | 
            -
                - !ruby/object:Gem::Version | 
| 56 | 
            -
                   | 
| 57 | 
            -
                  segments: 
         | 
| 58 | 
            -
                  - 0
         | 
| 59 | 
            -
                  version: "0"
         | 
| 37 | 
            +
              requirements:
         | 
| 38 | 
            +
              - - ! '>='
         | 
| 39 | 
            +
                - !ruby/object:Gem::Version
         | 
| 40 | 
            +
                  version: '0'
         | 
| 60 41 | 
             
            requirements: []
         | 
| 61 | 
            -
             | 
| 62 42 | 
             
            rubyforge_project: 
         | 
| 63 | 
            -
            rubygems_version: 1. | 
| 43 | 
            +
            rubygems_version: 1.8.23
         | 
| 64 44 | 
             
            signing_key: 
         | 
| 65 45 | 
             
            specification_version: 3
         | 
| 66 46 | 
             
            summary: Application configuration
         | 
| 67 47 | 
             
            test_files: []
         | 
| 68 | 
            -
             |