# Merb

**URL:** <https://www.ruby-forum.com/t/merb/166885>\
**Category:** Ruby\
**Created:** [April 30, 2009, 3:17pm UTC](https://www.ruby-forum.com/t/merb/166885 "2009-04-30T15:17:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mike-moscow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/53a042/32.png) [@mike-moscow](https://www.ruby-forum.com/u/mike-moscow)\
**Post date:** [April 30, 2009, 3:17pm UTC](https://www.ruby-forum.com/t/merb/166885/1 "2009-04-30T15:17:57Z")

</div>

Hi I’m writing a class in /lib,  
I would like the class accessing the “request” object

have you an idea ?

Thx

---

<div class="post-metadata">

**Author:** ![mike-moscow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/53a042/32.png) [@mike-moscow](https://www.ruby-forum.com/u/mike-moscow)\
**Post date:** [April 30, 2009, 3:35pm UTC](https://www.ruby-forum.com/t/merb/166885/2 "2009-04-30T15:35:42Z")

</div>

Mike Mickey wrote:

> Hi I’m writing a class in /lib,  
> I would like the class accessing the “request” object
> 
> have you an idea ?

Don’t. If it accesses that, it’s a Controller, no matter where you put  
it.

Now, can you pass what you need into the class in lib?

And if the class is modular (even if it has no database hooks), should  
it be a  
model?

And do you have unit tests on it, so you can refactor it as you learn  
the best  
role for it?

(But note that questions which are actually about Merb belong on its  
cheerful,  
friendly, and mature mailing list…)

---

<div class="post-metadata">

**Author:** ![mike-moscow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/53a042/32.png) [@mike-moscow](https://www.ruby-forum.com/u/mike-moscow)\
**Post date:** [April 30, 2009, 3:40pm UTC](https://www.ruby-forum.com/t/merb/166885/3 "2009-04-30T15:40:30Z")

</div>

> Now, can you pass what you need into the class in lib?

actually i need a lot of information from the request object

> And if the class is modular (even if it has no database hooks), should  
> it be a  
> model?

I thought a model could not access the request ?

> And do you have unit tests on it, so you can refactor it as you learn  
> the best  
> role for it?

not unit tests for it (for the moment)

---

<div class="post-metadata">

**Author:** ![mike-moscow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/53a042/32.png) [@mike-moscow](https://www.ruby-forum.com/u/mike-moscow)\
**Post date:** [April 30, 2009, 5:07pm UTC](https://www.ruby-forum.com/t/merb/166885/4 "2009-04-30T17:07:55Z")

</div>

> What is the class doing? I can think of things like correlating the  
> user\_agent  
> with the IP and the login history, to detect excessive login attempts…

it’s an API wrapper but a lot of calls are cached in the session, so i  
need to access the session in all the function of the class.

Have you a better solution ?

---

<div class="post-metadata">

**Author:** ![mike-moscow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/53a042/32.png) [@mike-moscow](https://www.ruby-forum.com/u/mike-moscow)\
**Post date:** [April 30, 2009, 4:03pm UTC](https://www.ruby-forum.com/t/merb/166885/5 "2009-04-30T16:03:19Z")

</div>

Mike Mickey wrote:

> > Now, can you pass what you need into the class in lib?
> 
> actually i need a lot of information from the request object

What is the class doing? I can think of things like correlating the  
user\_agent  
with the IP and the login history, to detect excessive login attempts…

> I thought a model could not access the request ?

For the same reason a lib item can’t see the request.

Just pass the request into your top method.

> not unit tests for it (for the moment)

Write the tests first! Don’t write them “last” - you will have no  
incentive to  
get back to it.

---

<div class="post-metadata">

**Author:** ![mike-moscow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/53a042/32.png) [@mike-moscow](https://www.ruby-forum.com/u/mike-moscow)\
**Post date:** [May 1, 2009, 2:46am UTC](https://www.ruby-forum.com/t/merb/166885/6 "2009-05-01T02:46:01Z")

</div>

Mike Mickey wrote:

> it’s an API wrapper but a lot of calls are cached in the session, so i  
> need to access the session in all the function of the class.
> 
> Have you a better solution ?

Better than passing the whole object in? you said you need the whole  
object; go  
ahead and rely on OO and encapsulation. There’s nothing architecturally  
wrong  
with passing a whole object in.

The Merb mailing list might have other ideas…

news://news.gmane.org:119/gmane.comp.web.merb.devel

…but be prepared to answer the same questions as I asked.
