Sha256: 0e6172603517de1fb7a0901bfa35715743e1b2df4145e464310b9e1246002518

Contents?: true

Size: 1.02 KB

Versions: 24

Compression:

Stored size: 1.02 KB

Contents

class Module
  # Allows you to make aliases for attributes, which includes
  # getter, setter, and a predicate.
  #
  #   class Content < ActiveRecord::Base
  #     # has a title attribute
  #   end
  #
  #   class Email < Content
  #     alias_attribute :subject, :title
  #   end
  #
  #   e = Email.find(1)
  #   e.title    # => "Superstars"
  #   e.subject  # => "Superstars"
  #   e.subject? # => true
  #   e.subject = "Megastars"
  #   e.title    # => "Megastars"
  def alias_attribute(new_name, old_name)
    # The following reader methods use an explicit `self` receiver in order to
    # support aliases that start with an uppercase letter. Otherwise, they would
    # be resolved as constants instead.
    module_eval <<-STR, __FILE__, __LINE__ + 1
      def #{new_name}; self.#{old_name}; end          # def subject; self.title; end
      def #{new_name}?; self.#{old_name}?; end        # def subject?; self.title?; end
      def #{new_name}=(v); self.#{old_name} = v; end  # def subject=(v); self.title = v; end
    STR
  end
end

Version data entries

24 entries across 24 versions & 4 rubygems

Version Path
sigterm_extensions-0.0.4 lib/sigterm_extensions/core_ext/module/aliasing.rb
activesupport-5.1.7 lib/active_support/core_ext/module/aliasing.rb
activesupport-5.1.7.rc1 lib/active_support/core_ext/module/aliasing.rb
activesupport-5.1.6.2 lib/active_support/core_ext/module/aliasing.rb
activesupport-5.1.6.1 lib/active_support/core_ext/module/aliasing.rb
activesupport-5.1.6 lib/active_support/core_ext/module/aliasing.rb
tdiary-5.0.8 vendor/bundle/gems/activesupport-5.1.5/lib/active_support/core_ext/module/aliasing.rb
activesupport-5.1.5 lib/active_support/core_ext/module/aliasing.rb
activesupport-5.1.5.rc1 lib/active_support/core_ext/module/aliasing.rb
pract6-0.1.0 .gem/ruby/2.3.0/gems/activesupport-5.1.4/lib/active_support/core_ext/module/aliasing.rb
activesupport-5.1.4 lib/active_support/core_ext/module/aliasing.rb
activesupport-5.1.4.rc1 lib/active_support/core_ext/module/aliasing.rb
activesupport-5.1.3 lib/active_support/core_ext/module/aliasing.rb
activesupport-5.1.3.rc3 lib/active_support/core_ext/module/aliasing.rb
activesupport-5.1.3.rc2 lib/active_support/core_ext/module/aliasing.rb
activesupport-5.1.3.rc1 lib/active_support/core_ext/module/aliasing.rb
tdiary-5.0.5 vendor/bundle/gems/activesupport-5.1.2/lib/active_support/core_ext/module/aliasing.rb
activesupport-5.1.2 lib/active_support/core_ext/module/aliasing.rb
activesupport-5.1.2.rc1 lib/active_support/core_ext/module/aliasing.rb
activesupport-5.1.1 lib/active_support/core_ext/module/aliasing.rb