Class: Dictuby::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/dictuby/runner.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Runner

Returns a new instance of Runner.



8
9
10
11
12
13
14
15
16
17
# File 'lib/dictuby/runner.rb', line 8

def initialize(args)
    @options = ArgsParser.parse(args)
    @config = DictubyConfig.new
    @args = args

    @sources = [SlovnikAzetSK, SlovnikCZ]

    @dicts = @sources.inject([]) {|r, p| r << p.dicts.keys}
    @dicts.flatten!.sort!
end

Class Method Details

.execute(args) ⇒ Object



4
5
6
# File 'lib/dictuby/runner.rb', line 4

def self.execute(args)
    new(args).run
end

Instance Method Details

#runObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dictuby/runner.rb', line 19

def run
    if @options[:list]
        @dicts.each {|d| puts d}
    elsif @options[:set]
        if @dicts.include?(@options[:set])
            @config.set('dictionary', @options[:set])
        else
            puts 'Invalid dictionary'
        end
    elsif @options[:get]
        puts @config.get('dictionary')
    else
        if @args.size == 1
            dict, query = @config.get('dictionary'), @args[0]
        elsif @args.size == 2
            dict, query = *@args
        end

        map = @sources.inject({}) do |r,p| 
            p.dicts.keys.each {|d| r[d] = p}; r
        end

        if map.include?(dict)
            puts query
            map[dict].lookup(dict, query).each do |word|
                puts "- #{word}"
            end
        else
            puts 'Invalid dictionary'
        end
    end
end