Why isn't my class method recognized? - ruby

I am trying to understand why my class method was not recognized. Below is my code:
wiki_patch.rb
require_dependency 'wiki_content'
module WikiRecipientPatch
def self.included(base)
base.send(:include, InstanceMethods)
base.class_eval do
alias_method_chain :recipients, :send_wiki_mail
end
end
end
module InstanceMethods
def self.set_mail_checker(mail)
#mail_checker = mail
end
end
Rails.configuration.to_prepare do
WikiContent.send(:include, WikiRecipientPatch)
end
controller.rb
WikiContent.set_mail_checker(params[:mail_checker_wiki])
I am getting this error:
NoMethodError (undefined method `set_mail_checker' for #<Class:0x4876560>):
Any idea why it happens and what the solution for it is?

You got the idiom slightly wrong.
ClassMethods/InstanceMethods modules are supposed to be nested in the "main" module (WikiRecipientPatch in this case).
You are including instance methods, but expecting class methods to somehow arise from this? Surely you meant extend ClassMethods, didn't you?
module WikiRecipientPatch
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def set_mail_checker(mail)
'mail checker'
end
end
end
class WikiContent; end
WikiContent.send(:include, WikiRecipientPatch)
WikiContent.set_mail_checker('whatever') # => "mail checker"

Related

lifecycle callbacks on instance and class methods in ruby

module Gym
def self.included(class_or_module)
class_or_module.send(:include, InstanceMethods)
class_or_module.extend(ClassMethods)
end
module ClassMethods
def build
end
end
module InstanceMethods
def open
end
def book_for_practice
end
def close
end
end
end
this is an example in the Ruby's Object Lifecycle Callbacks section of RubyMonk. I don't understand how it's supposed to work or what the point of this is. self.included should just document how the two modules within Gym get used, right? why does class_or_module then get sent/extended? why doesn't it get saved in some sort of arrays that document the lifecyle, like in the examples leading up to this one, such as
##extended_objects = []
def self.extended_objects
##extended_objects
end
def self.extended(class_or_module)
##extended_objects << class_or_module
It's not just documentation. self.included is a callback method that gets called as soon as the module is being included in any other module or class.
Instance methods are included via send, class or module methods via extend in that example.
Find out more in the Ruby documentation.
Let me answer your Question One by one.
1: self.included should just document how the two modules within Gym get used ?
module A
def instance_methods_1
p 'hello instance_methods_1'
end
def instance_methods_2
p 'hello instance_methods_2'
end
module KlassMethods
def klass_methods_1
p 'Hello!! klass_methods_1'
end
def klass_methods_2
p 'Hello!! klass_methods_2'
end
end
end
class B
include A # instead of writing two piece of code like this we could wrap in one using `self.included` method Hook
extend A::KlassMethods
end
B.new.instance_methods_1
B.new.instance_methods_2
B.klass_methods_1
B.klass_methods_2
Another version of same program with method hook using self.included
module A
# this is special method one of the methods hook in ruby.
def self.included(base)
base.extend(KlassMethods)
end
def instance_methods_1
p 'hello instance_methods_1'
end
def instance_methods_2
p 'hello instance_methods_2'
end
module KlassMethods
def klass_methods_1
p 'Hello!! klass_methods_1'
end
def klass_methods_2
p 'Hello!! klass_methods_2'
end
end
end
class B
include A
end
B.new.instance_methods_1
B.new.instance_methods_2
B.klass_methods_1
B.klass_methods_2
2: self.included
To know more about included hook and
Ruby Hook
3: why does class_or_module then get sent/extended ?
module AddAdditionalProperty
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def add_additional_property
p 'ClassMethods::add_additional_property'
end
end
end
module ActiveRecord
class Base
def test
p 'Base test Method'
end
end
end
ActiveRecord::Base.send(:include, AddAdditionalProperty)
ActiveRecord::Base.add_additional_property
## Another version of same Program
module AddAdditionalProperty
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def add_additional_property
p 'ClassMethods::add_additional_property'
end
end
end
module ActiveRecord
class Base
include AddAdditionalProperty
def test
p 'Base test Method'
end
end
end
ActiveRecord::Base.add_additional_property
Hope this answer help you !!!

Ruby Base.send calling it like instance method in model when using include and extend

Ruby library code
module Yaffle
module ActsAsYaffle
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def acts_as_yaffle(options = {})
cattr_accessor :yaffle_text_field
self.yaffle_text_field = (options[:yaffle_text_field] || :last_squawk).to_s
include Yaffle::ActsAsYaffle::LocalInstanceMethods
end
end
...
more code
...
end
end
ActiveRecord::Base.send(:include, ActiveRecord::Acts::Taggable)
Why does when i call the acts_as_yaffle in the model, its being used like
class Hickwall < ActiveRecord::Base
acts_as_yaffle
end
Does the ActiveRecord::Base.send(:include,...) included the ClassMethods as instance eventhough the base is extended (base.extend(ClassMethods) ?
The acts_as_yaffle was declared as a ClassMethods.
It is a class method. Consider this example:
class C
def my_instance_method
puts 'hello from instance'
end
def self.my_class_method
puts 'hello from class'
end
my_instance_method
# NoMethodError
my_class_method
# hello from class
end
inside class C ... end you can only call class methods, because there is no instance to send the method call to. The library code is written using a common pattern to put class methods inside a module. For better understanding, the example class I wrote above is equivalent to this:
module M
def my_instance_method
puts 'hello from instance'
end
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def my_class_method
puts 'hello from class'
end
end
end

how to write a ruby module to allow methods in class definition

Having a hard time figuring this out. Suppose I wanted to write a module, and when included, it would allow classes to define methods by calling a method with symbols
class Anything
include Foo
initializers :hello, :goodbye
end
module Foo
# What goes in here? Its not
# def self.initializers(*symbols)
end
Same syntax idea as attr_accessible. Tried finding it in the Rails source, but, well..
module Foo
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def initializers *names
names.each do |name|
define_method name do
'ok'
end
end
end
end
def self.included(base)
base.extend ClassMethods
end
end
class Anything
include Foo
initializers :hello, :goodbye
end
puts Anything.new.hello #=> ok
for example:
module Foo
def self.included(base)
block = Proc.new do |*symbols|
puts symbols.inspect
end
base.class.send(:define_method, :initializers, block)
end
end
class Anything
include Foo
initializers :one, :two , :three
end

include a Module to a class with a class Method referencing to the Module to be included

I have the following example
class Test
configure_helper
end
module ConfigureHelper
module ClassMethods
def configure_helper
end
end
end
ConfigureHelper has some more functionality which will extend the class with ClassMethods in which the module was included.
So the problem is, if i use the following code to include the module
Test.send :include, ConfigureHelper
The Test class will be loaded and will raise a NoMethodError for configure_helper.
Is there any way to attach the configure_helper method so that configure_helper wont be called?
Why not include the module right in the class definition?
module ConfigureHelper
def self.included base
base.extend ClassMethods
end
module ClassMethods
def configure_helper
end
end
end
class Test
include ConfigureHelper
configure_helper
end
Try it
class Test
end
module ConfigureHelper
module ClassMethods
def self.included(base)
base.class_eval do
def configure_helper
p 'Yes'
end
end
end
end
end
Test.send(:include, ConfigureHelper::ClassMethods)
Test.new.configure_helper

Class variables and extending a module

I have a module like the following
module MyModule
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def foo
##var = 1
end
def bar
puts ##var
end
end
end
class A
include MyModule
foo
end
class B < A; end
so that
B.bar outputs '1'.
However, I would like to have .bar only be defined if .foo is called. I tried
module MyModule
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def foo
##var = 1
extend SingletonMethods
end
module SingletonMethods
def bar
puts ##var
end
end
end
The problem is that
B.bar
returns the error "uninitialized class variable ##var in MyModule::SingletonMethods". How can I make it so that a variable defined in .foo is available to .bar?
use mattr_accessor instead
I was able to access class variables from a module using the self syntax
class User
include Base
##attributes = [:slug, :email, :crypted_password]
end
module Base
def self.included(base)
base.extend ClassMethods
end
def headers
if defined? self.attributes
self.attributes
end
end
end
Now calling User.headers gives me the expected result of
[:slug, :email, :crypted_password]
If anyone can shed more light on why this works exactly so in ruby, please let me know!

Resources