Adding class method through included module?

Discussion in 'Ruby' started by ivdkleyn@gmail.com, Feb 22, 2005.

  1. Guest

    Hi there,

    I've written an "Validation" module. Problem is that I would like to
    add a class method to the class in which the module is included, and
    not just an instance method.

    For example: by including a module I would be able to call added
    class-methods like in the following example

    class Test
    include Validation
    validate_members :attr1, :attr2
    rule_set :strict
    end

    Well, that doesn;t work (Ruby protests about instance methods which
    cannot be found).

    So therefore my question: how would I be able to add class methods to a
    class through a Module?

    Regards,

    Iwan
     
    , Feb 22, 2005
    #1
    1. Advertising

  2. <> schrieb im Newsbeitrag
    news:...
    > Hi there,
    >
    > I've written an "Validation" module. Problem is that I would like to
    > add a class method to the class in which the module is included, and
    > not just an instance method.
    >
    > For example: by including a module I would be able to call added
    > class-methods like in the following example
    >
    > class Test
    > include Validation
    > validate_members :attr1, :attr2
    > rule_set :strict
    > end
    >
    > Well, that doesn;t work (Ruby protests about instance methods which
    > cannot be found).
    >
    > So therefore my question: how would I be able to add class methods to a
    > class through a Module?


    module Validation
    def self.included(cl)
    def cl.validate_members(*a) puts "validate_members: #{a.inspect}" end
    def cl.rule_set(set) puts "rule_set: #{set.inspect}" end
    end
    end

    ?> class Test
    >> include Validation
    >> validate_members :attr1, :attr2
    >> rule_set :strict
    >> end

    validate_members: [:attr1, :attr2]
    rule_set: :strict
    => nil

    Kind regards

    robert
     
    Robert Klemme, Feb 22, 2005
    #2
    1. Advertising

  3. Stefan Lang Guest

    wrote:

    > Hi there,
    >
    > I've written an "Validation" module. Problem is that I would like to
    > add a class method to the class in which the module is included, and
    > not just an instance method.
    >
    > For example: by including a module I would be able to call added
    > class-methods like in the following example
    >
    > class Test
    > include Validation
    > validate_members :attr1, :attr2
    > rule_set :strict
    > end
    >
    > Well, that doesn;t work (Ruby protests about instance methods which
    > cannot be found).
    >
    > So therefore my question: how would I be able to add class methods to a
    > class through a Module?
    >
    > Regards,
    >
    > Iwan


    You could include the module for your class object:

    irb(main):001:0> module Validation
    irb(main):002:1> def validate_members(*symbols)
    irb(main):003:2> # do something with symbols
    irb(main):004:2* end
    irb(main):005:1> end
    => nil
    irb(main):006:0> class Test
    irb(main):007:1> class << self
    irb(main):008:2> include Validation
    irb(main):009:2> # more class methods
    irb(main):010:2* end
    irb(main):011:1> validate_members :attr1, :attr2
    irb(main):012:1> end
    => nil
    irb(main):013:0>
     
    Stefan Lang, Feb 22, 2005
    #3

  4. >>So therefore my question: how would I be able to add class methods to a
    >>class through a Module?


    > module Validation
    > def self.included(cl)
    > def cl.validate_members(*a) puts "validate_members: #{a.inspect}" end
    > def cl.rule_set(set) puts "rule_set: #{set.inspect}" end
    > end
    > end


    Works like a charm. Thanks! This one I completely overlooked in the
    Pickaxe book.

    Iwan
     
    Iwan van der Kleyn, Feb 22, 2005
    #4
  5. "Iwan van der Kleyn" <> schrieb im Newsbeitrag
    news:...
    >
    > >>So therefore my question: how would I be able to add class methods to

    a
    > >>class through a Module?

    >
    > > module Validation
    > > def self.included(cl)
    > > def cl.validate_members(*a) puts "validate_members: #{a.inspect}"

    end
    > > def cl.rule_set(set) puts "rule_set: #{set.inspect}" end
    > > end
    > > end

    >
    > Works like a charm. Thanks! This one I completely overlooked in the
    > Pickaxe book.


    Additional note: if you want to be more flexible and more efficient you
    can do this:

    module Validation
    module ClassMethods
    def validate_members(*a) puts "validate_members: #{a.inspect}" end
    def rule_set(set) puts "rule_set: #{set.inspect}" end
    end

    def self.included(cl) cl.extend ClassMethods end
    end

    >> class Test
    >> include Validation
    >> validate_members :attr1, :attr2
    >> rule_set :strict
    >> end

    validate_members: [:attr1, :attr2]
    rule_set: :strict
    => nil
    >> class <<Test; ancestors end

    => [Validation::ClassMethods, Class, Module, Object, Kernel]

    Advantage is that you have only one definition of those class methods plus
    you can change them later.

    And if you want to make that recursively work, just do this:

    module Validation
    module ClassMethods
    def validate_members(*a) puts "validate_members: #{a.inspect}" end
    def rule_set(set) puts "rule_set: #{set.inspect}" end

    # this is the crucial bit:
    def inherited(cl) cl.extend ClassMethods end
    end

    def self.included(cl) cl.extend ClassMethods end
    end

    >> class Test
    >> include Validation
    >> validate_members :attr1, :attr2
    >> rule_set :strict
    >> end

    validate_members: [:attr1, :attr2]
    rule_set: :strict
    => nil
    >> class DerivedTest < Test
    >> validate_members :attr1, :attr2
    >> rule_set :strict
    >> end

    validate_members: [:attr1, :attr2]
    rule_set: :strict
    => nil

    Kind regards

    robert
     
    Robert Klemme, Feb 23, 2005
    #5
    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. Galevsky Gal

    Class instance into included module

    Galevsky Gal, Jun 26, 2007, in forum: Ruby
    Replies:
    8
    Views:
    127
    Galevsky gal
    Jun 26, 2007
  2. Joshua Muheim
    Replies:
    4
    Views:
    104
    Robert Dober
    Nov 23, 2007
  3. Leon Bogaert
    Replies:
    0
    Views:
    128
    Leon Bogaert
    May 28, 2008
  4. Prince Nez
    Replies:
    3
    Views:
    256
    Robert Klemme
    Aug 18, 2009
  5. Iain Barnett
    Replies:
    2
    Views:
    127
    Iain Barnett
    Mar 31, 2011
Loading...

Share This Page