# Helper method for facade column

**URL:** <https://www.ruby-forum.com/t/helper-method-for-facade-column/117396>\
**Category:** Rails\
**Created:** [October 16, 2007, 4:39pm UTC](https://www.ruby-forum.com/t/helper-method-for-facade-column/117396 "2007-10-16T16:39:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![spike666](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/13edae/32.png) [@spike666](https://www.ruby-forum.com/u/spike666)\
**Post date:** [October 16, 2007, 4:39pm UTC](https://www.ruby-forum.com/t/helper-method-for-facade-column/117396/1 "2007-10-16T16:39:28Z")

</div>

Hey everyone,

I’ve got a facade column set up in 2 of my models. Specifically, I’m  
storing an IPv4 address as a 32-bit integer in my database and my model  
is set up to only return the IP in dotted notation (ie: 176942002 -\>  
‘10.139.235.178’).

Since I’m using this in more than one place, and I may use it in another  
project as well, I’d like to somehow make a plugin or at the very least  
extend my ApplicationController with a function to automatically handle  
this.

I’d like it to work something like this:

class SomeController \< ApplicationController  
ipv4\_address :ip\_address  
end

where :ip\_address is the name of the column that I’d like to create the  
facade.

is there any way of doing this?

Thanks!

…spike

---

<div class="post-metadata">

**Author:** ![spike666](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/13edae/32.png) [@spike666](https://www.ruby-forum.com/u/spike666)\
**Post date:** [October 16, 2007, 7:32pm UTC](https://www.ruby-forum.com/t/helper-method-for-facade-column/117396/2 "2007-10-16T19:32:12Z")

</div>

Hi Spike,

Certainly you can. Best idea is to create yourself a plug-in with  
something like:

module MyPlugin  
def self.append\_features(base)  
base.extend ClassMethods  
end

```
module ClassMethods
  def ipv4_address field
     class_eval do
       define_method field.to_sym do
         # code here to return the address
       end
     end
  end
end

```

end

ActiveRecord::Base.class\_eval do  
include MyPlugin  
end

This should allow you to do:

class Testing \< ActiveRecord::Base  
ipv4\_address :me  
end

t = Testing.new  
[t.me](http://t.me)

---

<div class="post-metadata">

**Author:** ![spike666](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/13edae/32.png) [@spike666](https://www.ruby-forum.com/u/spike666)\
**Post date:** [October 16, 2007, 8:10pm UTC](https://www.ruby-forum.com/t/helper-method-for-facade-column/117396/3 "2007-10-16T20:10:29Z")

</div>

Thanks, Dave!

I’ve got limited experience with using modules and mixins in Ruby, so…

Currently, I implement 3 methods for each column:

ip\_address  
ip\_address=  
ip\_address\_before\_typecast

if I were to do the above call to “ipv4\_address :my\_column” and wanted  
to get definitions for my\_column, my\_column=, and  
my\_column\_before\_typecast, how I would I do that?

I think I understand the rest of what you said. I’m going to attempt an  
implementation, now.

thanks, again.

…spike

---

<div class="post-metadata">

**Author:** ![spike666](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/13edae/32.png) [@spike666](https://www.ruby-forum.com/u/spike666)\
**Post date:** [October 16, 2007, 9:03pm UTC](https://www.ruby-forum.com/t/helper-method-for-facade-column/117396/4 "2007-10-16T21:03:33Z")

</div>

Excerpts from Gianmarco P.'s message of Tue Oct 16 19:10:29  
+0100 2007:

> …spike

Hi Spike,

The define\_method creates the method so add more ie:

module MyPlugin  
def self.append\_features(base)  
base.extend ClassMethods  
end

```
  module ClassMethods
    def ipv4_address field
       class_eval do
         define_method field.to_sym do
           # code here to return the address
         end
         define_method "#{field}_before_type_case".to_sym do
           # code here to return the value before type case
         end
       end
    end
  end
```

---

<div class="post-metadata">

**Author:** ![spike666](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/13edae/32.png) [@spike666](https://www.ruby-forum.com/u/spike666)\
**Post date:** [October 16, 2007, 9:58pm UTC](https://www.ruby-forum.com/t/helper-method-for-facade-column/117396/5 "2007-10-16T21:58:31Z")

</div>

I’m having one more issue with this. Having never worked with Rails  
plugins before, I must just be forgetting to do something simple.

I created a new plugin using “script/generate plugin Ipv4Address”

I then edited ‘lib/ipv4\_address.rb’ to contain the following:

=============START============

# Ipv4Address

module Ipv4Address  
def self.append\_features(base)  
base.extend ClassMethods  
end

module ClassMethods

def ipv4\_address field  
class\_eval do  
define\_method field.to\_sym do  
IPAddress.to\_ip read\_attribute(field)  
end

```
define_method "#{field}=".to_sym do |new_ip|
 write_attribute field, IPAddress.to_int(new_ip)
end

define_method "#{field}_before_type_cast".to_sym do
 field ##is this right?
end

```

end  
end

end  
end  
============END=============

and init.rb contains:

=========START=========  
ActiveRecord::Base.class\_eval do  
include Ipv4Address  
end  
=========END===========

When I set up my ActiveRecord class to use:

ipv4\_address :start\_ip

I get the following error:

undefined method `ipv4\_address’ for DhcpBlock:Class

am I missing something?

Thanks!

…spike
