Sha256: 59666fd294dd33a40f3bac85016e2958e1bd03acd567db45dc586c63094a3084

Contents?: true

Size: 1.28 KB

Versions: 3

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

module WordSearch
  class Generator < SimpleDelegator
    include ActiveModel::Validations

    validate :can_words_fit?
    validate :valid_plane?
    validate :valid_word_bank?

    def initialize(file, x, y, z = nil)
      plane = Plane.make_from_coordinates(x, y, z)
      obj =
        if z.present?
          ThreeDimensional::Generator.new(plane, WordBank.new(file))
        else
          TwoDimensional::Generator.new(plane, WordBank.new(file))
        end

      super obj
    end

    def perform
      super

      if valid?
        plane.add_letters
        plane
      else
        false
      end
    end

    def object
      __getobj__
    end

    private

    def can_words_fit?
      errors.add(:base, words_too_long) if plane.max < word_bank.longest_length
    end

    def words_too_long
      "#{word_bank.longest_words.join(' and ')} "\
      "#{'is'.pluralize(word_bank.longest_words.count)} "\
      "too long for the word search"
    end

    def valid_plane?
      return if plane.valid?

      plane.errors.full_messages.each do |msg|
        errors.add(:base, msg)
      end
    end

    def valid_word_bank?
      return if word_bank.valid?

      word_bank.errors.full_messages.each do |msg|
        errors.add(:base, msg)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

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