2015-02-23 5 views
12

Rozwijając moje pytanie tutaj (ruby/rails: extending or including other modules), używając mojego istniejącego rozwiązania, jaki jest najlepszy sposób ustalenia, czy mój moduł jest dołączony?ruby ​​/ rails: Jak ustalić, czy moduł jest dołączony?

Co na razie zrobiłem, to zdefiniowałem metody instancji w każdym module, więc gdy zostaną one włączone, dostępna byłaby metoda, a następnie dodałem moduł pobierania (method_missing()) do modułu nadrzędnego, aby można było wykryć, jeśli nie są w zestawie. Mój kod rozwiązanie wygląda następująco:

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 

więc moim modelu mam:

# 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 

A potem mogę

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

Czy to piszę poprawnie? Czy jest jakiś lepszy sposób?

+1

Pytanie jest nieco zawiłe, więc nie jestem pewien, czy to jest to, czego szukasz, ale do sprawdzenia jeśli model jest dołączony, możesz wykonać polecenie 'object.class.include? Module' – makhan

+0

Możesz użyć 'respond_to?', Aby sprawdzić, czy metoda jest dostępna. – Lesleh

Odpowiedz

29

Jeśli dobrze rozumiem Twoje pytanie, można to zrobić:

Man.included_modules.include?(Features)? 

Na przykład:

module M 
end 

class C 
    include M 
end 

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

jak

C.included_modules 
    #=> [M, Kernel] 

Inne sposoby:

jako @ Markan wspomniał:

C.include? M 
    #=> true 

czyli

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

lub po prostu:

C < M 
    #=> true 
+0

Rewizja dla składni "C makhan

+0

@makhan, jest to metoda [Module # <] (http://ruby-doc.org//core-2.2.0/Module.html#method-- 3C). –

+0

@makhan "Eloquent Ruby" od Russ Olsen jest miłym (i dość obszernym) przeglądem idów Ruby. – TNT

Powiązane problemy