Sha256: 596d40f1cafc8d65d865d3ab8eb0b0a4d80f8ec9b6af1b17d99b1f4203d0bc0a

Contents?: true

Size: 1.74 KB

Versions: 21

Compression:

Stored size: 1.74 KB

Contents

module Gecko
  module Helpers
    # Provides record validation
    module ValidationHelper
      # Returns whether a record is considered valid or not
      #
      # @return [Boolean]
      #
      # @api public
      def valid?
        errors.empty?
      end

      # Returns the validation errors object for the record
      #
      # @return [Gecko::Errors]
      #
      # @api public
      def errors
        @errors ||= Gecko::Errors.new(self)
      end
    end
  end

  class Errors
    # The hash of errors for this record
    #
    # @return [Hash]
    #
    # @api public
    attr_reader :messages

    # Set up the error object
    #
    # @api private
    def initialize(base)
      @base     = base
      @messages = {}
    end

    # Fetch the errors for a specific attribute
    #
    # @example
    #   product.errors[:name]
    #     #=> ["can't be blank"]
    #
    # @params [Symbol] :attribute
    #
    # @return [Array]
    #
    # @api public
    def [](attribute)
      messages[attribute.to_sym]
    end

    # Clear the validation errors
    #
    # @example
    #   product.errors.clear
    #
    # @return [undefined]
    #
    # @api public
    def clear
      @messages.clear
    end

    # Whether there are any errors
    #
    # @return [Boolean]
    #
    # @api public
    def empty?
      messages.all? { |k, v| v && v.empty? && !v.is_a?(String) }
    end

    # Parse JSON errors response into the error object
    #
    # @params [#to_hash] :error_hash hash of errors where key is an attribute
    #   name and value is an array of error strings
    #
    # @return [undefined]
    #
    # @api private
    def from_response(error_hash)
      error_hash.each do |attr, errors|
        @messages[attr.to_sym] = errors
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
gecko-ruby-0.9.1 lib/gecko/helpers/validation_helper.rb
gecko-ruby-0.9.0 lib/gecko/helpers/validation_helper.rb
gecko-ruby-0.8.0 lib/gecko/helpers/validation_helper.rb
gecko-ruby-0.2.6 lib/gecko/helpers/validation_helper.rb
gecko-ruby-0.7.1 lib/gecko/helpers/validation_helper.rb
gecko-ruby-0.7.0 lib/gecko/helpers/validation_helper.rb
gecko-ruby-0.6.0 lib/gecko/helpers/validation_helper.rb
gecko-ruby-0.5.0 lib/gecko/helpers/validation_helper.rb
gecko-ruby-0.2.5 lib/gecko/helpers/validation_helper.rb
gecko-ruby-0.2.4 lib/gecko/helpers/validation_helper.rb
gecko-ruby-0.2.3 lib/gecko/helpers/validation_helper.rb
gecko-ruby-0.2.2 lib/gecko/helpers/validation_helper.rb
gecko-ruby-0.2.0 lib/gecko/helpers/validation_helper.rb
gecko-ruby-0.1.0 lib/gecko/helpers/validation_helper.rb
gecko-ruby-0.0.10 lib/gecko/helpers/validation_helper.rb
gecko-ruby-0.0.9 lib/gecko/helpers/validation_helper.rb
gecko-ruby-0.0.8 lib/gecko/helpers/validation_helper.rb
gecko-ruby-0.0.7 lib/gecko/helpers/validation_helper.rb
gecko-ruby-0.0.6 lib/gecko/helpers/validation_helper.rb
gecko-ruby-0.0.5 lib/gecko/helpers/validation_helper.rb