Module, requires, and attr

I’m looking for the best method of coding. I have module. “module
Standards.” It consists of production standard constants and methods
that will be called from all over the place. It is gem’d, and is
implemented with a require method. I would like to change some of the
constants over to accessor variables, so I can change certain settings
on the fly (for testing purposes.) I would like the accessor variable
to have a default value, but I don’t know to accomplish this. (Can I
even have accessor variables/methods in a module?) I would like to get
things as simple as this:

attr :lpClientDrive, true # like to have default value here

def lpClientDrive=(dr)
@lpClientDrive = dr
end

What I currently have (without ability to update) is

def lpClientDrive; “//server/folder/foo/”; end

For testing purposes, I would like to re assign the client drive, as
testing will be done on a non-production machine.

Hi –

On Tue, 28 Nov 2006, [email protected] wrote:

attr :lpClientDrive, true # like to have default value here

def lpClientDrive=(dr)
@lpClientDrive = dr
end

You don’t need that setter method, as the “true” flag will cause both
reader and writer methods to be created. (The “true” flag is
considered a bit obscure; it’s more usual to use attr_accessor.)

What I currently have (without ability to update) is

def lpClientDrive; “//server/folder/foo/”; end

For testing purposes, I would like to re assign the client drive, as
testing will be done on a non-production machine.

It sounds like what you want, in essence, is:

module Standards
def lpClientDrive
@lpClientDrive || “//server/folder/foo”
end

 def lpClientDrive=(dr)
   @lpClientDriver ||= dr
 end

end

You could write a nice attr_*-style method to generate these. Here’s
an example:

module AttrExtensions
def attr_with_default(attr,default)
attr_writer attr
define_method(attr) do
instance_variable_get("@#{attr}") || default
end
end
end

module MyModule
extend AttrExtensions
attr_with_default(“x”, 100)
end

class C
include MyModule
end

c = C.new
p c.x # 100 (the default)
c.x = 200
p c.x # 200 (the newly-assigned value)

David

Hi –

On Tue, 28 Nov 2006, [email protected] wrote:

def lpClientDrive=(dr)
@lpClientDriver ||= dr

Ignore the || there; I meant just = .

David

Thank you very much. This is the kind of information I need to learn.
Thanks for everything.
dvn