Class: Connie::Dictionary

Inherits:
Object
  • Object
show all
Defined in:
lib/connie/dictionary.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dictionary_name) ⇒ Dictionary

Returns a new instance of Dictionary.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/connie/dictionary.rb', line 6

def initialize(dictionary_name)
  @name = dictionary_name
  @module_name = dictionary_name.gsub(%r{(?:^|_)([a-z])?}) { $1.upcase }
  
  #TODO: Check the format of the dictionary name
  raise DictionaryNameNotAllowed if %w(Dictionary Parser).include? @module_name      
  
  load_dictionary_files_and_modules
  
  Connie.register_dictionary self
end

Instance Attribute Details

#module_nameObject

Returns the value of attribute module_name.



4
5
6
# File 'lib/connie/dictionary.rb', line 4

def module_name
  @module_name
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/connie/dictionary.rb', line 4

def name
  @name
end

Instance Method Details

#inspectObject



18
19
20
21
22
23
24
# File 'lib/connie/dictionary.rb', line 18

def inspect
  from_the_module = Connie.const_defined?(@module_name) ? Connie.const_get(@module_name).instance_methods : []

  all_the_methods = @word_lists.keys.concat from_the_module
  
  "#{}<#{self.class} - Connie::#{@module_name} - #{all_the_methods.join(' ')}>"
end

#interpolate(string) ⇒ Object Also known as: i



26
27
28
# File 'lib/connie/dictionary.rb', line 26

def interpolate string
  Connie::Parser.process string, self
end

#load_dictionary_files_and_modulesObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/connie/dictionary.rb', line 32

def load_dictionary_files_and_modules
  @word_lists = []
  
  Connie.dictionaries_paths.each do |dictionaries_path|
    @word_lists.concat Dir[File.join dictionaries_path, @name, '*']
    @word_lists.concat Dir[File.join(dictionaries_path, "#{@name}.*")]
  end

  # Indexes the dictionary files into an array
  @word_lists = @word_lists.inject({}) {|a,f| a[f.split(%r{[./]}).last.to_sym]=f; a}
  
  @word_lists.delete :rb
  
  # Defines methods based on word lists
  @word_lists.keys.each do |list|
    instance_eval <<-LIST
    def #{list} options={}
      interpolate Connie.pick_a_line_from @word_lists[:#{list}], options[:line]
    end
    LIST
  end
  
  # Find and load modules
  modules = []
  Connie.dictionaries_paths.each do |dictionaries_path|
    modules.concat Dir[File.join dictionaries_path, "#{@name}.rb"]
  end
  modules.each {|m| require m}
  
  extend Connie.const_get(@module_name) if Connie.const_defined?(@module_name)
end