Sha256: 17ca9a71703dbfdb79d8d40d06b4c54ee13afda7f78b33de71da55925b26169f

Contents?: true

Size: 1.84 KB

Versions: 56

Compression:

Stored size: 1.84 KB

Contents

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


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

  deprecate superclass_delegating_accessor: :class_attribute

  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

56 entries across 54 versions & 10 rubygems

Version Path
activesupport-4.2.11.3 lib/active_support/core_ext/class/delegating_attributes.rb
activesupport-4.2.11.2 lib/active_support/core_ext/class/delegating_attributes.rb
cocoapods-dependency-html-0.0.2 vendor/bundle/gems/activesupport-4.2.11.1/lib/active_support/core_ext/class/delegating_attributes.rb
cocoapods-dependency-html-0.0.1 vendor/bundle/gems/activesupport-4.2.11.1/lib/active_support/core_ext/class/delegating_attributes.rb
activesupport-4.2.11.1 lib/active_support/core_ext/class/delegating_attributes.rb
activesupport-4.2.11 lib/active_support/core_ext/class/delegating_attributes.rb
activesupport-4.2.10 lib/active_support/core_ext/class/delegating_attributes.rb
activesupport-4.2.10.rc1 lib/active_support/core_ext/class/delegating_attributes.rb
activesupport-4.2.9 lib/active_support/core_ext/class/delegating_attributes.rb
activesupport-4.2.9.rc2 lib/active_support/core_ext/class/delegating_attributes.rb
activesupport-4.2.9.rc1 lib/active_support/core_ext/class/delegating_attributes.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/activesupport-4.2.8/lib/active_support/core_ext/class/delegating_attributes.rb
activesupport-4.2.8 lib/active_support/core_ext/class/delegating_attributes.rb
activesupport-4.2.8.rc1 lib/active_support/core_ext/class/delegating_attributes.rb
activesupport-4.2.7.1 lib/active_support/core_ext/class/delegating_attributes.rb
activesupport-4.2.7 lib/active_support/core_ext/class/delegating_attributes.rb
activesupport-4.2.7.rc1 lib/active_support/core_ext/class/delegating_attributes.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/core_ext/class/delegating_attributes.rb
activesupport-4.2.6 lib/active_support/core_ext/class/delegating_attributes.rb
activesupport-4.2.6.rc1 lib/active_support/core_ext/class/delegating_attributes.rb