Skip to content

Instantly share code, notes, and snippets.

@stephencelis
Created January 19, 2010 03:54
Show Gist options
  • Select an option

  • Save stephencelis/280650 to your computer and use it in GitHub Desktop.

Select an option

Save stephencelis/280650 to your computer and use it in GitHub Desktop.

Revisions

  1. stephencelis revised this gist Jan 19, 2010. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion app.rb
    Original file line number Diff line number Diff line change
    @@ -38,7 +38,7 @@ def method_missing(method, *args)
    super if (key = method.to_s).end_with?("=")
    boolean = key.chomp!("?")
    value = self[key]
    value = value.respond_to?(:call) ? value.call(*args) : value
    value = value.call(*args) if value.respond_to?(:call)
    boolean ? !!value : value
    end

  2. stephencelis revised this gist Jan 19, 2010. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion app.rb
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@
    # # config/app/development.rb
    # config.key = "devalue"
    #
    # App.key # => "value"
    # App.key # => "devalue"
    class Configuration
    autoload :OpenStruct, "ostruct"

  3. stephencelis created this gist Jan 19, 2010.
    54 changes: 54 additions & 0 deletions app.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    # Define application-level configuration using a simple DSL.
    #
    # class App < Configuration
    # config.key = "value"
    # config.lazy = lambda { |load| load }
    # end
    #
    # App.key # => "value"
    # App.key? # => true
    # App.lazy("load") # => "load"
    # App.lazy?(nil) # => false
    #
    # Override by Rails environment.
    #
    # # config/app/development.rb
    # config.key = "devalue"
    #
    # App.key # => "value"
    class Configuration
    autoload :OpenStruct, "ostruct"

    class << self
    def [](key)
    config.send key
    end

    def inspect
    config.inspect.sub config.class.name, name
    end

    private

    def config
    @config ||= OpenStruct.new
    end

    def method_missing(method, *args)
    super if (key = method.to_s).end_with?("=")
    boolean = key.chomp!("?")
    value = self[key]
    value = value.respond_to?(:call) ? value.call(*args) : value
    boolean ? !!value : value
    end

    def inherited(subclass)
    return unless defined? Rails

    config = "#{Rails.root}/config/#{name.underscore}/#{Rails.env}.rb"
    if File.exist?(config)
    eval File.read(config), binding, __FILE__, __LINE__
    end
    end
    end
    end