Class: Module

Inherits:
Object show all
Defined in:
lib/RubyExt/module.rb,
lib/RubyExt/ClassLoader.rb,
lib/RubyExt/Localization/Module.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.namespace_for(class_name) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/RubyExt/module.rb', line 36

def self.namespace_for class_name
  list = class_name.split("::")
  if list.size > 1
    list.pop
    return eval list.join("::"), TOPLEVEL_BINDING, __FILE__, __LINE__			
  else
    return nil
  end
end

Instance Method Details

#[](resource_name) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/RubyExt/module.rb', line 65

def [] resource_name
  self_ancestors_and_namespaces do |klass|
    if RubyExt::Resource.resource_exist? klass, resource_name
      return RubyExt::Resource.resource_get(klass, resource_name)
    end	
  end
  raise RubyExt::Resource::NotExist, "Resource '#{resource_name}' for Class '#{self.name}' doesn't exist!", caller
end

#[]=(resource_name, value) ⇒ Object



74
75
76
# File 'lib/RubyExt/module.rb', line 74

def []= resource_name, value
  RubyExt::Resource.resource_set self.name, resource_name, value
end

#const_missing(const) ⇒ Object

alias_method :old_const_missing, :const_missing



139
140
141
# File 'lib/RubyExt/ClassLoader.rb', line 139

def const_missing const
	return RubyExt::ClassLoader.load_class self, const.to_s
end

#each_ancestor(include_standard = false, &block) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/RubyExt/module.rb', line 19

def each_ancestor include_standard = false, &block
  if include_standard
    ancestors.each{|a| block.call a unless a == self}
  else
    exclude = [self, Object, Kernel]
    ancestors.each do |a|
      block.call a unless exclude.include? a
    end
  end
end

#each_namespace(&block) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/RubyExt/module.rb', line 11

def each_namespace &block
  current = namespace
  while current do
    block.call current
    current = current.namespace
  end
end

#inherit(*modules) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/RubyExt/module.rb', line 87

def inherit *modules
  modules.each do |amodule|
    include amodule
    
    processed = []
    amodule.ancestors.each do |a|			
      if a.const_defined? :ClassMethods				
        class_methods = a.const_get :ClassMethods
        next if processed.include? class_methods
        processed << class_methods
        extend class_methods
      end
    end
  end
end

#inherited_instance_methodsObject



78
79
80
81
82
83
84
85
# File 'lib/RubyExt/module.rb', line 78

def inherited_instance_methods
	im = Set.new
	ancestors.each do |a| 
		next if a == self
		im.merge a.instance_methods
	end
	return im.to_a
end

#is?(base) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/RubyExt/module.rb', line 54

def is?(base)
  ancestors.include?(base)
end

#localization(lang) ⇒ Object



2
3
4
5
6
7
8
9
10
# File 'lib/RubyExt/Localization/Module.rb', line 2

def localization lang
  list, resource = [], "#{lang}.#{RubyExt::Localization::RESOURCE_EXTENSION}"
  self_ancestors_and_namespaces do |klass|
  	if RubyExt::Resource.resource_exist? klass, resource
		list << RubyExt::Resource.resource_get(klass, resource)
	end
  end
  return list.reverse.inject(:merge)
end

#namespaceObject



2
3
4
5
6
7
8
9
# File 'lib/RubyExt/module.rb', line 2

def namespace
  if @module_namespace_defined
    @module_namespace
  else
    @module_namespace_defined = true
    @module_namespace = Module.namespace_for name
  end
end

#resource_exist?(resource_name) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
62
63
# File 'lib/RubyExt/module.rb', line 58

def resource_exist? resource_name		
  self_ancestors_and_namespaces do |klass|
    return true if RubyExt::Resource.resource_exist? klass, resource_name					
  end
  return false		
end

#self_ancestors_and_namespaces(&b) ⇒ Object



30
31
32
33
34
# File 'lib/RubyExt/module.rb', line 30

def self_ancestors_and_namespaces &b
  b.call self
  each_ancestor &b
  each_namespace &b
end

#wrap_method(sym, prefix = "old_", &blk) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/RubyExt/module.rb', line 46

def wrap_method( sym, prefix = "old_", &blk )
  old_method = "#{prefix}_#{sym}".to_sym
  alias_method old_method, sym
  define_method(sym) do |*args|
    instance_exec(old_method, *args, &blk)
  end
end