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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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_addressip_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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. > > > > ...spikeHi 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---