class Class
Public Instance Methods
superclass_delegating_accessor(name, options = {})
click to toggle source
# File lib/superclass_delegating_accessor/class/delegating_attributes.rb, line 4 def superclass_delegating_accessor(name, options = {}) # Create private _name and _name= methods that can still be used if the public # methods are overridden. _superclass_delegating_accessor("_#{name}", options) # Generate the public methods name, name=, and name?. # These methods dispatch to the private _name, and _name= methods, making them # overridable. singleton_class.send(:define_method, name) { send("_#{name}") } singleton_class.send(:define_method, "#{name}?") { !!send("_#{name}") } singleton_class.send(:define_method, "#{name}=") { |value| send("_#{name}=", value) } # If an instance_reader is needed, generate public instance methods name and name?. if options[:instance_reader] != false define_method(name) { send("_#{name}") } define_method("#{name}?") { !!send("#{name}") } end end
Private Instance Methods
_stash_object_in_method(object, method, instance_reader = true)
click to toggle source
Take the object being set and store it in a method. This gives us automatic inheritance behavior, without having to store the object in an instance variable and look up the superclass chain manually.
# File lib/superclass_delegating_accessor/class/delegating_attributes.rb, line 27 def _stash_object_in_method(object, method, instance_reader = true) singleton_class.remove_possible_method!(method) singleton_class.send(:define_method, method) { object } remove_possible_method!(method) define_method(method) { object } if instance_reader end
_superclass_delegating_accessor(name, options = {})
click to toggle source
# File lib/superclass_delegating_accessor/class/delegating_attributes.rb, line 34 def _superclass_delegating_accessor(name, options = {}) singleton_class.send(:define_method, "#{name}=") do |value| _stash_object_in_method(value, name, options[:instance_reader] != false) end send("#{name}=", nil) singleton_class.send(:private, name, "#{name}=") end