memory_dictionary 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.env.sample +3 -0
- data/.gitignore +11 -0
- data/.rspec +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +8 -0
- data/.yardopts +3 -0
- data/Gemfile +23 -0
- data/LICENSE +5 -0
- data/Rakefile +26 -0
- data/Readme.md +51 -0
- data/lib/memory_dictionary.rb +37 -0
- data/lib/memory_dictionary/dictionary.rb +23 -0
- data/lib/memory_dictionary/errors.rb +6 -0
- data/lib/memory_dictionary/errors/blacklisted_exception.rb +2 -0
- data/lib/memory_dictionary/errors/dictionary_not_found_exception.rb +2 -0
- data/lib/memory_dictionary/errors/word_not_found_exception.rb +2 -0
- data/lib/memory_dictionary/translator.rb +31 -0
- data/lib/memory_dictionary/version.rb +10 -0
- data/lib/memory_dictionary/word.rb +12 -0
- data/memory_dictionary.gemspec +40 -0
- data/spec/factories/dictionary.rb +9 -0
- data/spec/factories/word.rb +9 -0
- data/spec/memory_dictionary/dictionary_spec.rb +36 -0
- data/spec/memory_dictionary/translator_spec.rb +80 -0
- data/spec/memory_dictionary/word_spec.rb +11 -0
- data/spec/memory_dictionary_spec.rb +8 -0
- data/spec/spec_helper.rb +52 -0
- data/spec/support/mongoid.yml +23 -0
- data/tmp/.keep +0 -0
- metadata +222 -0
    
        checksums.yaml
    ADDED
    
    | @@ -0,0 +1,7 @@ | |
| 1 | 
            +
            ---
         | 
| 2 | 
            +
            SHA1:
         | 
| 3 | 
            +
              metadata.gz: 3dcd05232a97846c235a328f82a0bb93f10ccffb
         | 
| 4 | 
            +
              data.tar.gz: 75165a219e877e80fb46961785d19a6f5070b925
         | 
| 5 | 
            +
            SHA512:
         | 
| 6 | 
            +
              metadata.gz: e7d51e55d9112e91e4b96dd7386000f79d0591aa360de1e27f8f1080d110719e9632613dbdc8c67e877175b4bc3790ecb2300d5929946d1a6a850574ad85ef4d
         | 
| 7 | 
            +
              data.tar.gz: 1135c862bab53be2c573acc8918297676c1617b46b0e64e59dec3e60a60800737601cb1861a7bfb107023413b45900f113f259d68d50546ec520b99931686fc5
         | 
    
        data/.env.sample
    ADDED
    
    
    
        data/.gitignore
    ADDED
    
    
    
        data/.rspec
    ADDED
    
    | @@ -0,0 +1 @@ | |
| 1 | 
            +
            --color --format documentation
         | 
    
        data/.ruby-version
    ADDED
    
    | @@ -0,0 +1 @@ | |
| 1 | 
            +
            2.1.2
         | 
    
        data/.travis.yml
    ADDED
    
    
    
        data/.yardopts
    ADDED
    
    
    
        data/Gemfile
    ADDED
    
    | @@ -0,0 +1,23 @@ | |
| 1 | 
            +
            source 'https://rubygems.org'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            gem 'mongoid', '~> 5.1'
         | 
| 4 | 
            +
            gem 'rubysl', platform: :rbx
         | 
| 5 | 
            +
            gem 'yajl-ruby', :platforms=>[:rbx,:ruby]
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            gem 'dotenv', :groups => [:development, :test]
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            group :development do
         | 
| 10 | 
            +
              gem 'rake'
         | 
| 11 | 
            +
              gem 'pry'
         | 
| 12 | 
            +
              gem 'pry-nav'
         | 
| 13 | 
            +
              gem 'yard'
         | 
| 14 | 
            +
            end
         | 
| 15 | 
            +
             | 
| 16 | 
            +
            group :test do
         | 
| 17 | 
            +
              gem 'rspec'
         | 
| 18 | 
            +
              gem 'rspec-mocks'
         | 
| 19 | 
            +
              gem 'rspec-expectations'
         | 
| 20 | 
            +
              gem 'database_cleaner'
         | 
| 21 | 
            +
              gem 'factory_girl', github: 'thoughtbot/factory_girl'
         | 
| 22 | 
            +
              gem 'codeclimate-test-reporter', require: nil
         | 
| 23 | 
            +
            end
         | 
    
        data/LICENSE
    ADDED
    
    
    
        data/Rakefile
    ADDED
    
    | @@ -0,0 +1,26 @@ | |
| 1 | 
            +
            require "bundler"
         | 
| 2 | 
            +
            Bundler::GemHelper.install_tasks
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            require "rspec/core/rake_task"
         | 
| 5 | 
            +
            RSpec::Core::RakeTask.new
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            task :default => :travis
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            task :environment do
         | 
| 10 | 
            +
              require './spec/spec_helper'
         | 
| 11 | 
            +
            end
         | 
| 12 | 
            +
             | 
| 13 | 
            +
            desc 'Opens a Pry/IRB session with the environment loaded'
         | 
| 14 | 
            +
            task :console => :environment do
         | 
| 15 | 
            +
              #gotcha!
         | 
| 16 | 
            +
              Mongoid.load!("spec/support/mongoid.yml", 'development')
         | 
| 17 | 
            +
              binding.pry
         | 
| 18 | 
            +
            end
         | 
