Service objects pattern in Ruby on Rails - ruby

I´m trying to develop a service class that provides payment services in my Rails app, but it´s not working.
Service class (lib/paypal_service.rb) (not sure if it should be placed here, I read it in some posts):
class PaypalService
attr_reader :api #, :express_checkout_response
def initialize()
#api = PayPal::SDK::Merchant::API.new
end
def test()
puts "Congratulations, you have called test"
end
end
Controller (uses service):
class BookingsController < ApplicationController
include BoatsHelper
require 'paypal_service'
def create
PaypalService.test
end
...
In output I get:
NoMethodError (private method `test' called for PaypalService:Class):

It's because you are calling a class method, but you have defined an instance method.
Change you controller to this
def create
PaypalService.new.test
end
Or define a class method and leave your controller as is
class PaypalService
attr_reader :api #, :express_checkout_response
def initialize()
#api = PayPal::SDK::Merchant::API.new
end
def self.test
new.test
end
def test()
puts "Congratulations, you have called test"
end
end

Use PaypalService.new.test instead of PaypalService.test as test is an instance method of class PaypalService and not a class method. Update it as below:
class BookingsController < ApplicationController
include BoatsHelper
require 'paypal_service'
def create
PaypalService.new.test
end
...
NOTE:
If you want to call it as PaypalService.test then you can convert test to a class method as follows:
class PaypalService
attr_reader :api #, :express_checkout_response
def initialize
#api = PayPal::SDK::Merchant::API.new
end
def self.test
puts "Congratulations, you have called test"
end
end

Related

Calling Methods from Inherited Class in Ruby

I have the following classes:
Module
module AlertService
module MessageTemplate
def generate_message
"test"
end
end
end
Parent class:
module Client
def post uri, params={}
Net::HTTP.post_form uri, params
end
end
module AlertService
class BaseAlert
extend MessageTemplate
include Singleton
include Client
def initialize; end
end
end
Child Class:
module AlertService
class TestAlert < BaseAlert
include Singleton
def initialize
options = {
username: "Screen Alert Bot",
http_client: Client
}
#notifier = Slack::Notifier.new(rails.config.url, options)
end
def self.create_message
message = generate_message
end
def self.send_message
create_message
#notifier.post blocks: message
end
end
end
I can create the test alert like this: s= AlertService::TestAlert
But I get the error when I do this:
s.send_message
NoMethodError: undefined method `generate_message' for AlertService::TestAlert::Class
generate_message is a method from the MessageTemplate module included in the BaseAlert class. Why is it saying my inherited class doesn't have access to the method?
You're not using Singleton correctly. You're including it, but then not using it, instead bypassing that altogether and calling class methods that have nothing to do with Singleton. They're in turn calling class methods on the parent class that don't exist.
The solution is to use Singleton as intended:
module AlertService
class BaseAlert
include MessageTemplate
include Singleton
def initialize
end
end
end
module AlertService
class TestAlert < BaseAlert
def initialize
#notifier = Slack::Notifier.new(Rails.configuration.url, Rails.configuration.options)
end
def create_message
message = generate_message
end
def send_message
create_message
#notifier.post blocks: message
end
end
end
Where now you call with instance as documented:
AlertService::TestAlert.instance.send_message

How can I test delegating methods using SimpleDelegator and RSpec?

I'm using Ruby 1.9.3 and trying to make some tests with RSpec.
I have a class:
class A
def method1
"test"
end
end
class B < SimpleDelegator
def initialize(events)
#events = events
end
end
Now I'm trying to test delegation behaviour:
require 'spec_helper'
RSpec.describe B do
let(:a) { A.new }
let(:b) { B.new(a) }
it "Should delegate unknown calls to A object" do
expect(b.method1).not_to eq(nil)
end
end
I get the following error:
NoMethodError:
undefined method `method1' for nil:B
Seems that the test would pass if add method_missing manually:
class B < SimpleDelegator
def initialize(events)
#events = events
end
def method_missing(meth, *args, &blk)
#events.send(meth, *args, &blk)
end
end
What I'm doing wrong here?
Thanks
The problem is that you added a initializer to the class B without calling super and passing the instance you want to decorate. Your code should look like this:
class A
def method1
"test"
end
end
class B < SimpleDelegator
def initialize(events)
#events = events
super(events)
end
end
You don't need to define an initialize method on B. SimpleDelegator defines one for you. When you defined your own initialize method, you overrode the initialize method you inherited from the SimpleDelegator class.
Try this:
class A
def method1
"test"
end
end
class B < SimpleDelegator
end
This is from irb: B.new(A.new).method1 #=> "test"
You could define your own initialize method and call super, but I wouldn't unless you really had to.

Call parent method in child class

I'm learning the object model of Ruby. I've written this script:
#/usr/bin/ruby
module MyModule
class MyBase
def class_b_method
puts "class_b_method called"
end
end
class MyClass < MyBase
attr_accessor :name
class_b_method
def set_name(name)
#name = "My name is #{name}"
end
def display_name
return #name
end
end
end
obj = MyModule::MyClass.new
obj.set_name "Martin"
puts obj.display_name
Running the code above I get this error:
module.rb:13: undefined local variable or method `class_b_method' for MyModule::MyClass:Class (NameError)
I'm trying to call the parent method within the class MyClass. What I'm doing wrong?
Inside class MyClass,self is MyClass.But you define class_b_method as an instance method inside class MyBase,i.e. method which can be called by the instances of the class MyBase,can't be invoked by the class itself. so self.class_b_method throws an legitimate error.To make your code workable write the method as below:
class MyBase
def self.class_b_method
puts "class_b_method called"
end
end

Rails - how to call methods from lib directory?

I have this method in the lib dir (file my_class_name.rb):
class MyClassName
def doSomething
...
end
...
end
in the controller:
class UsersController < ApplicationController
require 'my_class_name'
def show_stats
::MyClassName.doSomething()
end
end
returns
undefined method `doSomething' for MyClassName:Class
How to properly call this method?
You've written a class with an instance method, so if you want to call it how you've written it you'll need to write:
mcn = MyClassName.new
mcn.doSomething
(by creating an instance, and then calling the method on that instance)
If what you want is a class method, define it as:
class MyClassName
def self.doSomething
...
end
...
end
and call it like: MyClassName.doSomething
class MyClassName
def self.doSomething
...
end
...
end
You have made and instance method not class method, change your code as follow, plus i would suggest you instead of making it as a class make it a module and include in your model and call doSomething from model.
class MyClassName
def self.doSomething
...
end
...
end
class UsersController < ApplicationController
require 'my_class_name'
def show_stats
MyClassName.doSomething()
end
end
If you are looking to make it work as is (with tiny change) then you should create an instance of MyClassName e.g. ::MyClassName.new.doSomething()
class MyClassName
def doSomething
...
end
...
end
class UsersController < ApplicationController
require 'my_class_name'
def show_stats
::MyClassName.new.doSomething()
end
end

Calling a class method within a class

I realize this perhaps a naive question but still I cant figure out how to call one method from another in a Ruby class.
i.e. In Ruby is it possible to do the following:
class A
def met1
end
def met2
met1 #call to previously defined method1
end
end
Thanks,
RM
Those aren't class methods, they are instance methods. You can call met1 from met2 in your example without a problem using an instance of the class:
class A
def met1
puts "In met1"
end
def met2
met1
end
end
var1 = A.new
var1.met2
Here is the equivalent using class methods which you create by prefixing the name of the method with its class name:
class A
def A.met1
puts "In met1"
end
def A.met2
met1
end
end
A.met2

Resources