Ruby methods question

B

bcparanj

I followed the online tutorials on how to write plugins. So I have
acts_as_fox.rb:

require 'active_record'

module Foo
module Acts #:nodoc:
module Fox #:nodoc:

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

# declare the class level helper methods which
# will load the relevant instance methods
# defined below when invoked
module ClassMethods
def acts_as_fox
class_eval do
extend Foo::Acts::Fox::SingletonMethods
end
include Foo::Acts::Fox::InstanceMethods
end
end

# Adds a catch_chickens class method which finds
# all records which have a 'chickens' field set
# to true.
module SingletonMethods
def catch_chickens
find:)all, :conditions => ['chickens = ?', true])
end
# etc...
end

# Adds instance methods.
module InstanceMethods
def eat_chicken
puts "Fox with ID #{self.id} just ate a chicken"
end
end

end
end
end

# reopen ActiveRecord and include all the above to make
# them available to all our models if they want it

ActiveRecord::Base.class_eval do
include Foo::Acts::Fox
end

and Thing.rb

class Thing < ActiveRecord::Base
acts_as_fox
end


My question I want to change the acts_as_fox to something like:

acts_as_fox :pass_something

where I can pass a symbol as a value to the acts_as_fox method.

I tried to modify the working example with no luck. Can someone please
point me in the right direction?
 
J

Jan Svitok

I followed the online tutorials on how to write plugins. So I have
acts_as_fox.rb:

require 'active_record'

module Foo
module Acts #:nodoc:
module Fox #:nodoc:

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

# declare the class level helper methods which
# will load the relevant instance methods
# defined below when invoked
module ClassMethods
def acts_as_fox
class_eval do
extend Foo::Acts::Fox::SingletonMethods
end
include Foo::Acts::Fox::InstanceMethods
end
end

# Adds a catch_chickens class method which finds
# all records which have a 'chickens' field set
# to true.
module SingletonMethods
def catch_chickens
find:)all, :conditions => ['chickens = ?', true])
end
# etc...
end

# Adds instance methods.
module InstanceMethods
def eat_chicken
puts "Fox with ID #{self.id} just ate a chicken"
end
end

end
end
end

# reopen ActiveRecord and include all the above to make
# them available to all our models if they want it

ActiveRecord::Base.class_eval do
include Foo::Acts::Fox
end

and Thing.rb

class Thing < ActiveRecord::Base
acts_as_fox
end


My question I want to change the acts_as_fox to something like:

acts_as_fox :pass_something

where I can pass a symbol as a value to the acts_as_fox method.

I tried to modify the working example with no luck. Can someone please
point me in the right direction?

Don't know anything about plugins, though I suppose

module ClassMethods
def acts_as_fox(arg)
do_something(arg)
class_eval do
extend Foo::Acts::Fox::SingletonMethods
end
include Foo::Acts::Fox::InstanceMethods
end
end

should suffice. If it doesn't work, please specify 1. what are you
trying to achieve, 2. what did you try (preferably include the code)
and 3. what did you get (include error messages and/or describe the
wrong behaviour)

This code is a copy-paste from the tutorial. To find out what's wrong
it's important to see your changes.
 
B

bcparanj

Your code works. I want to make the acts_as_chicken :something to be
available only to those ActiveRecord subclasses that uses the
acts_as_chicken declaration. How can I accomplish that?
Don't know anything about plugins, though I suppose

module ClassMethods
def acts_as_fox(arg)
do_something(arg)
class_eval do
extend Foo::Acts::Fox::SingletonMethods
end
include Foo::Acts::Fox::InstanceMethods
end
end

Loading development environment.NoMethodError: undefined method `catch_chickens' for #<Thing:
0x31d7b28>
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.2/lib/
active_record/base.rb:1861:in `method_missing'
from (irb):3
Thing.catch_chickens
=> [# said:
t.eat_chicken
ArgumentError: wrong number of arguments (0 for 1)
from (irb):5:in `eat_chicken'
from (irb):5?> df
NameError: undefined local variable or method `df' for #<Object:
0x3349ec>
from (irb):7Fox with ID just ate a chicken with name booie
=> nilArgumentError: wrong number of arguments (0 for 1)
from (irb):10:in `eat_chicken'
from (irb):1014:07:07:~/work/plugins/fox > script/console
Loading development environment.NameError: undefined local variable or method `name' for #<Thing:
0x31e2ac8>
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.2/lib/
active_record/base.rb:1861:in `method_missing'
from ./script/../config/../config/../vendor/plugins/
acts_as_fox/lib/acts_as_fox.rb:36:in `eat_chicken'
from (irb):2?> k
NameError: undefined local variable or method `k' for #<Object:
0x3349ec>
from (irb):4
17:54:47:~/work/plugins/fox > script/console
Loading development environment.good
 
E

eden li

It's still really unclear what you're asking (at least to me). Are
you saying you want acts_as_fox to work only if the object has fox ==
true (and the same for chicken)?

Or, are you talking about single table inheritance? If so, you can
just add acts_as_XXX to your sub-classes as appropriate...
 
B

bcparanj

I don't want to pollute the ActiveRecord class by making the
acts_as_chicken available to all of its classes. It must be made
available only to the sub-classes of the ActiveRecord that has the
acts_as_chicken declaration.
 
A

ara.t.howard

I don't want to pollute the ActiveRecord class by making the acts_as_chicken
available to all of its classes. It must be made available only to the
sub-classes of the ActiveRecord that has the acts_as_chicken declaration.

module ActsAsChicken
# stuff
end

class ActiveRecord
def self.inherited other
super
ensure
other.extend ActsAsChicken
end
end

-a
 
B

bcparanj

module ActsAsChicken
# stuff
end

class ActiveRecord
def self.inherited other
super
ensure
other.extend ActsAsChicken
end
end

What is happening in the opened up ActiveRecord class? I am still
learning Ruby. TIA.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top