Class: Lolita::LazyLoader
Class Method Summary collapse
-
.lazy_load(instance_name, var_name, lazy_class, *args, &block) ⇒ Object
attr_reader :lazy_class,:eval_block,:class_instance.
Instance Method Summary collapse
- #class_instance ⇒ Object
-
#initialize(lazy_class, *args, &block) ⇒ LazyLoader
constructor
A new instance of LazyLoader.
- #method_missing(method_name, *args, &block) ⇒ Object
Constructor Details
#initialize(lazy_class, *args, &block) ⇒ LazyLoader
Returns a new instance of LazyLoader.
18 19 20 21 22 |
# File 'lib/lolita/lazy_loader.rb', line 18 def initialize(lazy_class,*args,&block) @args=args @lazy_class=lazy_class @eval_block=block end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/lolita/lazy_loader.rb', line 28 def method_missing(method_name,*args,&block) unless @class_instance arity=@lazy_class.instance_method(:initialize).arity if arity==-1 # when expectign *args @class_instance=@lazy_class.new(*@args,&@eval_block) elsif arity.abs>0 # when expecting specific number of arguments without any *args @class_instance=@lazy_class.new(*@args.slice(0..arity.abs-1),&@eval_block) else @class_instance=@lazy_class.new(&@eval_block) end end @class_instance.__send__(method_name,*args,&block) end |
Class Method Details
.lazy_load(instance_name, var_name, lazy_class, *args, &block) ⇒ Object
attr_reader :lazy_class,:eval_block,:class_instance
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/lolita/lazy_loader.rb', line 6 def self.lazy_load(instance_name,var_name,lazy_class,*args,&block) temp_var=instance_name.instance_variable_get(var_name) unless temp_var temp_var=instance_name.instance_variable_set(var_name,self.new(lazy_class,*args,&block)) end if temp_var.to_s=~/Lolita::LazyLoader/ && temp_var.class_instance instance_name.instance_variable_set(var_name,temp_var.class_instance) else temp_var end end |
Instance Method Details
#class_instance ⇒ Object
24 25 26 |
# File 'lib/lolita/lazy_loader.rb', line 24 def class_instance @class_instance || self end |