lib/dsu/models/entry.rb in dsu-0.1.0.alpha.5 vs lib/dsu/models/entry.rb in dsu-1.0.0

- old
+ new

@@ -1,45 +1,57 @@ # frozen_string_literal: true -require 'deco_lite' +require 'active_model' require 'securerandom' require_relative '../support/descriptable' +require_relative '../validators/description_validator' module Dsu module Models - class Entry < DecoLite::Model + # This class represents something someone might want to share at their + # daily standup (DSU). + class Entry + include ActiveModel::Model include Support::Descriptable - ENTRY_UUID_REGEX = /\A[0-9a-f]{8}\z/i + validates_with Validators::DescriptionValidator - validates :uuid, presence: true, format: { - with: ENTRY_UUID_REGEX, - message: 'is the wrong format. ' \ - '0-9, a-f, and 8 characters were expected.' \ - } - validates :description, presence: true, length: { minimum: 2, maximum: 256 } + attr_reader :description - def initialize(description:, uuid: nil) - raise ArgumentError, 'description is nil' if description.nil? + def initialize(description:) raise ArgumentError, 'description is the wrong object type' unless description.is_a?(String) - raise ArgumentError, 'uuid is the wrong object type' unless uuid.is_a?(String) || uuid.nil? - uuid ||= SecureRandom.uuid[0..7] + self.description = description + end - super(hash: { - uuid: uuid, - description: description - }) + class << self + def clean_description(description) + return if description.nil? + + description.strip.gsub(/\s+/, ' ') + end end - def required_fields - %i[uuid description] + def description=(description) + @description = self.class.clean_description description end + def to_h + { description: description } + end + + # Override == and hash so that we can compare Entry objects based + # on description alone. This is useful for comparing entries in + # an array, for example. def ==(other) return false unless other.is_a?(Entry) - uuid == other.uuid && description == other.description + description == other.description + end + alias eql? == + + def hash + description.hash end end end end