How can I avoid this boring code?

S

Sam Kong

Hi!

I'm creating a class.
I wonder if there's a better way.

require 'dbi'

class Person
attr_accessor :person_id,
:last_name,
:first_name,
:home_phone,
:work_phone,
:address,
:city,
:state,
:zip

def initialize(row=nil)
if row.class == DBI::Row then
@person_id = row.by_field("person_id")
@last_name = row.by_field("last_name")
@first_name = row.by_field("first_name")
@home_phone = row.by_field("home_phone")
@work_phone = row.by_field("work_phone")
@address = row.by_field("address")
@city = row.by_field("city")
@state = row.by_field("state")
@zip = row.by_field("zip")
end
end
end

The code in initialize method is very boring.
How could I make it simpler?

Thanks in advance.

Sam
 
M

Marcel Molina Jr.

I'm creating a class.
I wonder if there's a better way.

require 'dbi'

class Person
attr_accessor :person_id,
:last_name,
:first_name,
:home_phone,
:work_phone,
:address,
:city,
:state,
:zip

def initialize(row=nil)
if row.class == DBI::Row then
@person_id = row.by_field("person_id")
@last_name = row.by_field("last_name")
@first_name = row.by_field("first_name")
@home_phone = row.by_field("home_phone")
@work_phone = row.by_field("work_phone")
@address = row.by_field("address")
@city = row.by_field("city")
@state = row.by_field("state")
@zip = row.by_field("zip")
end
end
end

The code in initialize method is very boring.
How could I make it simpler?

You could use Active Record.

class Person < ActiveRecord::Base
end

etc

Though ActiveRecord is known for being a component of Rails, it can be used
entirely discreetly from Rails.

e.g.

require 'active_record'

ActiveRecord::Base.establish_connection:)adapter => 'sqlite', :database => ':memory:')

class Person < Active::Record::Base
end

All set.

marcel
 
N

nobuyoshi nakada

Hi,

At Wed, 2 Nov 2005 11:17:08 +0900,
Sam Kong wrote in [ruby-talk:163688]:
require 'dbi'
Person = Struct.new:)person_id, :last_name, :first_name,
:home_phone, :work_phone,
:address, :city, :state, :zip)

class Person
def self.load_fields(row)
members.collect {|n| row.by_field(n)}
end

# may be preferable.
def self.load(row)
new(*load_fields(row))
end

def initialize(*args)
if args.size == 1 and DBI::Row === (row = args[0])
args = self.class.load_fields(row)
end
super(*args)
end
end

I'm uncertain if DBI may have more convenient method.
 
A

Ara.T.Howard

Hi!

I'm creating a class.
I wonder if there's a better way.

require 'dbi'

class Person
attr_accessor :person_id,
:last_name,
:first_name,
:home_phone,
:work_phone,
:address,
:city,
:state,
:zip

def initialize(row=nil)
if row.class == DBI::Row then
@person_id = row.by_field("person_id")
@last_name = row.by_field("last_name")
@first_name = row.by_field("first_name")
@home_phone = row.by_field("home_phone")
@work_phone = row.by_field("work_phone")
@address = row.by_field("address")
@city = row.by_field("city")
@state = row.by_field("state")
@zip = row.by_field("zip")
end
end
end

The code in initialize method is very boring.
How could I make it simpler?

class Person
FIELDS = %w(
person_id last_name first_name home_phone work_phone address city state zip
)
def initialize r = nil
FIELDS.each{|f| send "#{ f }=", r.by_field(f)} if row.class == DBI::Row
end
end

if the Person class can dynamically set FIELDS by inspecting a table - so much
the better.

hth.

-a
--
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| anything that contradicts experience and logic should be abandoned.
| -- h.h. the 14th dalai lama
===============================================================================
 
G

gabriele renzi

Sam Kong ha scritto:
Hi!

I'm creating a class.
I wonder if there's a better way.

require 'dbi'

class Person
attr_accessor :person_id,
:last_name,
:first_name,
:home_phone,
:work_phone,
:address,
:city,
:state,
:zip

def initialize(row=nil)
if row.class == DBI::Row then
@person_id = row.by_field("person_id")
@last_name = row.by_field("last_name")
@first_name = row.by_field("first_name")
@home_phone = row.by_field("home_phone")
@work_phone = row.by_field("work_phone")
@address = row.by_field("address")
@city = row.by_field("city")
@state = row.by_field("state")
@zip = row.by_field("zip")
end
end
end

The code in initialize method is very boring.
How could I make it simpler?

Thanks in advance.

Sam

I think I'd do the initialize method like this:
def initialize(row=nil)
if row.is_A? DBI::Row
row.each_with_name do |value,name|
send name.to_s+"=", value
end
end
end

And I'd make Person a Struct to get getters/setters for free.
 
T

Trans

Doesn't a DBI::Row respond to #to_h? If not teach it and be sure to
add:

class Hash
def to_h
self
end
end

You can define an attr method


module AttrDBI
def attr_dbi( *args )
@dbi_fields |= args
attr_accessor( *args )
end

def dbi_fields
@dbi_fields
end
end

class Person
extend AttrDBI

attr_dbi :person_id,
:last_name
...

def initialize(row)
row = row.to_h
self.class.dbi_fields.each {|f| send("#{f}=", row[f] }
end

T.
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top