Class: Module

Inherits:
Object
  • Object
show all
Defined in:
lib/tanuki/extensions/module.rb

Instance Method Summary collapse

Instance Method Details

#const_missing_with_balls(sym) ⇒ Object

Runs Tanuki::Loader for every missing constant in any namespace.



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/tanuki/extensions/module.rb', line 4

def const_missing_with_balls(sym)
  klass = "#{name + '::' unless name.nil? || (name == 'Object')}#{sym}"
  paths = Dir.glob(Tanuki::Loader.combined_class_path(klass))
  if paths.empty?
    unless Dir.glob(Tanuki::Loader.combined_class_dir(klass)).empty?
      return const_set(sym, Module.new)
    end
  else
    paths.reverse_each {|path| require path }
    return const_get(sym) if const_defined?(sym)
  end
  const_missing_without_balls(sym)
end

#const_set_recursive(sym, obj) ⇒ Object

Sets the named constant to the given object, returning that object. Creates new constants and modules recursively if no constant with the given name and nesting previously existed.



22
23
24
25
26
27
28
29
30
31
# File 'lib/tanuki/extensions/module.rb', line 22

def const_set_recursive(sym, obj)
  sym.to_s.split('::').inject(self) do |klass, const|
    if const_defined? const.to_sym
      const = const_get(const)
    else
      const = const_set(const, Module.new)
    end
    const
  end
end

#internal_attr_accessor(*syms) ⇒ Object

Creates a reader sym and a writer sym= for the instance variable @_sym.



35
36
37
38
# File 'lib/tanuki/extensions/module.rb', line 35

def internal_attr_accessor(*syms)
  internal_attr_reader(*syms)
  internal_attr_writer(*syms)
end

#internal_attr_reader(*syms) ⇒ Object

Creates a reader sym for the instance variable @_sym.



41
42
43
44
45
46
47
# File 'lib/tanuki/extensions/module.rb', line 41

def internal_attr_reader(*syms)
  syms.each do |sym|
    ivar = "@_#{sym}".to_sym
    instance_variable_set(ivar, nil) unless instance_variable_defined? ivar
    class_eval "def #{sym};#{ivar};end"
  end
end

#internal_attr_writer(*syms) ⇒ Object

Creates a writer sym= for the instance variable @_sym.



50
51
52
53
54
55
56
# File 'lib/tanuki/extensions/module.rb', line 50

def internal_attr_writer(*syms)
  syms.each do |sym|
    ivar = "@_#{sym}".to_sym
    instance_variable_set(ivar, nil) unless instance_variable_defined? ivar
    class_eval "def #{sym}=(obj);#{ivar}=obj;end"
  end
end