Module, requires, and attr

D

donn

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.
 
D

dblack

Hi --

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

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

--
David A. Black | (e-mail address removed)
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
 
D

donn

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

Hi --

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

Ignore the || there; I meant just = .


David

--
David A. Black | (e-mail address removed)
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Assignment without attr= 5
Defining attr like methods 3
obj.send(attr) = value 3
jQuery "attr" secrets finally revealed! 4
"attr" like functions 2
Translater + module + tkinter 1
attr 33
about class and module 6

Members online

Forum statistics

Threads
474,219
Messages
2,571,118
Members
47,731
Latest member
unutbu

Latest Threads

Top