Module: Pressletter::Core

Included in:
Shell::API, Shell::CLI
Defined in:
lib/pressletter.rb,
lib/pressletter/core/solve.rb,
lib/pressletter/core/find_words.rb,
lib/pressletter/core/sort_words.rb,
lib/pressletter/core/print_words.rb,
lib/pressletter/core/create_letters.rb,
lib/pressletter/core/load_dictionary.rb

Instance Method Summary collapse

Instance Method Details

#create_letters(input) ⇒ Object



2
3
4
5
6
7
8
9
10
# File 'lib/pressletter/core/create_letters.rb', line 2

def create_letters(input)
  Pressletter::Values::Letters.new(
    ensure_alphabetical(
      input.chomp.split('').
        map { |c| c.upcase }.
          reject { |c| c == ' ' }.compact.sort
    )
  )
end

#find_words(dictionary, letters) ⇒ Object



2
3
4
5
6
7
8
# File 'lib/pressletter/core/find_words.rb', line 2

def find_words(dictionary, letters)
  Pressletter::Values::Words.new(
    dictionary.as_array.map do |word|
      word if letters_can_buy?(letters, word)
    end.compact
  )
end

#load_dictionary(location) ⇒ Object



2
3
4
# File 'lib/pressletter/core/load_dictionary.rb', line 2

def load_dictionary(location)
  Dictionary.new(File.read(location).split("\n"))
end


2
3
4
# File 'lib/pressletter/core/print_words.rb', line 2

def print_words(words)
  words.as_array.join("\n")
end

#solve(config, letters) ⇒ Object



2
3
4
# File 'lib/pressletter/core/solve.rb', line 2

def solve(config, letters)
  sort_words(find_words(load_dictionary(config.dictionary_location), letters))
end

#sort_words(words) ⇒ Object



2
3
4
# File 'lib/pressletter/core/sort_words.rb', line 2

def sort_words(words)
  Pressletter::Values::Words.new(words.as_array.sort(&method(:size_then_alphabet)))
end