Class: Module

Inherits:
Object
  • Object
show all
Defined in:
lib/exceptioner/core_ext/module/attribute_accessors.rb,
lib/exceptioner/core_ext/module/remove_method.rb

Overview

This code is stolen from ActiveSupport gem. We don’t need to pass options like :instance_writer for mattr_ methods so responsible part of code was removed.

Note we don’t overwrite mattr_ methods if they exist already.

Instance Method Summary collapse

Instance Method Details

#mattr_accessor(*syms) ⇒ Object



30
31
32
33
# File 'lib/exceptioner/core_ext/module/attribute_accessors.rb', line 30

def mattr_accessor(*syms)
  mattr_reader(*syms)
  mattr_writer(*syms)
end

#mattr_reader(*syms) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/exceptioner/core_ext/module/attribute_accessors.rb', line 8

def mattr_reader(*syms)
  syms.each do |sym|
    class_eval(<<-EOS, __FILE__, __LINE__ + 1)
      @@#{sym} = nil unless defined? @@#{sym}

      def self.#{sym}
        @@#{sym}
      end
    EOS
  end
end

#mattr_writer(*syms) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/exceptioner/core_ext/module/attribute_accessors.rb', line 20

def mattr_writer(*syms)
  syms.each do |sym|
    class_eval(<<-EOS, __FILE__, __LINE__ + 1)
      def self.#{sym}=(obj)
        @@#{sym} = obj
      end
    EOS
  end 
end

#redefine_method(method, &block) ⇒ Object



7
8
9
10
# File 'lib/exceptioner/core_ext/module/remove_method.rb', line 7

def redefine_method(method, &block)
  remove_possible_method(method)
  define_method(method, &block)
end

#remove_possible_method(method) ⇒ Object



2
3
4
5
# File 'lib/exceptioner/core_ext/module/remove_method.rb', line 2

def remove_possible_method(method)
  remove_method(method)
rescue NameError
end