Class: Settable::Namespace

Inherits:
Object
  • Object
show all
Defined in:
lib/settable.rb

Overview

Private: A container for multiple settings. Every setting belongs to a

namespace group.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, parent = nil, &block) ⇒ Namespace

Public: Create a new namespace to store settings in. Namespaces inherit

their parent's environment checker by default, but that may be
overridden using +use_environment+

name - The namespace name. parent - The parent namespace when nesting namespaces. block - A block containing all your set calls

Examples

namespace = Namespace.new(:global, nil) do
  set :hello, 'world'
end

namespace.hello
# => 'world'

Returns the namespace to attach settings to.



257
258
259
260
261
262
# File 'lib/settable.rb', line 257

def initialize(name, parent = nil, &block)
  @name = name
  @environment = parent ? parent.environment : nil
  @parent = parent
  instance_eval &block
end

Instance Attribute Details

#environmentObject (readonly)

Returns the value of attribute environment.



237
238
239
# File 'lib/settable.rb', line 237

def environment
  @environment
end

#nameObject (readonly)

Returns the value of attribute name.



237
238
239
# File 'lib/settable.rb', line 237

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



237
238
239
# File 'lib/settable.rb', line 237

def parent
  @parent
end

Instance Method Details

#environment_matches?(*values) ⇒ Boolean

Public: Helper method for setting values based off our environment.

values - An array of values to check against our environment

Examples

$config = Settable.configure do
  use_environment :rails
  set :google_analytics, environment_matches?(:production, :staging)
end

Rails.env = "production" # (or Rails.env = "staging")
$config.google_analytics?
# => true

Rails.env = "development"
$config.google_analytics?
# => false

Returns true if our environment matches any of our values.

Returns:

  • (Boolean)


383
384
385
# File 'lib/settable.rb', line 383

def environment_matches?(*values)
  Array(values).any?{ |v| @environment.call(v) }
end

#namespace(name, &block) ⇒ Object

Public: Create a nested namespace.

name - The name for the namespace. block - A block to run in this namespace’s context.

Examples

$config = Settable.configure do
  set :username, 'user'

  namespace :api do
    set :username, 'api-user'
  end
end

$config.username
# => 'user'

$config.api.username
# => 'api-user'

Returns nothing.



356
357
358
359
360
361
# File 'lib/settable.rb', line 356

def namespace(name, &block)
  define_metaclass_method(name.to_sym) do
    namespace = Namespace.new(name, self, &block)
    namespace
  end
end

#rootObject

Public: Find the top-most namespace and return it. Examples

$config = Settable.configure do
  set :hello, 'world'

  namespace :a do
    namespace :b do
      set(:test){ root.hello }
    end
  end
end

$config.a.b.test
# => 'world'

Returns the topmost namespace.



404
405
406
407
408
409
410
# File 'lib/settable.rb', line 404

def root
  @root ||= begin
    root = self
    root = root.parent until root.parent.nil?
    root
  end
end

#set(name, value = nil, &block) ⇒ Object

Public: Create a setting method and its ‘presence’ method.

name - The setting name. value - The settings value (unless a block is given). block - The block to run for this setting’s value

Examples

$config = Settable.configure do
  set :hello, 'world'
end
$config.hello
# => 'world'

$config.hello?
# => true

$config = Settable.configure do
  set(:hello){ 'world' }
end
$config.hello
# => 'world'

Returns nothing.



328
329
330
331
332
# File 'lib/settable.rb', line 328

def set(name, value = nil, &block)
  setting = Setting.new(self, name, value, &block)
  define_metaclass_method(name.to_sym){ setting.value }
  define_metaclass_method(:"#{name}?"){ setting.present? }
end

#use_environment(klass = nil, &block) ⇒ Object

Public: A custom checker when using the Setting’s environment helper.

You can pass a block in which will receive the +environment+ value
and should return a true/false to indicate a match. All nested
namespaces will inherit their parent environment by default.

klass - A Class which responds to a #call(value) method, or a symbol of

either :rails, or :env for builtin testers

block - A block that will be called with the environment to see if it is

a match.

Examples

# check if the given value is in our ENV
namespace.use_environment{ |value| ENV.has_key?(value) }

# use the built-in tester that looks at Rails.env
namespace.use_environment(:rails)

# use a custom class that responds to #call(value)
namespace.use_environment(MyCustomChecker.new)

Returns the duplicated String.



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/settable.rb', line 286

def use_environment(klass = nil, &block)
  if klass.nil?
    if block_given?
      klass = block
    else
      return
    end
  elsif klass.is_a?(Symbol)
    klass = Object.module_eval("Settable::Environment::#{klass.to_s.capitalize}", __FILE__, __LINE__)
  else
    unless klass.is_a?(Object) && klass.respond_to?(:call)
      raise "#{klass} must respond to #call(value) to be a valid environment!"
    end
  end

  @environment = klass
end