| 19 | 
            +
             | 
| 20 | 
            +
            task :travis do
         | 
| 21 | 
            +
              ["rake spec", "rake build"].each do |cmd|
         | 
| 22 | 
            +
                puts "Starting to run #{cmd}..."
         | 
| 23 | 
            +
                system("export DISPLAY=:99.0 && bundle exec #{cmd}")
         | 
| 24 | 
            +
                raise "#{cmd} failed!" unless $?.exitstatus == 0
         | 
| 25 | 
            +
              end
         | 
| 26 | 
            +
            end
         | 
    
        data/Readme.md
    ADDED
    
    | @@ -0,0 +1,51 @@ | |
| 1 | 
            +
            [](https://codeclimate.com/github/lucasmartins/memory-dictionary) [](https://codeclimate.com/github/lucasmartins/memory-dictionary) [](https://travis-ci.org/lucasmartins/memory-dictionary)
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            Memory Dictionary
         | 
| 4 | 
            +
            =================
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            **IN PROGRESS**
         | 
| 7 | 
            +
             | 
| 8 | 
            +
            Install
         | 
| 9 | 
            +
            =======
         | 
| 10 | 
            +
             | 
| 11 | 
            +
            You can:
         | 
| 12 | 
            +
            ```
         | 
| 13 | 
            +
              $ gem install memory_dictionary
         | 
| 14 | 
            +
            ```
         | 
| 15 | 
            +
             | 
| 16 | 
            +
            Or just add it to your Gemfile
         | 
| 17 | 
            +
            ```ruby
         | 
| 18 | 
            +
              gem 'memory_dictionary', '~> 1.0'
         | 
| 19 | 
            +
            ```
         | 
| 20 | 
            +
             | 
| 21 | 
            +
            Use
         | 
| 22 | 
            +
            ===
         | 
| 23 | 
            +
             | 
| 24 | 
            +
            Check the [specs](https://github.com/ContaBoa/memory_dictionary/tree/master/spec)
         | 
| 25 | 
            +
             | 
| 26 | 
            +
            Docs
         | 
| 27 | 
            +
            ====
         | 
| 28 | 
            +
            You should check the [factories](https://github.com/ContaBoa/memory_dictionary/tree/master/spec/factories) to learn what you need to build your objects, and the [tests](https://github.com/ContaBoa/memory_dictionary/tree/master/spec/MemoryDictionary) to learn how to use them. But hey, we have docs [right here](http://rdoc.info/github/ContaBoa/memory_dictionary/master/frames).
         | 
| 29 | 
            +
             | 
| 30 | 
            +
            Roadmap
         | 
| 31 | 
            +
            =======
         | 
| 32 | 
            +
             | 
| 33 | 
            +
            - Binary search;
         | 
| 34 | 
            +
            - Redis backend;
         | 
| 35 | 
            +
            - Postgres backend;
         | 
| 36 | 
            +
            - HTTP (webservice) interface - so you don't need to be coding Ruby to use it!;
         | 
| 37 | 
            +
             | 
| 38 | 
            +
            Contribute
         | 
| 39 | 
            +
            ==========
         | 
| 40 | 
            +
             | 
| 41 | 
            +
            Just fork [MemoryDictionary](https://github.com/ContaBoa/memory_dictionary), add your feature+spec, and make a pull request. **DO NOT** mess up with the version file though.
         | 
| 42 | 
            +
             | 
| 43 | 
            +
            Support
         | 
| 44 | 
            +
            =======
         | 
| 45 | 
            +
             | 
| 46 | 
            +
            This is an opensource project so don't expect premium support, but don't be shy, post any troubles you're having in the [Issues](https://github.com/ContaBoa/memory_dictionary/issues) page and we'll do what we can to help.
         | 
| 47 | 
            +
             | 
| 48 | 
            +
            License
         | 
| 49 | 
            +
            =======
         | 
| 50 | 
            +
             | 
| 51 | 
            +
            Please see [LICENSE](https://github.com/lucasmartins/memory_dictionary/blob/master/LICENSE) for licensing details.
         | 
| @@ -0,0 +1,37 @@ | |
| 1 | 
            +
            if File.exists?('.env')
         | 
| 2 | 
            +
              require 'dotenv'
         | 
| 3 | 
            +
              Dotenv.load
         | 
| 4 | 
            +
            end
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            require 'yaml'
         | 
| 7 | 
            +
            require 'bundler'
         | 
| 8 | 
            +
            require 'json'
         | 
| 9 | 
            +
            require 'mongoid'
         | 
| 10 | 
            +
            require 'yajl-ruby' if RUBY_PLATFORM=='ruby'
         | 
| 11 | 
            +
             | 
| 12 | 
            +
            Encoding.default_internal = "utf-8"
         | 
| 13 | 
            +
            Encoding.default_external = "utf-8"
         | 
| 14 | 
            +
             | 
| 15 | 
            +
            module MemoryDictionary
         | 
| 16 | 
            +
              require_relative 'memory_dictionary/errors'
         | 
| 17 | 
            +
              require_relative 'memory_dictionary/word'
         | 
| 18 | 
            +
              require_relative 'memory_dictionary/dictionary'
         | 
| 19 | 
            +
              require_relative 'memory_dictionary/translator'
         | 
| 20 | 
            +
              require_relative 'memory_dictionary/version'
         | 
| 21 | 
            +
             | 
| 22 | 
            +
              # Returns the lib logger object
         | 
| 23 | 
            +
              def self.logger
         | 
| 24 | 
            +
                @logger || initialize_logger
         | 
| 25 | 
            +
              end
         | 
| 26 | 
            +
             | 
| 27 | 
            +
              # Initializes logger with MemoryDictionary setup
         | 
| 28 | 
            +
              def self.initialize_logger(log_target = STDOUT)
         | 
| 29 | 
            +
                oldlogger = @logger
         | 
| 30 | 
            +
                @logger = Logger.new(log_target)
         | 
| 31 | 
            +
                @logger.level = Logger::INFO
         | 
| 32 | 
            +
                @logger.progname = 'memory_dictionary'
         | 
| 33 | 
            +
                oldlogger.close if oldlogger && !$TESTING # don't want to close testing's STDOUT logging
         | 
| 34 | 
            +
                @logger
         | 
| 35 | 
            +
              end
         | 
| 36 | 
            +
             | 
| 37 | 
            +
            end
         | 
| @@ -0,0 +1,23 @@ | |
| 1 | 
            +
            class MemoryDictionary::Dictionary
         | 
| 2 | 
            +
              include Mongoid::Document
         | 
| 3 | 
            +
              field :name, type: String
         | 
| 4 | 
            +
             | 
| 5 | 
            +
              embeds_many :words, class_name: 'MemoryDictionary::Word'
         | 
| 6 | 
            +
             | 
| 7 | 
            +
              validates :name, uniqueness: true
         | 
| 8 | 
            +
              index({ name: 1 }, { unique: true, name: 'dictionary_names_index' })
         | 
| 9 | 
            +
              #index 'words.name' => 1
         | 
| 10 | 
            +
             | 
| 11 | 
            +
              def translations
         | 
| 12 | 
            +
                words.map(&:translation).uniq
         | 
| 13 | 
            +
              end
         | 
| 14 | 
            +
             | 
| 15 | 
            +
              def words_by_translation(translation)
         | 
| 16 | 
            +
                words.where(translation: translation)
         | 
| 17 | 
            +
              end
         | 
| 18 | 
            +
             | 
| 19 | 
            +
              def append_word(name, translation)
         | 
| 20 | 
            +
                words << MemoryDictionary::Word.new(name: name, translation: translation)
         | 
| 21 | 
            +
              end
         | 
| 22 | 
            +
             | 
| 23 | 
            +
            end
         | 
| @@ -0,0 +1,31 @@ | |
| 1 | 
            +
            class MemoryDictionary::Translator
         | 
| 2 | 
            +
              def initialize(dictionary_name)
         | 
| 3 | 
            +
                begin
         | 
| 4 | 
            +
                  @dictionary = MemoryDictionary::Dictionary.find_by(name: dictionary_name)  
         | 
| 5 | 
            +
                rescue Mongoid::Errors::DocumentNotFound => e
         | 
| 6 | 
            +
                  MemoryDictionary.logger.debug e
         | 
| 7 | 
            +
                  raise MemoryDictionary::Errors::DictionaryNotFoundException
         | 
| 8 | 
            +
                end
         | 
| 9 | 
            +
              end
         | 
| 10 | 
            +
             | 
| 11 | 
            +
              def translate(word)
         | 
| 12 | 
            +
                begin
         | 
| 13 | 
            +
                  word = @dictionary.words.find_by(name: word)
         | 
| 14 | 
            +
                  word.translation
         | 
| 15 | 
            +
                rescue Mongoid::Errors::DocumentNotFound => e
         | 
| 16 | 
            +
                  MemoryDictionary.logger.debug e
         | 
| 17 | 
            +
                  raise MemoryDictionary::Errors::WordNotFoundException
         | 
| 18 | 
            +
                end
         | 
| 19 | 
            +
              end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
              def add_word(word, translation, &block)
         | 
| 22 | 
            +
                @dictionary.words << MemoryDictionary::Word.new(name: word, translation: translation)
         | 
| 23 | 
            +
                block.call if block_given?
         | 
| 24 | 
            +
              end
         | 
| 25 | 
            +
             | 
| 26 | 
            +
              def self.add_word(dictionary, word, translation, &block)
         | 
| 27 | 
            +
                translator = MemoryDictionary::Translator.new(dictionary)
         | 
| 28 | 
            +
                translator.add_word(word, translation, &block)
         | 
| 29 | 
            +
              end
         | 
| 30 | 
            +
             | 
| 31 | 
            +
            end
         | 
| @@ -0,0 +1,12 @@ | |
| 1 | 
            +
            class MemoryDictionary::Word
         | 
| 2 | 
            +
              include Mongoid::Document
         | 
| 3 | 
            +
              field :name, type: String
         | 
| 4 | 
            +
              field :translation, type: String
         | 
| 5 | 
            +
             | 
| 6 | 
            +
              embedded_in :dictionary, class_name: 'MemoryDictionary::Dictionary'
         | 
| 7 | 
            +
             | 
| 8 | 
            +
              validates :name, uniqueness: true
         | 
| 9 | 
            +
              validates :name, :translation, presence: true
         | 
| 10 | 
            +
              index({ name: 1 }, { unique: true, name: 'dictionary_words_index' })
         | 
| 11 | 
            +
             | 
| 12 | 
            +
            end
         | 
| @@ -0,0 +1,40 @@ | |
| 1 | 
            +
            # -*- encoding: utf-8 -*-
         | 
| 2 | 
            +
            $:.push File.expand_path("../lib", __FILE__)
         | 
| 3 | 
            +
            require "memory_dictionary/version"
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            Gem::Specification.new do |s|
         | 
| 6 | 
            +
              s.name                  = "memory_dictionary"
         | 
| 7 | 
            +
              s.version               = MemoryDictionary::Version::STRING
         | 
| 8 | 
            +
              s.platform              = Gem::Platform::RUBY
         | 
| 9 | 
            +
              s.required_ruby_version = ">= 2.1.0"
         | 
| 10 | 
            +
              s.authors               = ["Lucas Martins"]
         | 
| 11 | 
            +
              s.email                 = ["lucasmartins@railsnapraia.com"]
         | 
| 12 | 
            +
              s.homepage              = "http://rubygems.org/gems/memory_dictionary"
         | 
| 13 | 
            +
              s.summary               = "A general purposed memory dictionary for quick and dirty translations."
         | 
| 14 | 
            +
              s.description           = s.summary
         | 
| 15 | 
            +
              s.license               = "LGPL-3.0"
         | 
| 16 | 
            +
             | 
| 17 | 
            +
              s.files         = `git ls-files`.split("\n")
         | 
| 18 | 
            +
              s.test_files    = `git ls-files -- {test,spec,features}/*`.split("\n")
         | 
| 19 | 
            +
              s.executables   = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
         | 
| 20 | 
            +
              s.require_paths = ["lib"]
         | 
| 21 | 
            +
             | 
| 22 | 
            +
              if RUBY_ENGINE=='rbx'
         | 
| 23 | 
            +
                s.add_dependency 'rubysl'
         | 
| 24 | 
            +
              end
         | 
| 25 | 
            +
              if RUBY_PLATFORM=='ruby'
         | 
| 26 | 
            +
                s.add_dependency 'yajl-ruby'
         | 
| 27 | 
            +
              end
         | 
| 28 | 
            +
             | 
| 29 | 
            +
              s.add_dependency 'mongoid', '~> 5.1'
         | 
| 30 | 
            +
             | 
| 31 | 
            +
              s.add_development_dependency "rspec", "~> 2.1"
         | 
| 32 | 
            +
              s.add_development_dependency "rspec-mocks", "~> 2.1"
         | 
| 33 | 
            +
              s.add_development_dependency "rspec-expectations", "~> 2.1"
         | 
| 34 | 
            +
              s.add_development_dependency "database_cleaner", '~> 0'
         | 
| 35 | 
            +
              s.add_development_dependency "factory_girl", '~> 0'
         | 
| 36 | 
            +
              s.add_development_dependency "rake", '~> 0'
         | 
| 37 | 
            +
              s.add_development_dependency "pry", '~> 0'
         | 
| 38 | 
            +
              s.add_development_dependency "pry-nav", '~> 0'
         | 
| 39 | 
            +
              s.add_development_dependency "yard", '~> 0'
         | 
| 40 | 
            +
            end
         | 
| @@ -0,0 +1,9 @@ | |
| 1 | 
            +
            FactoryGirl.define do
         | 
| 2 | 
            +
              factory :dictionary, class: MemoryDictionary::Dictionary do
         | 
| 3 | 
            +
                name 'rosetta-stone'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
                factory :dictionary_with_words, class: MemoryDictionary::Dictionary do
         | 
| 6 | 
            +
                  words {[MemoryDictionary::Word.new(name: 'ruby', translation: 'is awesome'), MemoryDictionary::Word.new(name: 'rubinius', translation: 'is faster')]}
         | 
| 7 | 
            +
                end
         | 
| 8 | 
            +
              end
         | 
| 9 | 
            +
            end
         | 
| @@ -0,0 +1,36 @@ | |
| 1 | 
            +
            require "spec_helper"
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            describe MemoryDictionary::Dictionary do
         | 
| 4 | 
            +
              #smoke test
         | 
| 5 | 
            +
             | 
| 6 | 
            +
              let(:dictionary) { create(:dictionary_with_words) }
         | 
| 7 | 
            +
             | 
| 8 | 
            +
              before(:each) { dictionary.save }
         | 
| 9 | 
            +
             | 
| 10 | 
            +
              describe "#words" do
         | 
| 11 | 
            +
                it "has words" do
         | 
| 12 | 
            +
                  expect(dictionary.reload.words.count).to eq(2)
         | 
| 13 | 
            +
                end
         | 
| 14 | 
            +
              end
         | 
| 15 | 
            +
             | 
| 16 | 
            +
              describe "#translations" do
         | 
| 17 | 
            +
                it "retrieve translations" do
         | 
| 18 | 
            +
                  expect(dictionary.reload.translations).to eq(["is awesome", "is faster"])
         | 
| 19 | 
            +
                end
         | 
| 20 | 
            +
              end
         | 
| 21 | 
            +
             | 
| 22 | 
            +
              describe "#words_by_translation" do
         | 
| 23 | 
            +
                it "search words by translation" do
         | 
| 24 | 
            +
                  words = dictionary.reload.words_by_translation("is faster")
         | 
| 25 | 
            +
                  expect(words.count).to eq(1)
         | 
| 26 | 
            +
                  expect(words.first.name).to eq('rubinius')
         | 
| 27 | 
            +
                end
         | 
| 28 | 
            +
              end
         | 
| 29 | 
            +
             | 
| 30 | 
            +
              describe "#append_word" do
         | 
| 31 | 
            +
                it "appends a new word" do
         | 
| 32 | 
            +
                  expect{ dictionary.reload.append_word('jruby', 'is jruby...') }.to change{ dictionary.reload.words.count}.by 1
         | 
| 33 | 
            +
                end
         | 
| 34 | 
            +
              end
         | 
| 35 | 
            +
             | 
| 36 | 
            +
            end
         | 
| @@ -0,0 +1,80 @@ | |
| 1 | 
            +
            require "spec_helper"
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            describe MemoryDictionary::Translator do
         | 
| 4 | 
            +
             | 
| 5 | 
            +
              #smoke test
         | 
| 6 | 
            +
              describe "#initialize" do
         | 
| 7 | 
            +
             | 
| 8 | 
            +
                context 'when theres a dictionary' do
         | 
| 9 | 
            +
                  let(:dictionary) { create(:dictionary) }
         | 
| 10 | 
            +
                  let(:translator) { MemoryDictionary::Translator.new(dictionary.name) }
         | 
| 11 | 
            +
                  it "has a dictionary" do
         | 
| 12 | 
            +
                    expect{translator}.not_to raise_error
         | 
| 13 | 
            +
                  end
         | 
| 14 | 
            +
                end
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                context 'when there is no dictionary' do
         | 
| 17 | 
            +
                  let(:translator) { MemoryDictionary::Translator.new('wut?') }
         | 
| 18 | 
            +
                  it "does not have a dictionary" do
         | 
| 19 | 
            +
                    expect{translator}.to raise_error(MemoryDictionary::Errors::DictionaryNotFoundException)
         | 
| 20 | 
            +
                  end
         | 
| 21 | 
            +
                end
         | 
| 22 | 
            +
              
         | 
| 23 | 
            +
              end
         | 
| 24 | 
            +
             | 
| 25 | 
            +
              describe '#translate_word' do
         | 
| 26 | 
            +
                let(:dictionary) { create(:dictionary_with_words) }
         | 
| 27 | 
            +
                let(:translator) { MemoryDictionary::Translator.new(dictionary.name) }
         | 
| 28 | 
            +
                context 'when word exists' do
         | 
| 29 | 
            +
                  it 'translates the word' do
         | 
| 30 | 
            +
                    expect(translator.translate('ruby')).to eq('is awesome')
         | 
| 31 | 
            +
                  end
         | 
| 32 | 
            +
                end
         | 
| 33 | 
            +
                context 'when word do not exist' do
         | 
| 34 | 
            +
                  it 'raises WordNotFoundException' do
         | 
| 35 | 
            +
                    expect{translator.translate('waka waka')}.to raise_error(MemoryDictionary::Errors::WordNotFoundException)
         | 
| 36 | 
            +
                  end
         | 
| 37 | 
            +
                end    
         | 
| 38 | 
            +
              end
         | 
| 39 | 
            +
             | 
| 40 | 
            +
              describe '#add_word' do
         | 
| 41 | 
            +
                let(:dictionary) { create(:dictionary, name: 'empty-dic') }
         | 
| 42 | 
            +
                let(:translator) { MemoryDictionary::Translator.new(dictionary.name) }
         | 
| 43 | 
            +
             | 
| 44 | 
            +
                context 'without a block' do
         | 
| 45 | 
            +
                  it 'adds a new word to the dictionary' do
         | 
| 46 | 
            +
                    expect(dictionary.words.count).to eq(0)
         | 
| 47 | 
            +
                    expect{ translator.add_word('new word', 'new translation') }.to change{ dictionary.reload.words.count }.by(1)
         | 
| 48 | 
            +
                  end
         | 
| 49 | 
            +
                  it 'translates a recently added word' do
         | 
| 50 | 
            +
                    translator.add_word('recently added word', 'recently added translation')
         | 
| 51 | 
            +
                    expect(translator.translate('recently added word')).to eq('recently added translation')
         | 
| 52 | 
            +
                  end
         | 
| 53 | 
            +
                end
         | 
| 54 | 
            +
             | 
| 55 | 
            +
                context 'with a block' do
         | 
| 56 | 
            +
                  it 'translates a word and runs a block' do
         | 
| 57 | 
            +
                    translator.add_word('recently added word', 'recently added translation') do
         | 
| 58 | 
            +
                      expect(translator.translate('recently added word')).to eq('recently added translation')
         | 
| 59 | 
            +
                    end
         | 
| 60 | 
            +
                  end
         | 
| 61 | 
            +
                end
         | 
| 62 | 
            +
             | 
| 63 | 
            +
              end
         | 
| 64 | 
            +
             | 
| 65 | 
            +
              describe '.add_word' do
         | 
| 66 | 
            +
                let(:dictionary) { create(:dictionary, name: 'instance-dic') }
         | 
| 67 | 
            +
                let(:translator) { MemoryDictionary::Translator.new(dictionary.name) }
         | 
| 68 | 
            +
                
         | 
| 69 | 
            +
                it 'calls the instance method with all the params' do
         | 
| 70 | 
            +
                  pending 'unstable travis exec'
         | 
| 71 | 
            +
                  translator
         | 
| 72 | 
            +
                  block = lambda do
         | 
| 73 | 
            +
                    puts "I'm a block"
         | 
| 74 | 
            +
                  end
         | 
| 75 | 
            +
                  MemoryDictionary::Translator.any_instance.should_receive(:add_word).with('new word', 'new translation', &block)
         | 
| 76 | 
            +
                  MemoryDictionary::Translator.add_word('instance-dic', 'new word', 'new translation', &block)
         | 
| 77 | 
            +
                end
         | 
| 78 | 
            +
              end
         | 
| 79 | 
            +
             | 
| 80 | 
            +
            end
         | 
    
        data/spec/spec_helper.rb
    ADDED
    
    | @@ -0,0 +1,52 @@ | |
| 1 | 
            +
            require 'codeclimate-test-reporter'
         | 
| 2 | 
            +
            CodeClimate::TestReporter.start
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            require 'pry'
         | 
| 5 | 
            +
            require 'pry-nav'
         | 
| 6 | 
            +
            require 'memory_dictionary'
         | 
| 7 | 
            +
            require 'rspec'
         | 
| 8 | 
            +
            require 'rspec/mocks'
         | 
| 9 | 
            +
            require 'rspec/expectations'
         | 
| 10 | 
            +
            require "pathname"
         | 
| 11 | 
            +
            require 'database_cleaner'
         | 
| 12 | 
            +
            require 'factory_girl'
         | 
| 13 | 
            +
             | 
| 14 | 
            +
            MemoryDictionary::logger.level = Logger::ERROR
         | 
| 15 | 
            +
             | 
| 16 | 
            +
            if ENV['TRAVIS']==true
         | 
| 17 | 
            +
              begin
         | 
| 18 | 
            +
                require 'coveralls'
         | 
| 19 | 
            +
                Coveralls.wear!
         | 
| 20 | 
            +
              rescue Exception => e
         | 
| 21 | 
            +
                puts "Coveralls not available!"
         | 
| 22 | 
            +
              end  
         | 
| 23 | 
            +
            end
         | 
| 24 | 
            +
             | 
| 25 | 
            +
            SPECDIR = Pathname.new(File.dirname(__FILE__))
         | 
| 26 | 
            +
            TMPDIR = SPECDIR.join("tmp")
         | 
| 27 | 
            +
             | 
| 28 | 
            +
            Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each {|r| require r}
         | 
| 29 | 
            +
            Dir[File.dirname(__FILE__) + "/factories/**/*.rb"].each {|r| require r}
         | 
| 30 | 
            +
             | 
| 31 | 
            +
            RSpec.configure do |config|
         | 
| 32 | 
            +
              config.treat_symbols_as_metadata_keys_with_true_values = true
         | 
| 33 | 
            +
              config.order = :random
         | 
| 34 | 
            +
              config.include FactoryGirl::Syntax::Methods
         | 
| 35 | 
            +
              config.before { FileUtils.mkdir_p(TMPDIR) }
         | 
| 36 | 
            +
              config.mock_with :rspec
         | 
| 37 | 
            +
              
         | 
| 38 | 
            +
              config.before(:suite) do
         | 
| 39 | 
            +
                DatabaseCleaner.clean_with(:truncation)
         | 
| 40 | 
            +
                DatabaseCleaner.strategy = :truncation
         | 
| 41 | 
            +
              end
         | 
| 42 | 
            +
             | 
| 43 | 
            +
              config.around(:each) do |example|
         | 
| 44 | 
            +
                DatabaseCleaner[:mongoid].strategy = :truncation
         | 
| 45 | 
            +
                DatabaseCleaner.strategy = :truncation
         | 
| 46 | 
            +
                DatabaseCleaner.start
         | 
| 47 | 
            +
                example.run
         | 
| 48 | 
            +
                DatabaseCleaner.clean
         | 
| 49 | 
            +
              end
         | 
| 50 | 
            +
            end
         | 
| 51 | 
            +
             | 
| 52 | 
            +
            Mongoid.load!("spec/support/mongoid.yml", 'test')
         | 
| @@ -0,0 +1,23 @@ | |
| 1 | 
            +
            development:
         | 
| 2 | 
            +
              # Configure available database sessions. (required)
         | 
| 3 | 
            +
              sessions:
         | 
| 4 | 
            +
                # Defines the default session. (required)
         | 
| 5 | 
            +
                default:
         | 
| 6 | 
            +
                  # Defines the name of the default database that Mongoid can connect to.
         | 
| 7 | 
            +
                  # (required).
         | 
| 8 | 
            +
                  database: memory_dictionary_development
         | 
| 9 | 
            +
                  # Provides the hosts the default session can connect to. Must be an array
         | 
| 10 | 
            +
                  # of host:port pairs. (required)
         | 
| 11 | 
            +
                  hosts:
         | 
| 12 | 
            +
                    - localhost:<%= ENV['MONGODB_PORT'] || '27017' %>
         | 
| 13 | 
            +
            test:
         | 
| 14 | 
            +
              sessions:
         | 
| 15 | 
            +
                default:
         | 
| 16 | 
            +
                  database: memory_dictionary_test
         | 
| 17 | 
            +
                  hosts:
         | 
| 18 | 
            +
                    - localhost:<%= ENV['MONGODB_PORT'] || '27017' %>
         | 
| 19 | 
            +
                  options:
         | 
| 20 | 
            +
                    # In the test environment we lower the retries and retry interval to
         | 
| 21 | 
            +
                    # low amounts for fast failures.
         | 
| 22 | 
            +
                    max_retries: 1
         | 
| 23 | 
            +
                    retry_interval: 0
         | 
    
        data/tmp/.keep
    ADDED
    
    | 
            File without changes
         | 
    
        metadata
    ADDED
    
    | @@ -0,0 +1,222 @@ | |
| 1 | 
            +
            --- !ruby/object:Gem::Specification
         | 
| 2 | 
            +
            name: memory_dictionary
         | 
| 3 | 
            +
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            +
              version: 1.0.0
         | 
| 5 | 
            +
            platform: ruby
         | 
| 6 | 
            +
            authors:
         | 
| 7 | 
            +
            - Lucas Martins
         | 
| 8 | 
            +
            autorequire: 
         | 
| 9 | 
            +
            bindir: bin
         | 
| 10 | 
            +
            cert_chain: []
         | 
| 11 | 
            +
            date: 2016-05-23 00:00:00.000000000 Z
         | 
| 12 | 
            +
            dependencies:
         | 
| 13 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 14 | 
            +
              name: mongoid
         | 
| 15 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 16 | 
            +
                requirements:
         | 
| 17 | 
            +
                - - "~>"
         | 
| 18 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 19 | 
            +
                    version: '5.1'
         | 
| 20 | 
            +
              type: :runtime
         | 
| 21 | 
            +
              prerelease: false
         | 
| 22 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 23 | 
            +
                requirements:
         | 
| 24 | 
            +
                - - "~>"
         | 
| 25 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 26 | 
            +
                    version: '5.1'
         | 
| 27 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 28 | 
            +
              name: rspec
         | 
| 29 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 30 | 
            +
                requirements:
         | 
| 31 | 
            +
                - - "~>"
         | 
| 32 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 33 | 
            +
                    version: '2.1'
         | 
| 34 | 
            +
              type: :development
         | 
| 35 | 
            +
              prerelease: false
         | 
| 36 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 37 | 
            +
                requirements:
         | 
| 38 | 
            +
                - - "~>"
         | 
| 39 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 40 | 
            +
                    version: '2.1'
         | 
| 41 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 42 | 
            +
              name: rspec-mocks
         | 
| 43 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 44 | 
            +
                requirements:
         | 
| 45 | 
            +
                - - "~>"
         | 
| 46 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 47 | 
            +
                    version: '2.1'
         | 
| 48 | 
            +
              type: :development
         | 
| 49 | 
            +
              prerelease: false
         | 
| 50 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 51 | 
            +
                requirements:
         | 
| 52 | 
            +
                - - "~>"
         | 
| 53 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 54 | 
            +
                    version: '2.1'
         | 
| 55 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 56 | 
            +
              name: rspec-expectations
         | 
| 57 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 58 | 
            +
                requirements:
         | 
| 59 | 
            +
                - - "~>"
         | 
| 60 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 61 | 
            +
                    version: '2.1'
         | 
| 62 | 
            +
              type: :development
         | 
| 63 | 
            +
              prerelease: false
         | 
| 64 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 65 | 
            +
                requirements:
         | 
| 66 | 
            +
                - - "~>"
         | 
| 67 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 68 | 
            +
                    version: '2.1'
         | 
| 69 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 70 | 
            +
              name: database_cleaner
         | 
| 71 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 72 | 
            +
                requirements:
         | 
| 73 | 
            +
                - - "~>"
         | 
| 74 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 75 | 
            +
                    version: '0'
         | 
| 76 | 
            +
              type: :development
         | 
| 77 | 
            +
              prerelease: false
         | 
| 78 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 79 | 
            +
                requirements:
         | 
| 80 | 
            +
                - - "~>"
         | 
| 81 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 82 | 
            +
                    version: '0'
         | 
| 83 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 84 | 
            +
              name: factory_girl
         | 
| 85 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 86 | 
            +
                requirements:
         | 
| 87 | 
            +
                - - "~>"
         | 
| 88 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 89 | 
            +
                    version: '0'
         | 
| 90 | 
            +
              type: :development
         | 
| 91 | 
            +
              prerelease: false
         | 
| 92 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 93 | 
            +
                requirements:
         | 
| 94 | 
            +
                - - "~>"
         | 
| 95 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 96 | 
            +
                    version: '0'
         | 
| 97 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 98 | 
            +
              name: rake
         | 
| 99 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 100 | 
            +
                requirements:
         | 
| 101 | 
            +
                - - "~>"
         | 
| 102 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 103 | 
            +
                    version: '0'
         | 
| 104 | 
            +
              type: :development
         | 
| 105 | 
            +
              prerelease: false
         | 
| 106 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 107 | 
            +
                requirements:
         | 
| 108 | 
            +
                - - "~>"
         | 
| 109 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 110 | 
            +
                    version: '0'
         | 
| 111 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 112 | 
            +
              name: pry
         | 
| 113 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 114 | 
            +
                requirements:
         | 
| 115 | 
            +
                - - "~>"
         | 
| 116 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 117 | 
            +
                    version: '0'
         | 
| 118 | 
            +
              type: :development
         | 
| 119 | 
            +
              prerelease: false
         | 
| 120 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 121 | 
            +
                requirements:
         | 
| 122 | 
            +
                - - "~>"
         | 
| 123 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 124 | 
            +
                    version: '0'
         | 
| 125 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 126 | 
            +
              name: pry-nav
         | 
| 127 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 128 | 
            +
                requirements:
         | 
| 129 | 
            +
                - - "~>"
         | 
| 130 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 131 | 
            +
                    version: '0'
         | 
| 132 | 
            +
              type: :development
         | 
| 133 | 
            +
              prerelease: false
         | 
| 134 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 135 | 
            +
                requirements:
         | 
| 136 | 
            +
                - - "~>"
         | 
| 137 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 138 | 
            +
                    version: '0'
         | 
| 139 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 140 | 
            +
              name: yard
         | 
| 141 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 142 | 
            +
                requirements:
         | 
| 143 | 
            +
                - - "~>"
         | 
| 144 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 145 | 
            +
                    version: '0'
         | 
| 146 | 
            +
              type: :development
         | 
| 147 | 
            +
              prerelease: false
         | 
| 148 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 149 | 
            +
                requirements:
         | 
| 150 | 
            +
                - - "~>"
         | 
| 151 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 152 | 
            +
                    version: '0'
         | 
| 153 | 
            +
            description: A general purposed memory dictionary for quick and dirty translations.
         | 
| 154 | 
            +
            email:
         | 
| 155 | 
            +
            - lucasmartins@railsnapraia.com
         | 
| 156 | 
            +
            executables: []
         | 
| 157 | 
            +
            extensions: []
         | 
| 158 | 
            +
            extra_rdoc_files: []
         | 
| 159 | 
            +
            files:
         | 
| 160 | 
            +
            - ".env.sample"
         | 
| 161 | 
            +
            - ".gitignore"
         | 
| 162 | 
            +
            - ".rspec"
         | 
| 163 | 
            +
            - ".ruby-version"
         | 
| 164 | 
            +
            - ".travis.yml"
         | 
| 165 | 
            +
            - ".yardopts"
         | 
| 166 | 
            +
            - Gemfile
         | 
| 167 | 
            +
            - LICENSE
         | 
| 168 | 
            +
            - Rakefile
         | 
| 169 | 
            +
            - Readme.md
         | 
| 170 | 
            +
            - lib/memory_dictionary.rb
         | 
| 171 | 
            +
            - lib/memory_dictionary/dictionary.rb
         | 
| 172 | 
            +
            - lib/memory_dictionary/errors.rb
         | 
| 173 | 
            +
            - lib/memory_dictionary/errors/blacklisted_exception.rb
         | 
| 174 | 
            +
            - lib/memory_dictionary/errors/dictionary_not_found_exception.rb
         | 
| 175 | 
            +
            - lib/memory_dictionary/errors/word_not_found_exception.rb
         | 
| 176 | 
            +
            - lib/memory_dictionary/translator.rb
         | 
| 177 | 
            +
            - lib/memory_dictionary/version.rb
         | 
| 178 | 
            +
            - lib/memory_dictionary/word.rb
         | 
| 179 | 
            +
            - memory_dictionary.gemspec
         | 
| 180 | 
            +
            - spec/factories/dictionary.rb
         | 
| 181 | 
            +
            - spec/factories/word.rb
         | 
| 182 | 
            +
            - spec/memory_dictionary/dictionary_spec.rb
         | 
| 183 | 
            +
            - spec/memory_dictionary/translator_spec.rb
         | 
| 184 | 
            +
            - spec/memory_dictionary/word_spec.rb
         | 
| 185 | 
            +
            - spec/memory_dictionary_spec.rb
         | 
| 186 | 
            +
            - spec/spec_helper.rb
         | 
| 187 | 
            +
            - spec/support/mongoid.yml
         | 
| 188 | 
            +
            - tmp/.keep
         | 
| 189 | 
            +
            homepage: http://rubygems.org/gems/memory_dictionary
         | 
| 190 | 
            +
            licenses:
         | 
| 191 | 
            +
            - LGPL-3.0
         | 
| 192 | 
            +
            metadata: {}
         | 
| 193 | 
            +
            post_install_message: 
         | 
| 194 | 
            +
            rdoc_options: []
         | 
| 195 | 
            +
            require_paths:
         | 
| 196 | 
            +
            - lib
         | 
| 197 | 
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         | 
| 198 | 
            +
              requirements:
         | 
| 199 | 
            +
              - - ">="
         | 
| 200 | 
            +
                - !ruby/object:Gem::Version
         | 
| 201 | 
            +
                  version: 2.1.0
         | 
| 202 | 
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         | 
| 203 | 
            +
              requirements:
         | 
| 204 | 
            +
              - - ">="
         | 
| 205 | 
            +
                - !ruby/object:Gem::Version
         | 
| 206 | 
            +
                  version: '0'
         | 
| 207 | 
            +
            requirements: []
         | 
| 208 | 
            +
            rubyforge_project: 
         | 
| 209 | 
            +
            rubygems_version: 2.6.4
         | 
| 210 | 
            +
            signing_key: 
         | 
| 211 | 
            +
            specification_version: 4
         | 
| 212 | 
            +
            summary: A general purposed memory dictionary for quick and dirty translations.
         | 
| 213 | 
            +
            test_files:
         | 
| 214 | 
            +
            - spec/factories/dictionary.rb
         | 
| 215 | 
            +
            - spec/factories/word.rb
         | 
| 216 | 
            +
            - spec/memory_dictionary/dictionary_spec.rb
         | 
| 217 | 
            +
            - spec/memory_dictionary/translator_spec.rb
         | 
| 218 | 
            +
            - spec/memory_dictionary/word_spec.rb
         | 
| 219 | 
            +
            - spec/memory_dictionary_spec.rb
         | 
| 220 | 
            +
            - spec/spec_helper.rb
         | 
| 221 | 
            +
            - spec/support/mongoid.yml
         | 
| 222 | 
            +
            has_rdoc: 
         |