user3675188
user3675188

Reputation: 7419

How to define custom class and use it in helper

I want to define a class and let many helpers use.

I can include MvaasPortal moude in fine,

Then I can new the object , but can not use any methods of the object,

It's so strange.

If I can not use the methods in the object, why I can new the object. Ruby is so strange.

 @portal = Portal.new

There is no methods in @portal object

mvaas_portal.rb

module MvaasPortal
    module InstanceMethods
      class Portal
        def initialize(server_url)
      ~~~~
        end
        def query_server(body_to_send={},session_id=nil)
      ~~~
        end
      end
    end

    def self.included(receiver)
        receiver.send :include, InstanceMethods
    end
end

Upvotes: 0

Views: 46

Answers (1)

Skelz0r
Skelz0r

Reputation: 114

If you're using rails, you can use ActiveSupport::Concern : http://api.rubyonrails.org/classes/ActiveSupport/Concern.html If don't, take a look at the first example on the link.

Moreover, your namespace is a little bit weird and misses some context. Here is an example with a dummy method :

require 'active_support/concern'

module MvaasPortal
  include ActiveSupport::Concern

  def an_instance_method
    puts "Here!"
  end
end

class Portal
  include MvaasPortal
end

Portal.new.an_instance_method
=> "Here!"

Upvotes: 3

Related Questions