Class: Gloo::Core::Dictionary

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

Instance Attribute Summary collapse

Class Method 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

Class Method Details

.getObject

Get the singleton Dictionary and Initialize if this is the first time.



31
32
33
34
35
# File 'lib/gloo/core/dictionary.rb', line 31

def self.get
  o = Gloo::Core::Dictionary.instance
  o.init if o.verbs.count == 0
  return o
end

Instance Method Details

#find_obj(word) ⇒ Object

Find the object type by name.



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

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.



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

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.



101
102
103
# File 'lib/gloo/core/dictionary.rb', line 101

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

#get_verbsObject

Get the list of verbs, sorted.



108
109
110
# File 'lib/gloo/core/dictionary.rb', line 108

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

#initObject

Initialize verbs and objects in the dictionary.



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

def init
  # @engine.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.



116
117
118
119
120
121
122
123
124
# File 'lib/gloo/core/dictionary.rb', line 116

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)


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

def obj?( word )
  return false unless word

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

#register_obj(subclass) ⇒ Object

Register an object type.



47
48
49
# File 'lib/gloo/core/dictionary.rb', line 47

def register_obj( subclass )
  @obj_references << subclass
end

#register_obj_post_start(obj_class) ⇒ Object

Register an object type after start up.



155
156
157
# File 'lib/gloo/core/dictionary.rb', line 155

def register_obj_post_start( obj_class )
  add_object obj_class
end

#register_verb(subclass) ⇒ Object

Register a verb.



40
41
42
# File 'lib/gloo/core/dictionary.rb', line 40

def register_verb( subclass )
  @verb_references << subclass
end

#register_verb_post_start(verb_class) ⇒ Object

Register a verb after start up.



148
149
150
# File 'lib/gloo/core/dictionary.rb', line 148

def register_verb_post_start( verb_class )
  add_verb verb_class
end

#show_keywordsObject

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



130
131
132
133
134
135
136
137
138
139
# File 'lib/gloo/core/dictionary.rb', line 130

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

#unregister_obj(obj_class) ⇒ Object

Un-Register an object.



173
174
175
176
177
178
179
# File 'lib/gloo/core/dictionary.rb', line 173

def unregister_obj( obj_class )
  @objs.delete( obj_class.typename )
  @objs.delete( obj_class.short_typename )

  @keywords.delete( obj_class.typename )
  @keywords.delete( obj_class.short_typename )
end

#unregister_verb(verb_class) ⇒ Object

Un-Register a verb.



162
163
164
165
166
167
168
# File 'lib/gloo/core/dictionary.rb', line 162

def unregister_verb( verb_class )
  @verbs.delete( verb_class.keyword )
  @verbs.delete( verb_class.keyword_shortcut )

  @keywords.delete( verb_class.keyword )
  @keywords.delete( verb_class.keyword_shortcut )
end

#verb?(word) ⇒ Boolean

Is the given word a verb?

Returns:

  • (Boolean)


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

def verb?( word )
  return false unless word

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