Sha256: 86df2e3f6a22acf90a304f2a5443c7b8a253498b9fb7a2b12468e17bee916aac

Contents?: true

Size: 1.73 KB

Versions: 44

Compression:

Stored size: 1.73 KB

Contents

require 'active_support/core_ext/kernel/singleton_class'
require 'active_support/core_ext/module/remove_method'

class Class
  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
    # 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.
    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

    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)
    end
end

Version data entries

44 entries across 44 versions & 4 rubygems

Version Path
activesupport-4.1.16 lib/active_support/core_ext/class/delegating_attributes.rb
activesupport-4.1.16.rc1 lib/active_support/core_ext/class/delegating_attributes.rb
activesupport-4.1.15 lib/active_support/core_ext/class/delegating_attributes.rb
activesupport-4.1.15.rc1 lib/active_support/core_ext/class/delegating_attributes.rb
activesupport-4.1.14.2 lib/active_support/core_ext/class/delegating_attributes.rb
activesupport-4.1.14.1 lib/active_support/core_ext/class/delegating_attributes.rb
angular-rails4-templates-0.4.1 vendor/ruby/2.1.0/gems/activesupport-4.1.13/lib/active_support/core_ext/class/delegating_attributes.rb
angular-rails4-templates-0.4.0 vendor/ruby/2.1.0/gems/activesupport-4.1.13/lib/active_support/core_ext/class/delegating_attributes.rb
angular-rails4-templates-0.3.0 vendor/ruby/2.1.0/gems/activesupport-4.1.13/lib/active_support/core_ext/class/delegating_attributes.rb
activesupport-4.1.14 lib/active_support/core_ext/class/delegating_attributes.rb
activesupport-4.1.14.rc2 lib/active_support/core_ext/class/delegating_attributes.rb
activesupport-4.1.14.rc1 lib/active_support/core_ext/class/delegating_attributes.rb
activesupport-4.1.13 lib/active_support/core_ext/class/delegating_attributes.rb
activesupport-4.1.13.rc1 lib/active_support/core_ext/class/delegating_attributes.rb
activesupport-4.1.12 lib/active_support/core_ext/class/delegating_attributes.rb
activesupport-4.1.12.rc1 lib/active_support/core_ext/class/delegating_attributes.rb
activesupport-4.1.11 lib/active_support/core_ext/class/delegating_attributes.rb
activesupport-4.1.10 lib/active_support/core_ext/class/delegating_attributes.rb
activesupport-4.1.10.rc4 lib/active_support/core_ext/class/delegating_attributes.rb
activesupport-4.1.10.rc3 lib/active_support/core_ext/class/delegating_attributes.rb