[issue #565] refactoring, proposed by @alexander-machehin

This commit is contained in:
George Vinogradov 2012-07-30 23:58:56 +04:00
parent 6e598ad5f9
commit ef25bb502a
1 changed files with 13 additions and 16 deletions

View File

@ -58,34 +58,31 @@ class Feedback
end end
def to_s def to_s
str = "#<#{self.class} " str = %w{ name email subject message }.map do |e|
str += %w{ name email subject message }.inject('') do |s, e| "#{e}: #{ send(e).inspect }"
s << "#{e}: #{ send(e).inspect }, "; s end.join(', ')
end return "#<#{self.class} #{str}>"
str[-2] = '>'
str[0..-1]
end end
class << self class << self
def create(attributes = nil, options = {}, &block) def create(attributes = nil, options = {}, &block)
if attributes.is_a?(Array) do_create(attributes, options, false, &block)
attributes.collect { |attr| create!(attr, options, &block) }
else
object = new(attributes, options)
yield(object) if block_given?
object.perform_send
object
end
end end
def create!(attributes = nil, options = {}, &block) def create!(attributes = nil, options = {}, &block)
do_create(attributes, options, true, &block)
end
protected
def do_create(attributes = nil, options = {}, bang = false, &block)
if attributes.is_a?(Array) if attributes.is_a?(Array)
attributes.collect { |attr| create!(attr, options, &block) } attributes.collect { |attr| do_create(attr, options, bang, &block) }
else else
object = new(attributes, options) object = new(attributes, options)
yield(object) if block_given? yield(object) if block_given?
object.perform_send! bang ? object.perform_send! : object.perform_send
object object
end end
end end