Breakdown of this Ruby code?

Posted by randombits on Stack Overflow See other posts from Stack Overflow or by randombits
Published on 2010-04-23T22:56:38Z Indexed on 2010/04/23 23:03 UTC
Read the original article Hit count: 254

Filed under:
|

Would anyone be kind enough to dissect the merge! method? Its usage of conditions and variable assignment looks rather terse, and I'm having a difficult time following it. Would love to hear a Ruby-savvy developer break this apart.

module ActiveRecord
  class Errors

    def merge!(errors, options={})
      fields_to_merge = if only=options[:only]
        only
      elsif except=options[:except]
        except = [except] unless except.is_a?(Array)
        except.map!(&:to_sym)
        errors.entries.map(&:first).select do |field|
          !except.include?(field.to_sym)
        end
      else
        errors.entries.map(&:first)
      end
      fields_to_merge = [fields_to_merge] unless fields_to_merge.is_a?(Array)
      fields_to_merge.map!(&:to_sym)

      errors.entries.each do |field, msg|
        add field, msg if fields_to_merge.include?(field.to_sym)
      end
    end
  end
end

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby