<repo_name>theerincollins/dictionary<file_sep>/lib/definition.rb
class Definition
  @@definitions = []

  attr_reader(:description)

  define_method(:initialize) do |description|
    @description = description
  end

  define_method(:save) do
    @@definitions.push(self)
  end

  define_singleton_method(:clear) do
    @@definitions = []
  end

  define_singleton_method(:all) do
    @@definitions
  end
end
<file_sep>/spec/dictionary_integration_spec.rb
require ('capybara/rspec')
require('./app')

Capybara.app = Sinatra::Application
set(:show_exceptions, false)

describe('add new word path', {:type => :feature}) do
  it('show page for input a new word and then display that word with all other words in the dictionary') do
    visit('/word/new')
    fill_in('word_name', :with => 'Airplane')
    click_button('+ Add')
    expect(page).to have_content('Airplane')
  end
end

describe('add new definition path', {:type => :feature}) do
  it('show page a word that exists, a user can input a definition and it will be added to that word') do
    visit('/word/new')
    fill_in('definition', :with => 'wow')
    click_button('+ Add')
    expect(page).to have_content('wow')
  end
end
<file_sep>/README.md
Dictionary

Description: Using Ruby this method will list the words in a dictionary. A user can select a word to see the definitions and add more definitions to existing words. A user can also add a new word and definition.

The method will alert the user if they enter a word twice.


Author: <NAME> Date: May 1, 2015

Link to Heroku: https://erin-dictionary.herokuapp.com/word/new

Setup Instructions: -- Download all files -- Run bundle install -- Run Ruby app.rb -- Enter localhost:4567 into address bar

The MIT License

Copyright (c) 2015 <NAME> <EMAIL>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
<file_sep>/spec/dictionary_spec.rb
require('rspec')
require('word')
require('definition')

describe Word do

  before do
    Word.clear()
  end

  describe('.all') do
    it('will return array of all words') do
      test_word = Word.new("cheeseburger").save()
      expect(Word.all).to(eq(test_word))
    end
    it('will be empty at first') do
      expect(Word.all).to(eq([]))
    end
  end

  describe('.clear') do
    it('will clear out array of words') do
      test_word = Word.new("cheeseburger").save()
      Word.clear()
      expect(Word.all).to(eq([]))
    end
  end

  describe('#save') do
    it('will save a word to an array of all words') do
      test_word = Word.new("vegan")
      test_word.save()
      expect(Word.all).to(eq([test_word]))
    end
  end

  describe('#id') do
    it('will return id of a word') do
      test_word = Word.new("Snoopy")
      expect(test_word.id()).to(eq(1))
    end
  end

  describe('#name') do
    it('will return the name of a word') do
      test_word = Word.new("Peanut")
      expect(test_word.word_name()).to(eq("Peanut"))
    end
  end

  describe('.find') do
    it("will return a word by using that word's id") do
      test_word1 = Word.new("Hard")
      test_word1.save()
      test_word2 = Word.new("Rock")
      test_word2.save()
      expect(Word.find(test_word2.id())).to(eq(test_word2))
    end
  end

  describe('#add_definition') do
    it("will add a definition to a word's array of definitions") do
      test_definition1 = Definition.new("it barks a lot")
      test_definition2 = Definition.new("it pees a lot")
      test_word = Word.new("dog")
      test_word.save()
      test_word.add_definition(test_definition1)
      test_word.add_definition(test_definition2)
      expect(test_word.get_definitions()).to(eq([test_definition1, test_definition2]))
    end
  end

  describe('.duplicate?') do
    it('will return true if the word entered is alread in the dictionary') do
      test_word1 = Word.new("dog")
      test_word1.save()
      test_word2 = Word.new("dog")
      test_word2.save()
      expect(Word.duplicate?(test_word2.word_name())).to(eq(true))
    end
  end

  describe('.delete_duplicate') do
    it('will delete the last word if it was a duplicate') do
      test_word1 = Word.new("cat")
      test_word1.save()
      test_word2 = Word.new("dog")
      test_word2.save()
      test_word3 = Word.new("dog")
      test_word3.save()
      Word.delete_duplicate(test_word3)
      expect(Word.all).to(eq([test_word1, test_word2]))
    end
  end
end

describe Definition do

  before do
    Definition.clear()
  end

  describe('.clear') do
    it('will clear array of definitions') do
      Definition.new("this and that").save()
      Definition.clear
      expect(Definition.all()).to(eq([]))
    end
  end

  describe('.all') do
    it('will be empty at first') do
      expect(Definition.all()).to(eq([]))
    end
  end

  describe('#save') do
    it('will save a definition to an array of definitions') do
      test_definition = Definition.new("those things over there")
      test_definition.save()
      expect(Definition.all).to(eq([test_definition]))
    end
  end

  describe('#description') do
    it('will return the description of the definition') do
      test_definition = Definition.new("those things over there")
      expect(test_definition.description).to(eq("those things over there"))
    end
  end

end
<file_sep>/lib/word.rb
class Word

  @@words = []

  attr_reader(:word_name, :id)

  define_method(:initialize) do |word_name|
    @word_name = word_name
    @id = @@words.length() + 1
    @word_definitions = []
  end

  define_singleton_method(:clear) do
    @@words = []
  end

  define_singleton_method(:all) do
    @@words
  end

  define_singleton_method(:find) do |id|
    found_word = ''
    @@words.each do |word|
      if word.id() == id
        found_word = word
      end
    end
    found_word
  end

  define_method(:save) do
    @@words.push(self)
  end

  define_method(:add_definition) do |definition|
    @word_definitions.push(definition)
  end

  define_method(:get_definitions) do
    @word_definitions
  end

  define_singleton_method(:duplicate?) do |word_name|
    duplicate = nil
    @@words.each do |word|
      if word.word_name == word_name
        duplicate = true
      end
    end
    duplicate
  end

  define_singleton_method(:delete_duplicate) do |word|
    if Word.duplicate?(word.word_name())
      @@words.pop
    end
    @@words
  end
end
<file_sep>/app.rb
require('./lib/word')
require('./lib/definition')
require('sinatra')
require('sinatra/reloader')
also_reload('lib/**/*.rb')
require('pry')
require('rubygems')

get('/') do
  @words = Word.all
  erb(:index)
end

get('/word/new') do
  erb(:new_word)
end

post('/save_word') do
  @new_word = Word.new(params.fetch("word_name"))
  definition = Definition.new(params.fetch("definition"))
  @new_word.add_definition(definition)
  @definitions = @new_word.get_definitions()
  @duplicate = Word.duplicate?(@new_word.word_name())
  @new_word.save()
  erb(:word)
end

get('/word/:id') do
  @new_word = Word.find(params.fetch('id').to_i())
  @definitions = @new_word.get_definitions()
  erb(:word)
end

post('/add_definition') do
  new_definition = Definition.new(params.fetch("definition"))
  @new_word = Word.find(params.fetch("word_id").to_i())
  @new_word.add_definition(new_definition)
  @definitions = @new_word.get_definitions()
  erb(:word)
end

get('/reset') do
  Word.clear()
  erb(:index)
end
