Class: Gloo::Core::Dictionary

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/gloo/core/dictionary.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDictionary

Set up the object dictionary.



19
20
21
22
23
24
25
# File 'lib/gloo/core/dictionary.rb', line 19

def initialize
  @verbs = {}
  @objs = {}
  @verb_references = []
  @obj_references = []
  @keywords = []
end

Instance Attribute Details

#keywordsObject (readonly)

Returns the value of attribute keywords.



14
15
16
# File 'lib/gloo/core/dictionary.rb', line 14

def keywords
  @keywords
end

#objsObject (readonly)

Returns the value of attribute objs.



14
15
16
# File 'lib/gloo/core/dictionary.rb', line 14

def objs
  @objs
end

#verbsObject (readonly)

Returns the value of attribute verbs.



14
15
16
# File 'lib/gloo/core/dictionary.rb', line 14

def verbs
  @verbs
end

Instance Method Details

#find_obj(word) ⇒ Object

Find the object type by name.



62
63
64
65
66
67
# File 'lib/gloo/core/dictionary.rb', line 62

def find_obj( word )
  return nil unless word
  return nil unless obj?( word )

  return @objs[ word.downcase ]
end

#find_verb(verb) ⇒ Object

Find the verb by name.



81
82
83
84
85
86
# File 'lib/gloo/core/dictionary.rb', line 81

def find_verb( verb )
  return nil unless verb
  return nil unless verb?( verb )

  return @verbs[ verb.downcase ]
end

#get_obj_typesObject

Get the list of verbs, sorted.



91
92
93
# File 'lib/gloo/core/dictionary.rb', line 91

def get_obj_types
  return @obj_references.sort { |a, b| a.typename <=> b.typename }
end

#get_verbsObject

Get the list of verbs, sorted.



98
99
100
# File 'lib/gloo/core/dictionary.rb', line 98

def get_verbs
  return @verb_references.sort { |a, b| a.keyword <=> b.keyword }
end

#initObject

Initialize verbs and objects in the dictionary.



44
45
46
47
48
# File 'lib/gloo/core/dictionary.rb', line 44

def init
  $log.debug 'initializing dictionaries'
  init_verbs
  init_objs
end

#lookup_keyword(key) ⇒ Object

Lookup the keyword by name or shortcut. Return the keyword (name) or nil if it is not found.



106
107
108
109
110
111
112
113
114
# File 'lib/gloo/core/dictionary.rb', line 106

def lookup_keyword( key )
  v = find_verb key
  return v.keyword if v

  o = find_obj key
  return o.typename if o

  return nil
end

#obj?(word) ⇒ Boolean

Is the given word an object type?

Returns:

  • (Boolean)


53
54
55
56
57
# File 'lib/gloo/core/dictionary.rb', line 53

def obj?( word )
  return false unless word

  return @objs.key?( word.downcase )
end

#register_obj(subclass) ⇒ Object

Register an object type.



37
38
39
# File 'lib/gloo/core/dictionary.rb', line 37

def register_obj( subclass )
  @obj_references << subclass
end

#register_verb(subclass) ⇒ Object

Register a verb.



30
31
32
# File 'lib/gloo/core/dictionary.rb', line 30

def register_verb( subclass )
  @verb_references << subclass
end

#show_keywordsObject

Show a list of all keywords. This includes verbs and objects, names and shortcuts.



120
121
122
123
124
125
126
127
128
129
# File 'lib/gloo/core/dictionary.rb', line 120

def show_keywords
  str = ''
  @keywords.sort.each_with_index do |k, i|
    str << k.ljust( 20, ' ' )
    if ( ( i + 1 ) % 6 ).zero?
      puts str
      str = ''
    end
  end
end

#verb?(word) ⇒ Boolean

Is the given word a verb?

Returns:

  • (Boolean)


72
73
74
75
76
# File 'lib/gloo/core/dictionary.rb', line 72

def verb?( word )
  return false unless word

  return @verbs.key?( word.downcase )
end