class Warp::Dir::Config

Constants

DEFAULTS

Attributes

variables[RW]

Public Class Methods

new(opts = {}) click to toggle source
# File lib/warp/dir/config.rb, line 19
def initialize(opts = {})
  configure(opts)
end

Public Instance Methods

[](key) click to toggle source

allow syntax @config

# File lib/warp/dir/config.rb, line 57
def [](key)
  self.send(key)
end
configure(opts = {}) click to toggle source
# File lib/warp/dir/config.rb, line 23
def configure(opts = {})
  options = DEFAULTS.merge(opts)

  # Move :config hash key->value to :warprc that is expected by the Config
  if options[:config]
    options[:warprc] = options[:config]
    options.delete(:config)
  end

  self.variables = []

  # OK. I concede. This is very likely a total overkill :O
  # The thing is: I just really like calling hash members via
  # methods, and I didn't want to load HashieMash because
  # it's big. Real big.
  #
  # IRB Session that explains it all:
  #
  # c = Config.new({ foo: "bar", bar: "foo"})
  # => #<Warp::Dir::Config:0x007ff8e39531f0 @variables=[:config, :foo, :bar], @config="/Users/kigster/.warprc", @foo="bar", @bar="foo">
  #  > c.foo              # => "bar"
  #  > c.bar              # => "foo"
  #  > c.bar?             # => true
  #  > c.config           # => "/Users/kigster/.warprc"
  #  > c.color = "red"    # => "red"
  #  > c.color            # => "red"
  #  > c.color?           # => true

  options.each_pair do |variable_name, value|
    self.variables << add_config_variable(variable_name, value)
  end
end
method_missing(method, *args, &block) click to toggle source

Dispatches redis operations to master/slaves.

Calls superclass method
# File lib/warp/dir/config.rb, line 62
def method_missing(method, *args, &block)
  if method =~ /=$/
    add_config_variable(method.to_s.gsub(/=$/, ''), *args)
  else
    super
  end
end

Private Instance Methods

add_config_variable(variable_name, value) click to toggle source
# File lib/warp/dir/config.rb, line 72
def add_config_variable(variable_name, value)
  reader   = variable_name.to_sym
  writer   = "#{reader}=".to_sym
  boolean  = "#{reader}?"
  variable = "@#{reader}".to_sym
  # set this value on the the instance of config class
  instance_variable_set(variable, value)
  # add the reader and the writer to this key
  define_singleton_method(reader) { instance_variable_get(variable) }
  define_singleton_method(writer) { |new_value| instance_variable_set(variable, new_value) }
  define_singleton_method(boolean){ !instance_variable_get(variable).nil? }
  reader
end