Class: Dat::Word

Inherits:
Object
  • Object
show all
Defined in:
lib/dat/word.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(word, defn = "") ⇒ Word

Returns a new instance of Word.



9
10
11
12
13
# File 'lib/dat/word.rb', line 9

def initialize(word, defn="")
  @word = word.upcase
  @definition = defn
  @relatives = Set.new
end

Instance Attribute Details

#definitionObject

Returns the value of attribute definition.



6
7
8
# File 'lib/dat/word.rb', line 6

def definition
  @definition
end

#relativesObject (readonly)

Returns the value of attribute relatives.



5
6
7
# File 'lib/dat/word.rb', line 5

def relatives
  @relatives
end

#typeObject

Returns the value of attribute type.



6
7
8
# File 'lib/dat/word.rb', line 6

def type
  @type
end

#wordObject (readonly) Also known as: get

Returns the value of attribute word.



5
6
7
# File 'lib/dat/word.rb', line 5

def word
  @word
end

Class Method Details

.relatives(*words) ⇒ Object

Helper to declare that words come from the same root



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dat/word.rb', line 38

def self.relatives(*words)
  # make a set of all the relatives
  relatives = Set.new
  words.each do |word|
    word.relatives.each { |rs| relatives.add(rs) }
    relatives.add(word)
  end

  # update words to include the full list of relatives
  words.each do |word|
    relatives.each { |r| word.add_relative(r) }
  end
end

Instance Method Details

#add_relative(word) ⇒ Object Also known as: <<



15
16
17
# File 'lib/dat/word.rb', line 15

def add_relative(word)
  @relatives.add(word) if word.get != @word && !@relatives.include?(word)
end

#isolate!Object



20
21
22
23
24
# File 'lib/dat/word.rb', line 20

def isolate!
  @relatives.each { |r| r.relatives.delete(self) }
  @relatives = Set.new
  @type = nil
end

#to_dict_entryObject



30
31
32
33
34
35
# File 'lib/dat/word.rb', line 30

def to_dict_entry
  str = @word.clone
  str << (@type ? " (" << @type << ") " : " ")
  str << @definition.strip << " " unless @definition.strip.empty?
  str << "[#{@relatives.to_a.join(" ")}]"
end

#to_sObject



26
27
28
# File 'lib/dat/word.rb', line 26

def to_s
  @word
end