Sha256: 432de139d1ed33686f2bd7c37fa47999d78f2f7c8a0914d8c92f50d074c6054c

Contents?: true

Size: 1.34 KB

Versions: 9

Compression:

Stored size: 1.34 KB

Contents

module PivotalTracker

  class Errors
    include Enumerable
    attr_reader :errors

    alias :messages :errors

    def initialize
      @errors = []
    end

    def each
      @errors.each do |error|
        yield error
      end
    end

    def empty?
      @errors.empty?
    end

    def add_from_xml(xml)
      Nokogiri::XML(xml).xpath("/errors/error").each do |error|
        @errors << error.text
      end
    end
  end

  module Validation

    def self.included(klass)
      klass.class_eval do
        if klass.instance_methods.include?(:create)
          alias_method :create_without_validations, :create
          alias_method :create, :create_with_validations
        end

        if klass.instance_methods.include?(:update)
          alias_method :update_without_validations, :update
          alias_method :update, :update_with_validations
        end
      end
    end

    def create_with_validations
      begin
        create_without_validations
      rescue RestClient::UnprocessableEntity => e
        errors.add_from_xml e.response
        self
      end
    end

    def update_with_validations(attrs={})
      begin
        update_without_validations attrs
      rescue RestClient::UnprocessableEntity => e
        errors.add_from_xml e.response
        self
      end
    end

    def errors
      @errors ||= Errors.new
    end
  end
end

Version data entries

9 entries across 9 versions & 3 rubygems

Version Path
pivotal-tracker-0.5.1 lib/pivotal-tracker/validation.rb
pivotal-tracker-0.5.0 lib/pivotal-tracker/validation.rb
pivotal-tracker-0.4.1 lib/pivotal-tracker/validation.rb
pivotal-tracker-0.4.0 lib/pivotal-tracker/validation.rb
tsenart-pivotal-tracker-0.5.0 lib/pivotal-tracker/validation.rb
tsenart-pivotal-tracker-0.4.0 lib/pivotal-tracker/validation.rb
topprospect-pivotal-tracker-0.1.1 lib/pivotal-tracker/validation.rb
topprospect-pivotal-tracker-0.0.1 lib/pivotal-tracker/validation.rb
pivotal-tracker-0.3.1 lib/pivotal-tracker/validation.rb