Module: Inform::Modular

Included in:
Object
Defined in:
lib/runtime/module.rb

Overview

The Inform::Modular module

Instance Method Summary collapse

Instance Method Details

#after_initializeObject



136
137
138
139
# File 'lib/runtime/module.rb', line 136

def after_initialize
  self.apply_modules
  super
end

#apply_modules(exceptions = []) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
# File 'lib/runtime/module.rb', line 176

def apply_modules(exceptions = [])
  nil_safe_modules.each do |module_name|
    next if exceptions.include?(module_name.to_sym)
    mod = find_module(module_name)
    next if mod.nil?
    mod.ancestors.reverse.each do |ancestor|
      self.extend(ancestor)
    end
  end
  self
end

#mod(*args) ⇒ Object



147
148
149
150
151
152
153
154
155
# File 'lib/runtime/module.rb', line 147

def mod(*args)
  a = args.flatten.collect(&:to_s)
  (a - (nil_safe_modules & a)).each do |mod|
    add_module(Inform::Module.find_or_create(name: mod))
  end
  save
  apply_modules
  self
end

#modulesObject



141
142
143
144
145
# File 'lib/runtime/module.rb', line 141

def modules
  super
rescue StandardError => _e
  Array::Empty
end

#modules_semaphoreObject



168
169
170
# File 'lib/runtime/module.rb', line 168

def modules_semaphore
  @modules_semaphore ||= Mutex.new
end

#nil_safe_modulesObject



172
173
174
# File 'lib/runtime/module.rb', line 172

def nil_safe_modules
  modules.compact.map(&:to_s)
end

#reset_modulesObject

TODO: This is broken somehow FIXME



189
190
191
192
193
194
195
196
197
198
# File 'lib/runtime/module.rb', line 189

def reset_modules
  nil_safe_modules.each do |module_name|
    remove_module(module_name)
    mod = find_module(module_name)
    next if mod.nil?
    mod.instance_methods.each { |method_name| undef_method(method_name) }
  end
  save
  self
end

#unmod(*args) ⇒ Object



157
158
159
160
161
162
163
164
165
166
# File 'lib/runtime/module.rb', line 157

def unmod(*args)
  (nil_safe_modules & args.flatten.collect(&:to_s)).each do |mod|
    remove_module(Inform::Module.find(name: mod))
  end
  save
  i = parent(self)
  self.remove
  i << self
  self
end