Mike G
Mike G

Reputation: 379

Calling Ruby module methods from a nested class within

Is there an alternative to calling a Module method from a nested class? The code:

module GroupSweeper

  def expire_cache(paths)
    paths.each do |path|
      expire_page(path)
    end
  end

  class SweeperOne < ActionController::Caching::Sweeper
    include GroupSweeper
    observe Subject
    def after_save(subject)
      expire_cache([root_path,subjects_path])
    end
    def after_destroy(subject)
      expire_cache([root_path,subjects_path])
    end 
  end

end

How can I call GroupSweeper's expire_cache method from within SweeperOne without explicitely including it?

Thanks for any input.

Upvotes: 2

Views: 2769

Answers (2)

Jesse Wolgamott
Jesse Wolgamott

Reputation: 40277

You've got some circular dependencies going on here.

  • GroupSweeper defines a nested class of SweeperOne
  • SweeperOne includes GroupSweeper

That won't work.

To answer your ruby method/nested class question:

module MyModule
  def my_method
    puts "yo yo yo"
  end

  class MySweetClass
    def sweet_method
      puts "swweeeeeeeeeeeet"
    end
  end
end

And you want to call MySweetClass's sweet_method, you would change to be:

module MyModule
  def my_method
    puts "yo yo yo"
    MySweetClass.new.sweet_method
  end

  class MySweetClass
    def sweet_method
      puts "swweeeeeeeeeeeet"
    end
  end
end

#....

class MyClass
  include MyModule
end

MyClass.new.my_method

However! I think you're on the wrong track regarding rails' sweepers. This answer is very tactical, but I think you should open a question about what you're trying to do regarding rails sweepers.

Upvotes: 1

Joseph Le Brech
Joseph Le Brech

Reputation: 6653

I may be wrong as i'm pretty new to ruby myself, but the class should probably not include the module that it itself is part of.

maybe if you close the module with an end including it from the class should work.

Upvotes: 0

Related Questions