Method: ActiveCouch::Base.define_attr_method

Defined in:
lib/active_couch/base.rb

.define_attr_method(name, value = nil, &block) ⇒ Object

Defines an “attribute” method. A new (class) method will be created with the given name. If a value is specified, the new method will return that value (as a string). Otherwise, the given block will be used to compute the value of the method.

The original method, if it exists, will be aliased, with the new name being prefixed with “original_”. This allows the new method to access the original value.

This method is stolen from ActiveRecord.

Example:

class Foo < ActiveCouch::Base
  define_attr_method :database_name, 'foo'
  # OR
  define_attr_method(:database_name) do
    original_database_name + '_legacy'
  end
end


356
357
358
359
360
361
362
363
# File 'lib/active_couch/base.rb', line 356

def define_attr_method(name, value = nil, &block)
  metaclass.send(:alias_method, "original_#{name}", name)
  if block_given?
    meta_def name, &block
  else
    metaclass.class_eval "def #{name}; #{value.to_s.inspect}; end"
  end
end