Sha256: b67bb9ea871573aa947553fa7cf3e416cca1601886d201de9a6956e07bd2c37b

Contents?: true

Size: 811 Bytes

Versions: 2

Compression:

Stored size: 811 Bytes

Contents

# Classe astratta
# Oggetto che si valida, contiene all'interno la lista
# di messaggi che spiegano perche' non e' valido
#
# la classe derivata deve definire il metodo validate
#
class ObjectWithValidation

  def initialize
    @valid = true
    # @explain = []
    # puts "base initialize"
  end

  def valid?
    @valid
  end

  def invalid?
    not valid?
  end

  def force_validate
    validate
    @valid
  end

  def explain
    raise "Valid object" if valid?
    @explain
  end

  protected

  def add_error( msg )
    @valid = false
    @explain ||= []

    if msg.class == String
      @explain << msg
    end

    if msg.class == Array
      msg.each { |ex|
        add_error("Invalid image_file nested message: '#{ex}'")
      }
    end
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gf-ralbum-0.0.2 lib/ralbum-common/object_with_validation.rb
gf-ralbum-0.0.5 lib/ralbum/object_with_validation.rb