Sha256: 34ce8e2f3656a6badbd5bfaeda67613304a08c89281b5ed9dd8d0df97483605b

Contents?: true

Size: 841 Bytes

Versions: 3

Compression:

Stored size: 841 Bytes

Contents

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-0.5.1 lib/word_search/word_bank.rb
word_search-0.5.0 lib/word_search/word_bank.rb
word_search-0.1.0 lib/word_search/word_bank.rb