[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Creating a ruby class with mass assignment

Ruby Nooby

1/30/2009 12:08:00 PM

Hi,
How do I create a ruby class that has mass assignment functionality? I
want it to work in a similar way to the way in which ActiveRecord does,
ie.
MyClass.new
=> #<MyClass:>
MyClass.new(:name => "Cool class")
=> #<MyClass: @name="s">
MyClass.new(:name => "Cool class", :other_attribute => "some other")
=> #<MyClass: @name="s", @other_attribute="some other">

I have found the following code snippit which works for the mass
assignment part but doesn't work if I initialize the class without any
arguments:

class MyClass
attr_accessor :name
attr_accessor :other_attribute

def initialize(args)
args.keys.each { |name| instance_variable_set "@" + name.to_s,
args[name] }
end
end

MyClass.new
ArgumentError: wrong number of arguments (0 for 1)
from (irb):19:in `initialize'
from (irb):19:in `new'
from (irb):19

What is the best way of achieving mass assignment whilst still allowing
to initialize without parameters?
--
Posted via http://www.ruby-....

2 Answers

Rupert Voelcker

1/30/2009 12:42:00 PM

0

> What is the best way of achieving mass assignment whilst still allowing
> to initialize without parameters?

def initialize(args = {})
args.keys.each { ....... }
end

Robert Klemme

1/30/2009 1:34:00 PM

0

2009/1/30 Ruby Nooby <uslxycdabssmy@mailinator.com>:
> Hi,
> How do I create a ruby class that has mass assignment functionality? I
> want it to work in a similar way to the way in which ActiveRecord does,
> ie.
> MyClass.new
> => #<MyClass:>
> MyClass.new(:name => "Cool class")
> => #<MyClass: @name="s">
> MyClass.new(:name => "Cool class", :other_attribute => "some other")
> => #<MyClass: @name="s", @other_attribute="some other">
>
> I have found the following code snippit which works for the mass
> assignment part but doesn't work if I initialize the class without any
> arguments:
>
> class MyClass
> attr_accessor :name
> attr_accessor :other_attribute
>
> def initialize(args)
> args.keys.each { |name| instance_variable_set "@" + name.to_s,
> args[name] }
> end
> end
>
> MyClass.new
> ArgumentError: wrong number of arguments (0 for 1)
> from (irb):19:in `initialize'
> from (irb):19:in `new'
> from (irb):19
>
> What is the best way of achieving mass assignment whilst still allowing
> to initialize without parameters?

You can make your life considerably easier by using Struct:

irb(main):001:0> Test = Struct.new :foo, :bar, :baz do
irb(main):002:1* def self.create(h = {})
irb(main):003:2> new(*members.map {|m| h[m.to_sym]})
irb(main):004:2> end
irb(main):005:1> end
=> Test
irb(main):006:0> Test.create(:foo => 1)
=> #<struct Test foo=1, bar=nil, baz=nil>
irb(main):007:0> Test.create(:bar => 3, :foo => 123)
=> #<struct Test foo=123, bar=3, baz=nil>

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end