Module: InheritableClassAttributes::ClassMethods

Defined in:
lib/inheritable_class_attributes.rb

Instance Method Summary collapse

Instance Method Details

#cattr_inheritable_accessor(*symbols) ⇒ Object



45
46
47
48
# File 'lib/inheritable_class_attributes.rb', line 45

def cattr_inheritable_accessor(*symbols)
  cattr_inheritable_writer(*symbols)
  cattr_inheritable_reader(*symbols)
end

#cattr_inheritable_reader(*symbols) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/inheritable_class_attributes.rb', line 21

def cattr_inheritable_reader(*symbols)
  symbols.each do |symbol|
    inheritable_cattr_readers << symbol
    module_eval %{
      def self.#{symbol}
        @#{symbol}
      end
    }
  end
  inheritable_cattr_readers.uniq!
end

#cattr_inheritable_writer(*symbols) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/inheritable_class_attributes.rb', line 33

def cattr_inheritable_writer(*symbols)
  symbols.each do |symbol|
    inheritable_cattr_writers << symbol
    module_eval %{
      def self.#{symbol}=(value)
        @#{symbol} = value
      end
    }
  end
  inheritable_cattr_writers.uniq!
end

#inheritable_cattr_readersObject



13
14
15
# File 'lib/inheritable_class_attributes.rb', line 13

def inheritable_cattr_readers
  @inheritable_class_readers ||= []
end

#inheritable_cattr_writersObject



17
18
19
# File 'lib/inheritable_class_attributes.rb', line 17

def inheritable_cattr_writers
  @inheritable_class_writers ||= []
end

#inherited_with_inheritable_class_attributes(klass) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/inheritable_class_attributes.rb', line 50

def inherited_with_inheritable_class_attributes(klass)
  inherited_without_inheritable_class_attributes(klass) if respond_to?(:inherited_without_inheritable_class_attributes)

  readers = inheritable_cattr_readers.dup
  writers = inheritable_cattr_writers.dup
  inheritables = %i[inheritable_cattr_readers inheritable_cattr_writers]

  (readers + writers + inheritables).uniq.each do |attr|
    var = "@#{attr}"
    old_value = module_eval(var)
    new_value = (begin
                   old_value.dup
                 rescue StandardError
                   old_value
                 end)
    klass.module_eval("#{var} = new_value")
  end
end