Sha256: 5d9210c1d47a597fe52276913c39d181f0cd7ba5d6a4dc4da3e0c15afca51d04

Contents?: true

Size: 872 Bytes

Versions: 3

Compression:

Stored size: 872 Bytes

Contents

# frozen_string_literal: true

module WordSearch
  class WordBank < Array
    include ActiveModel::Validations

    validate :word_bank

    def initialize(file)
      return invalid_file unless valid_file?(file)
      words = []

      CSV.foreach(file) do |row|
        row.each do |word|
          words << word.strip.downcase if word.strip.length > 1
        end
      end

      super words.uniq
    end

    def longest_length
      @longest ||= collect(&:length).max.to_i
    end

    def longest_words
      select do |word|
        word.length == longest_length
      end
    end

    private

    def word_bank
      errors.add(:base, "Word bank cannot be empty") if blank?
    end

    def valid_file?(file)
      File.file?(file) && File.extname(file) == ".csv"
    end

    def invalid_file
      errors.add(:file, "is invalid")
      false
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
word_search-1.3.0 lib/word_search/word_bank.rb
word_search-1.2.1 lib/word_search/word_bank.rb
word_search-1.2.0 lib/word_search/word_bank.rb