[code]
module BeanUtils
RESERVE_INSTANCE_KEY = %w{id attributes_cache new_record}
#copy properties
def cp(dest)
dest = dest.new if dest.is_a?(Class)
copy = lambda{|h| h.each{|key, value| dest.send("#{key}=", value) if dest.respond_to?("#{key}=") and !value.blank?}}
returning dest do
self.instance_values.each do |key, value|
unless RESERVE_INSTANCE_KEY.include? key
if value.is_a? Hash
#copy attributes
copy.call(value)
else
#copy other instance values
copy.call({key=>value})
end
end
end
end
end
end
[/code]
Usage:
[code]
topic = Topic.new params[:topic]
post = topic.cp(Post)
[/code]
Any other useful methods?
http://commons.apache.org/beanutils/apidocs/index.html
pls do it yourself:)
module BeanUtils
RESERVE_INSTANCE_KEY = %w{id attributes_cache new_record}
#copy properties
def cp(dest)
dest = dest.new if dest.is_a?(Class)
copy = lambda{|h| h.each{|key, value| dest.send("#{key}=", value) if dest.respond_to?("#{key}=") and !value.blank?}}
returning dest do
self.instance_values.each do |key, value|
unless RESERVE_INSTANCE_KEY.include? key
if value.is_a? Hash
#copy attributes
copy.call(value)
else
#copy other instance values
copy.call({key=>value})
end
end
end
end
end
end
[/code]
Usage:
[code]
topic = Topic.new params[:topic]
post = topic.cp(Post)
[/code]
Any other useful methods?
http://commons.apache.org/beanutils/apidocs/index.html
pls do it yourself:)