Class: Class

Inherits:
Object show all
Defined in:
lib/core_ext.rb

Instance Method Summary collapse

Instance Method Details

#inherited_accessor(accessor, default = nil) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/core_ext.rb', line 62

def inherited_accessor(accessor, default = nil)
  instance_eval <<-RUBY, __FILE__, __LINE__ + 1
    class << self; attr_writer :#{accessor}; end
    @#{accessor} = default

    def #{accessor}
      @#{accessor} ||= superclass.send(:#{accessor}).dup
    end
  RUBY
end

#inherited_property(accessor, default = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/core_ext.rb', line 34

def inherited_property(accessor, default = nil)
  instance_eval <<-RUBY, __FILE__, __LINE__ + 1
    @#{accessor} = default

    def set_#{accessor}(value)
      @#{accessor} = value
    end
    alias #{accessor} set_#{accessor}

    def get_#{accessor}
      return @#{accessor} if instance_variable_defined?(:@#{accessor})
      superclass.send(:get_#{accessor})
    end
  RUBY

  # @path = default
  #
  # def set_path(value)
  #   @path = value
  # end
  # alias_method path, set_path

  # def get_path
  #   return @path if instance_variable_defined?(:path)
  #   superclass.send(:path)
  # end
end