Sha256: e943d9fe1500236543bfe526ea505129cfaea4a3a17f29741a7ddbd7686a137b

Contents?: true

Size: 1.05 KB

Versions: 226

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

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

226 entries across 211 versions & 26 rubygems

Version Path
activesupport-7.0.4.1 lib/active_support/core_ext/module/aliasing.rb
activesupport-6.1.7.1 lib/active_support/core_ext/module/aliasing.rb
activesupport-6.0.6.1 lib/active_support/core_ext/module/aliasing.rb
rubypitaya-3.12.2 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/core_ext/module/aliasing.rb
activesupport-7.0.4 lib/active_support/core_ext/module/aliasing.rb
activesupport-6.1.7 lib/active_support/core_ext/module/aliasing.rb
activesupport-6.0.6 lib/active_support/core_ext/module/aliasing.rb
activesupport-7.0.3.1 lib/active_support/core_ext/module/aliasing.rb
activesupport-6.1.6.1 lib/active_support/core_ext/module/aliasing.rb
activesupport-6.0.5.1 lib/active_support/core_ext/module/aliasing.rb
activesupport-5.2.8.1 lib/active_support/core_ext/module/aliasing.rb
activesupport-5.2.8 lib/active_support/core_ext/module/aliasing.rb
activesupport-6.0.5 lib/active_support/core_ext/module/aliasing.rb
activesupport-6.1.6 lib/active_support/core_ext/module/aliasing.rb
activesupport-7.0.3 lib/active_support/core_ext/module/aliasing.rb
activesupport-7.0.2.4 lib/active_support/core_ext/module/aliasing.rb
activesupport-6.1.5.1 lib/active_support/core_ext/module/aliasing.rb
activesupport-6.0.4.8 lib/active_support/core_ext/module/aliasing.rb
activesupport-5.2.7.1 lib/active_support/core_ext/module/aliasing.rb
activesupport-5.2.7 lib/active_support/core_ext/module/aliasing.rb