Class: Class
- Inherits:
-
Object
- Object
- Class
- Defined in:
- lib/class_attr.rb
Instance Method Summary collapse
-
#class_attr_accessor(*names) ⇒ Object
Executes #class_attr_reader and #class_attr_writer.
-
#class_attr_reader(*args) ⇒ Object
Defines a method that allows you to read an instance variable set at the class level.
-
#class_attr_writer(*args) ⇒ Object
Defines a method for to write to an instance varaible set at the class level.
-
#inheritable_class_attr_accessor(*names) ⇒ Object
Executes #inheritable_class_attr_reader and #inheritable_class_attr_writer.
-
#inheritable_class_attr_reader(*args) ⇒ Object
Creates a class and instance method to read the class level variable(s) with the name(s) provided.
-
#inheritable_class_attr_writer(*args) ⇒ Object
The same as #class_attr_writer.
Instance Method Details
#class_attr_accessor(*names) ⇒ Object
Executes #class_attr_reader and #class_attr_writer.
93 94 95 96 |
# File 'lib/class_attr.rb', line 93 def class_attr_accessor(*names) class_attr_reader(*names) class_attr_writer(*names) end |
#class_attr_reader(*args) ⇒ Object
Defines a method that allows you to read an instance variable set at the class level. Also defines an instance method that reads the same thing. Comparable to #attr_reader for the class level.
So in summary defines: .var (which gets @var) and
#var (which gets self.class.var)
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/class_attr.rb', line 29 def class_attr_reader(*args) names, default = separate_argument_list_and_default(args) names.each do |name| class_eval <<-EOS def self.#{name} @#{name} end def #{name} self.class.send(:#{name}) end EOS self.instance_variable_set("@#{name}", (default.dup rescue default)) end end |
#class_attr_writer(*args) ⇒ Object
Defines a method for to write to an instance varaible set at the class level. Comparable to #attr_writer for the class level.
So in summary defines: .var= (which sets @var)
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/class_attr.rb', line 65 def class_attr_writer(*args) names, default = separate_argument_list_and_default(args) names.each do |name| class_eval <<-EOS def self.#{name}=(obj) @#{name} = obj end EOS end end |
#inheritable_class_attr_accessor(*names) ⇒ Object
Executes #inheritable_class_attr_reader and #inheritable_class_attr_writer.
163 164 165 166 |
# File 'lib/class_attr.rb', line 163 def inheritable_class_attr_accessor(*names) inheritable_class_attr_reader(*names) inheritable_class_attr_writer(*names) end |
#inheritable_class_attr_reader(*args) ⇒ Object
Creates a class and instance method to read the class level variable(s) with the name(s) provided. If no value has been set it attempts to use the value of the superclass.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/class_attr.rb', line 104 def inheritable_class_attr_reader(*args) names, default = separate_argument_list_and_default(args) names.each do |name| class_eval <<-EOS def self.#{name} if @#{name} @#{name} elsif superclass.respond_to? :#{name} @#{name} ||= superclass.#{name} end end def #{name} self.class.send(:#{name}) end EOS self.instance_variable_set("@#{name}", (default.dup rescue default)) end end |
#inheritable_class_attr_writer(*args) ⇒ Object
The same as #class_attr_writer.
125 126 127 |
# File 'lib/class_attr.rb', line 125 def inheritable_class_attr_writer(*args) class_attr_writer(*args) end |