Calling Ruby module methods from a nested class within

Discussion in 'Ruby' started by mgerstenblatt@gmail.com, Mar 16, 2011.

  1. Guest

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

    module GroupSweeper


    =A0 def expire_cache(paths)

    =A0 =A0 paths.each do |path|

    =A0 =A0 =A0 expire_page(path)

    =A0 =A0 end

    =A0 end


    =A0 class SweeperOne < ActionController::Caching::Sweeper

    =A0 =A0 include GroupSweeper

    =A0 =A0 observe Subject

    =A0 =A0 def after_save(subject)

    =A0 =A0 =A0 expire_cache([root_path,subjects_path])

    =A0 =A0 end

    =A0 =A0 def after_destroy(subject)

    =A0 =A0 =A0 expire_cache([root_path,subjects_path])

    =A0 =A0 end

    =A0 end


    end

    How can I call GroupSweeper's expire_cache method from within
    SweeperOne without explicitely including it? Should I not be doing
    this?
     
    , Mar 16, 2011
    #1
    1. Advertising

  2. [Note: parts of this message were removed to make it a legal post.]

    Afaik there is no way to call an instance method of a module without
    including it (or extending an Object with it).
    But you can make expire_cache a module class method and then call it.

    module GroupSweeper
    def self.expire_cache(paths)
    puts "dummy code"
    end
    end

    class Foo < Bar
    def baz(arg)
    GroupSweeper.expire_cache(arg)
    end
    end

    2011/3/16 <>

    > Hi, 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? Should I not be doing
    > this?
    >
    >
     
    Gunther Diemant, Mar 16, 2011
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,844
    Thomas Weidenfeller
    Oct 12, 2005
  2. Ultrus
    Replies:
    3
    Views:
    390
    Stefan Behnel
    Jul 9, 2007
  3. Oltmans
    Replies:
    6
    Views:
    360
    Terry Reedy
    Mar 11, 2009
  4. Nikita Petrov
    Replies:
    2
    Views:
    123
    Gary Wright
    Apr 6, 2008
  5. Kenneth McDonald
    Replies:
    5
    Views:
    345
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page