Class: Module

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

Instance Method Summary collapse

Instance Method Details

#attr_accessor_with_default(sym, default = nil, &block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/support/class_options.rb', line 26

def attr_accessor_with_default(sym, default = nil, &block)
  raise 'Default value or block required' unless !default.nil? || block
  define_method(sym, block_given? ? block : Proc.new { default })
  module_eval(<<-EVAL, __FILE__, __LINE__)
    def #{sym}=(value)
      class << self; attr_reader :#{sym} end
      @#{sym} = value
    end
  EVAL
end