Module: Archivist::ArchiveMethods
- Defined in:
- lib/archivist/archive.rb
Defined Under Namespace
Modules: ArchiveClassMethods
Class Method Summary
collapse
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
10
11
12
13
14
15
16
17
|
# File 'lib/archivist/archive.rb', line 10
def method_missing(method,*args,&block)
if get_klass_instance_methods.include?(method)
build_proxy_method(method.to_s)
self.method(method).call(*args,&block)
else
super(method,*args,&block)
end
end
|
Class Method Details
.included(base) ⇒ Object
3
4
5
6
7
8
|
# File 'lib/archivist/archive.rb', line 3
def self.included(base)
base.class_eval do
extend ArchiveClassMethods
protected :get_klass,:get_klass_name,:get_klass_instance_methods,:build_proxy_method
end
end
|
Instance Method Details
#build_proxy_method(method_name) ⇒ Object
45
46
47
48
49
50
51
52
|
# File 'lib/archivist/archive.rb', line 45
def build_proxy_method(method_name)
class_eval <<-EOF
def #{method_name}(*args,&block)
instance = #{get_klass_name}.new(self.attributes.reject{|k,v| !#{get_klass.new.attribute_names.inspect}.include?(k.to_s)})
instance.#{method_name}(*args,&block)
end
EOF
end
|
#get_klass ⇒ Object
27
28
29
|
# File 'lib/archivist/archive.rb', line 27
def get_klass
@klass ||= get_klass_name.constantize
end
|
#get_klass_instance_methods ⇒ Object
36
37
38
|
# File 'lib/archivist/archive.rb', line 36
def get_klass_instance_methods
@klass_instance_methods ||= get_klass.instance_methods(false)
end
|
#get_klass_name ⇒ Object
31
32
33
|
# File 'lib/archivist/archive.rb', line 31
def get_klass_name
@klass_name ||= ([ '' ] + self.class.name.split('::')[0..-2]) * '::'
end
|
#respond_to?(method, include_private = false) ⇒ Boolean
19
20
21
22
23
24
25
|
# File 'lib/archivist/archive.rb', line 19
def respond_to?(method,include_private=false)
if get_klass_instance_methods.include?(method)
return true
else
super(method,include_private)
end
end
|