class MetaInstance::Proxy
Public Class Methods
Instance is multiton. Use this method instead of new to get a cached instance.
# File lib/meta_instance/proxy.rb, line 40 def self.instance(delegate) @cache[delegate] ||= MetaInstance::Proxy.new(delegate) end
Initialize new Instance instance. If the delegate is a type of Module or Class then the instance will be extended with the {ModuleExtensions} mixin.
# File lib/meta_instance/proxy.rb, line 48 def initialize(delegate) @delegate = delegate extend ModuleExtensions if Module === delegate end
Public Instance Methods
Set an instance variable given a name and a value in an array pair.
Example
f = Friend.new f.instance << [:name, "John"] f.name #=> "John"
Returns the set value.
# File lib/meta_instance/proxy.rb, line 127 def <<(pair) name, value = *pair name = atize(name) set(name, value) end
The delegated object.
# File lib/meta_instance/proxy.rb, line 54 def delegate @delegate end
Iterate over instance variables.
# File lib/meta_instance/proxy.rb, line 59 def each variables.each do |name| yield(name[1..-1].to_sym, get(name)) end end
Instance evaluation.
# File lib/meta_instance/proxy.rb, line 189 def eval(*a,&b) delegate.instance_eval(*a, &b) end
Instance execution.
# File lib/meta_instance/proxy.rb, line 194 def exec(*a,&b) delegate.instance_exec(*a, &b) end
Get instance variable's value. Will return `nil` if the variable does not exist.
Returns the value of the instance variable.
# File lib/meta_instance/proxy.rb, line 103 def get(name) name = atize(name) delegate.instance_variable_get(name) end
Get object's instance id.
Returns [Integer]
# File lib/meta_instance/proxy.rb, line 251 def id delegate.object_id end
Instance vairable names as symbols.
Returns [Array<Symbols>].
# File lib/meta_instance/proxy.rb, line 171 def keys variables.collect do |name| name[1..-1].to_sym end end
Get method. Usage of this might seem strange because Ruby's own `instance_method` method is a misnomer. It should be something like `definition` or `method_definition`. In Ruby the acutal “instance” method is accessed via the unadorned `method` method.
Returns [Method].
# File lib/meta_instance/proxy.rb, line 204 def method(name) bind_call(:method, name) end
Returns list of method names.
Returns [Array<Symbol>].
# File lib/meta_instance/proxy.rb, line 211 def methods(*selection) list = [] if selection.empty? list.concat @delegate.methods end selection.each do |s| case s when :public, :all list.concat @delegate.public_methods when :protected, :all list.concat @delegate.protected_methods when :private, :all list.concat @bind_call.private_methods end end return list end
Is the object an instance of a given class?
Returns [Boolean]
# File lib/meta_instance/proxy.rb, line 235 def of?(a_class) delegate.instance_of?(a_class) end
Remove instance variable.
# File lib/meta_instance/proxy.rb, line 134 def remove(name) name = atize(name) delegate.remove_instance_variable(name) end
Set instance variable.
Returns the set value.
# File lib/meta_instance/proxy.rb, line 112 def set(name, value) name = atize(name) delegate.instance_variable_set(name, value) end
Number of instance variables.
# File lib/meta_instance/proxy.rb, line 66 def size variables.size end
Get instance variables with values as a hash.
Examples
class X def initialize(a,b) @a, @b = a, b end end x = X.new(1,2) x.instance.to_h #=> { :a=>1, :b=>2 }
Returns [Hash].
# File lib/meta_instance/proxy.rb, line 85 def to_h(at=false) h = {} if at variables.each do |name| h[name] = get(name) end else each do |key, value| h[key] = value end end h end
Set instance variables given a hash
.
instance.update('@a'=>1, '@b'=>2) @a #=> 1 @b #=> 2
Also, +@+ sign is not neccessary.
instance.update(:a=>1, :b=>2) @a #=> 1 @b #=> 2
Returns nothing.
# File lib/meta_instance/proxy.rb, line 152 def update(hash) hash.each do |pair| self << pair end end
Instance variable values.
Returns [Array<Object>].
# File lib/meta_instance/proxy.rb, line 182 def values variables.collect do |name| get(name) end end
Is an instaance variable defined?
Returns [Boolean]
# File lib/meta_instance/proxy.rb, line 243 def variable_defined?(name) name = atize(name) delegate.instance_variable_defined?(name) end
Same as instance_variables.
# File lib/meta_instance/proxy.rb, line 162 def variables delegate.instance_variables end
Private Instance Methods
# File lib/meta_instance/proxy.rb, line 260 def atize(name) name.to_s !~ /^@/ ? "@#{name}" : name end