Modules in lib not follwing naming convension

Hej! Okey i have a problem, i have earlier written a set of classes in
ruby implementing a msrp chat client and now im trying to use it in my
rails app.

The msrp implementation consists of several files and most of the
functions and classes in those files ar encapsulated in a module lets
call it X. so for instance they look like this

header.rb

module X
class Z

end
end

The problem now is that rails some how gets confused about the names of
the files and what modules and classes they contain or something… For
instance i can instantiate classes in the module but some methods are
not recognised and all is a mess. So i guess my question is how do i
include these files in my project in a good way? without
renaming/reorganizing them.

If the naming doesn’t follow what Rails is looking for, just do a
require ’
header.rb’ in whatever controller / model you want to use said library
in.
Rails already adds lib/ to the load path, so you don’t have to worry
about
finding the correct path.

Jason

Rails follow a very simple pattern to load you modules. If you are
looking for a module SmsChat, rails will look for sms_chat.rb in your
*lib directory and in some /vendor directories too, i think. Try this:

  1. make a sms_chat directory in /lib

    /lib/sms_chat

  2. make a file sms_chat.rb in your /lib directory. This will be
    required whenever you call SmsChat::something.

sms_chat.rb --------------

module SmsChat
end

Required file loader hackery…

lib_path = File.join(File.dirname(FILE), ‘sms_chat’)

will require /lib/sms_chat/header.rb >>

require File.join(lib_path, ‘header.rb’)

will require /lib/sms_chat/other.rb >>

require File.join(lib_path, ‘other’)

-----------------------EOF

  1. If you keep having problems, try reference the module from the
    “global namespace”, just adding two colons before the module:

instead of

   SmsChat::MsrpURI

use

   ::SmsChat::MsrpURI

Thank you for clearing that up to me. Still having troubles tho. It
seems like methods in one or more of the classes wont be defined on
requiring the file, works great in the ruby interpreter but in rails i
get undefined method on methods not declared as a attr_, also the to_s
of MsrpURI class is being ignored and the object standard method get
called.

module SmsChat
class MsrpURI
attr_accessor :username, :password, :host, :port, :resource,
:protocol

def initialize str
  matches = 

/(msrp|sms)://((\w+)(:(\w+))?@)?([\w.]+)(:(\d+))?/?(\w+)?/.match
str
raise “Formatting error” unless matches

  @protocol  = matches[1]
  @username  = matches[3]
  @password = matches[5]
  @host      = matches[6]
  @port      = matches[8].to_i
  @resource = matches[9].to_i

  #port      = 13040 unless port > 0

end

end

def to_s
str = String.new
str << @protocol;
str << “://”
if @username
str << @username
str << “:#{@password}” if @password
str << “@”
end

str << @host
str << ":#{@port}" if @port > 0
 str << "/#{@resource}" if @resource > 0
return str

end
end

That all looks fine, what does the code that uses this look like?

Jason

Followed your suggestion and still no go, it findes the modules now and
i can create the uri object but still when i call to_s function the only
thing i get is the standard Object.to_s object reference… :confused:

the code that is currently using this is a short test function

class SmschatController < ApplicationController
include SmsChat

def test
uri = MsrpURI.new ‘msrp://test’
render :text => uri.to_s
end
end

Thanks for all the help… Im really getting gray hairs over this

Hmm, everything looks good.

I’d say try SmsChat::MsrpURI.new…

and as a sanity check, are you restarting your server for each change to
this library? Only classes that are auto-loaded by Rails (e.g. not
yours)
are reloaded with each request.

Jason

hum just realized that your example only works once… If you try to
visit it twise it cant find the module the second time and you have to
restart the server! :frowning: