Module: CoreExt::BooleanAccessor

Defined in:
lib/core_ext/module/boolean_accessor.rb

Instance Method Summary collapse

Instance Method Details

#boolean_accessor(*syms) ⇒ Object



3
4
5
6
7
8
9
10
11
# File 'lib/core_ext/module/boolean_accessor.rb', line 3

def boolean_accessor(*syms)
  syms.each do |sym|
    class_eval(<<-EOS, __FILE__, __LINE__)
      def #{sym}?; @#{sym} == 1; end
      def #{sym}=(value); @#{sym} = value.to_s.match(/true|yes|on|1/i) ? 1 : 0; end
      def #{sym}; @#{sym}; end
    EOS
  end
end