ruby / rails:如何确定是否包含模块?

时间:2022-05-24 04:09:49

Expanding on my question here (ruby/rails: extending or including other modules), using my existing solution, what's the best way to determine if my module is included?

在这里扩展我的问题(ruby / rails:扩展或包括其他模块),使用我现有的解决方案,确定我的模块是否包含在内的最佳方法是什么?

What I did for now was I defined instance methods on each module so when they get included a method would be available, and then I just added a catcher (method_missing()) to the parent module so I can catch if they are not included. My solution code looks like:

我现在做的是我在每个模块上定义了实例方法,所以当它们被包含时,一个方法可用,然后我只是将一个catcher(method_missing())添加到父模块,所以我可以捕获它们是否包含它们。我的解决方案代码如下:

module Features
  FEATURES = [Running, Walking]

  # include Features::Running
  FEATURES.each do |feature|
    include feature
  end

  module ClassMethods
    # include Features::Running::ClassMethods
    FEATURES.each do |feature|
      include feature::ClassMethods
    end
  end

  module InstanceMethods
    def method_missing(meth)
      # Catch feature checks that are not included in models to return false
      if meth[-1] == '?' && meth.to_s =~ /can_(\w+)\z?/
        false
      else
        # You *must* call super if you don't handle the method,
        # otherwise you'll mess up Ruby's method lookup
        super
      end
    end
  end

  def self.included(base)
    base.send :extend, ClassMethods
    base.send :include, InstanceMethods
  end
end

# lib/features/running.rb
module Features::Running
  module ClassMethods
    def can_run
      ...

      # Define a method to have model know a way they have that feature
      define_method(:can_run?) { true }
    end
  end
end

# lib/features/walking.rb
module Features::Walking
  module ClassMethods
    def can_walk
      ...

      # Define a method to have model know a way they have that feature
      define_method(:can_walk?) { true }
    end
  end
end

So in my models I have:

所以在我的模特中我有:

# Sample models
class Man < ActiveRecord::Base
  # Include features modules
  include Features

  # Define what man can do
  can_walk
  can_run
end

class Car < ActiveRecord::Base
  # Include features modules
  include Features

  # Define what man can do
  can_run
end

And then I can

然后我可以

Man.new.can_walk?
# => true
Car.new.can_run?
# => true
Car.new.can_walk? # method_missing catches this
# => false

Did I write this correctly? Or is there a better way?

我写得对吗?或者,还有更好的方法?

1 个解决方案

#1


35  

If I understand your question correctly, you can do this:

如果我理解你的问题,你可以这样做:

Man.included_modules.include?(Features)?

For example:

例如:

module M
end

class C
  include M
end

C.included_modules.include?(M)
  #=> true

as

C.included_modules
  #=> [M, Kernel]

Other ways:

其他方法:

as @Markan mentioned:

正如@Markan所说:

C.include? M
  #=> true

or:

要么:

C.ancestors.include?(M)
  #=> true

or just:

要不就:

C < M
  #=> true 

#1


35  

If I understand your question correctly, you can do this:

如果我理解你的问题,你可以这样做:

Man.included_modules.include?(Features)?

For example:

例如:

module M
end

class C
  include M
end

C.included_modules.include?(M)
  #=> true

as

C.included_modules
  #=> [M, Kernel]

Other ways:

其他方法:

as @Markan mentioned:

正如@Markan所说:

C.include? M
  #=> true

or:

要么:

C.ancestors.include?(M)
  #=> true

or just:

要不就:

C < M
  #=> true