简体   繁体   中英

Accessing included class's protected constant in a ActiveSupport::Concern

What's the simplest way to access included class's protected constant in the ActiveSupport::Concern context?

Example classes:

module Printable
    extend ActiveSupport::Concern

private
    def print_constant
        puts MY_CONSTANT
    end
end

class Printer
    include Printable

    def print
        print_constant
    end

private
    MY_CONSTANT = 'Hello'.freeze
end

This solution produces an error:

NameError: uninitialized constant Printable::MY_CONSTANT

I'm aware of an alternative that seems to work:

puts self.class::MY_CONSTANT

But, it doesn't feel right. :-)

Any better suggestions?

First of all, you should put #print_constant into an included block:

module Printable
  extend ActiveSupport::Concern

  included do
    private

    def print_constant
      puts MY_CONSTANT
    end
  end
end

Now there are at least two ways of accessing the class constant MY_CONSTANT :

  1. #included yields with a base parameter similar to Ruby's #included :

     module Printable extend ActiveSupport::Concern included do |base| private define_method :print_constant do puts base::MY_CONSTANT end end end 
  2. Another method is going from self.class :

     module Printable extend ActiveSupport::Concern included do private def print_constant puts self.class::MY_CONSTANT end end end 

ActiveSupport Concern Documentation

Accessing a constant of an including class from a concern isn't really a good idea.

A concern shouldn't have (too much) knowledge of the classes it's included in.

I would go for a common API in the concern and override when needed... like this:

module Printable
    extend ActiveSupport::Concern

private
    def print
      puts ""
    end
end

class Printer
    include Printable

    def print
        MY_CONSTANT
    end

private
    MY_CONSTANT = 'Hello'.freeze
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM