Permalink
Browse files

ready for the start

  • Loading branch information...
1 parent 08f21d2 commit e724b8daa84f5d4a280d844ba59bdfb81bc763d0 prikha committed Oct 11, 2012
Showing with 71 additions and 14 deletions.
  1. +30 −8 lib/dickens.rb
  2. +21 −3 lib/dickens/find_items.rb
  3. +20 −3 spec/dickens/dickens_spec.rb
View
@@ -1,5 +1,6 @@
require "dickens/version"
require "dickens/list_items"
+require "dickens/find_items"
require 'rbconfig'
require RbConfig::CONFIG['target_os'] == 'mingw32' && !(RUBY_VERSION =~ /1.9/) ? 'win32/open3' : 'open3'
@@ -12,6 +13,17 @@
require 'active_support/core_ext/blank'
end
+class Object
+ ##
+ # @person ? @person.name : nil
+ # vs
+ # @person.try(:name)
+ def try(method)
@prikha

prikha Aug 16, 2013

Owner

Ok, that was dirty. Didn't try to use it with Rails at all. I'll try to fix it an a mean time.

+ send method if respond_to? method
+ end
+end
+
+
module Dickens
class StarDict
@@executable = "sdcv"
@@ -25,7 +37,7 @@ class StarDict
cattr_accessor :config, :executable
- #Использование:
+ ##Использование:
# sdcv [ПАРАМЕТР...] words
#
#Параметры приложения:
@@ -40,7 +52,21 @@ class StarDict
class << self
def find(word)
command = [@@executable, prepare_options, word.to_s].join(" ")
- invoke(command).join("")
+ status, *response = invoke(command)
+ puts status
+ Dickens::FindItem.parse(response.join(""), word)
+ end
+
+ def where(word, dictionaries=[])
+ return find(word) if dictionaries.empty? or !dictionaries.is_a?(Array)
+ cache=@@config
+ result=[]
+ dictionaries.each do |d|
+ @@config[:use_dict] = d.try(:name) || d
+ result+= find(word)
+ end
+ @@config=cache
+ result
end
def list
@@ -56,8 +82,7 @@ def invoke(command)
def prepare_options
raise WrongFormatError unless @@config.is_a? Hash
- options = normalize_options(@@config)
- options.flatten
+ normalize_options(@@config).flatten
end
def normalize_options(options)
@@ -75,12 +100,9 @@ def normalize_arg(arg)
end
def normalize_value(value)
- value.is_a?(TrueClass) ? nil : value.to_s
+ value.is_a?(TrueClass) ? nil : "\"#{value.to_s}\""
end
end
-
-
-
end
end
View
@@ -1,11 +1,29 @@
module Dickens
class FindItem
- attr_accessor :dictionary, :word, :matching
+ attr_accessor :article, :dictionary, :word, :matching
- def initialize(string)
- /\n-->(?<dictionary>.*)\n-->(?<word>.*)\n/.match(list)
+ def initialize(array, word)
+ extract = /-->(?<dic>.*)\n-->(?<w>.*)\n\n(?<a>[\W|\w|.]*)/.match(array)
+ @dictionary= extract[:dic]
+ @word= extract[:w]
+ @article= extract[:a].strip
+ @matching= (@word == word)
+ end
+ def to_s
+ "-->#{dictionary}\n-->#{word}\n===\n#{article}"
end
+ def self.parse(string,word)
+ out=[]
+
+ #split console output by article delimiter but preserve the delimiter info
+ #then slice them by pairs [delimiter, article] and join back
+ #then initialize FindItems
+ string.split(/(-->.*\n-->.*\n)/).reject(&:empty?).each_slice(2).map(&:join).each do |e|
+ out<<Dickens::FindItem.new(e, word)
+ end
+ out
+ end
end
end
@@ -2,13 +2,30 @@
require 'spec_helper'
describe Dickens::StarDict do
- it "should get the result" do
+ it "should FIND the result" do
Dickens::StarDict.find("петля").should_not be_nil
+ Dickens::StarDict.find("петля").should be_a(Array)
+ Dickens::StarDict.find("петля").first.should be_a(Dickens::FindItem)
end
- it "should list the dictionaries" do
+ if "should return NIL if nothing was found"
+ Dickens::StarDict.find("wrong_string_query").should be_nil
+ end
+
+
+ it "should LIST the dictionaries" do
Dickens::StarDict.list.should be_a(Array)
Dickens::StarDict.list.count.should be Dickens::StarDict.list.compact.count
Dickens::StarDict.list.first.should be_a(Dickens::ListItem)
end
-end
+end
+
+describe Dickens::FindItem do
+ before do
+ @item=Dickens::StarDict.find("петля").first
+ end
+ it { @item.should respond_to(:dictionary)}
+ it { @item.should respond_to(:word)}
+ it { @item.should respond_to(:article)}
+ it { @item.should respond_to(:matching)}
+end

0 comments on commit e724b8d

Please sign in to comment.