Module: TcravitRubyLib::AppConfig

Extended by:
AppConfig
Included in:
AppConfig
Defined in:
lib/tcravit_ruby_lib/app_config.rb

Overview

Simple and Flexible Configuration Data Storage.

AppConfig provides a simple facility for storing configuration data in an application. It does this using metaprogramming techniques to define accessor methods dynamically when the configuration file is loaded.

This is far from the best way to do this, and was written as an exercise in metaprogramming as much as anything else.

Example

TcravitRubyLib::AppConfig.configure do
    some_value 1023
    another_value "Foo"
end

TcravitRubyLib::AppConfig.some_value      # => 1023
TcravitRubyLib::AppConfig.some_value = 2048
TcravitRubyLib::AppConfig.some_value     # => 2048

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(methname, *args) ⇒ Object

-nodoc-#



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/tcravit_ruby_lib/app_config.rb', line 100

def method_missing(methname, *args)
  if (methname.to_s =~ /^([A-Za-z0-9][A-Za-z0-9\-\_]+)$/)
    if @in_config
      getter_method_name = methname.to_s.to_sym
      setter_method_name = "#{methname}=".to_s.to_sym
      send :define_method, getter_method_name do
        @definitions[getter_method_name]
      end
      send :define_method, setter_method_name do |new_value|
        @definitions[getter_method_name] = new_value
      end
      send setter_method_name, (args.count > 0 ? args.first : nil)
    else
      super
    end
  else
    nil
  end
end

Instance Method Details

#configure(&block) ⇒ Object

Configure the settings.

configure accepts a block which is evaluated by ‘instance_eval`. Method calls inside this block are turned into app settings via #method_missing, which dynamically creates getters and setters for the provided values.

As a side effect, this means you can’t create app settings whose name is the same as a ruby reserved word or a method defined by a ruby ‘Object`, because those calls don’t trigger #method_missing to execute. In practice, this shouldn’t be a huge limitation, but it’s worth being aware of.

Configuration setting names must begin with an uppercase letter, lowercase letter, or digit. Dashes and underscores are permitted after the initial character.

Parameters:

  • block (Block)

    The configuration block.

Returns:

  • Nothing, but stores the configuration values provided inside the block.



70
71
72
73
74
75
76
77
78
# File 'lib/tcravit_ruby_lib/app_config.rb', line 70

def configure(&block)
  @definitions ||= Hash.new

  # The method_missing auto-trigger should only run when we're inside
  # a configure block
  @in_config = true
  instance_eval &block
  @in_config = false
end

#remove!(key) ⇒ Object

@ Remove a previously defined configuration value.

#remove can be passed the name of the configuration setting as a string or a symbol. If the requested setting exists, its value will be discarded, and the getter and setter methods will be undefined. If the requested setting does not exist, no action is taken and no exceptions are raised.

can also be provided for the key.

Parameters:

  • key (String)

    The configuration setting to remove. A symbol

Returns:

  • Nothing.



92
93
94
95
96
97
# File 'lib/tcravit_ruby_lib/app_config.rb', line 92

def remove!(key)
  the_sym = key.to_sym
  @definitions.delete(the_sym) if @definitions.include?(the_sym)
  send :undef_method, the_sym if self.respond_to?(the_sym)
  send :undef_method, "#{the_sym.to_s}=".to_sym if self.respond_to?("#{the_sym.to_s}=".to_sym)
end