helper method for facade column

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

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

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

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