class MetaInstance::Proxy

Public Class Methods

instance(delegate) click to toggle source

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
new(delegate) click to toggle source

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

<<(pair) click to toggle source

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
[](name)
Alias for: get
[]=(name, value)
Alias for: set
assign(hash)

A hold-over from the the old instance_assign method.

Alias for: update
delegate() click to toggle source

The delegated object.

# File lib/meta_instance/proxy.rb, line 54
def delegate
  @delegate
end
each() { |to_sym, get(name)| ... } click to toggle source

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
eval(*a,&b) click to toggle source

Instance evaluation.

# File lib/meta_instance/proxy.rb, line 189
def eval(*a,&b)
  delegate.instance_eval(*a, &b)
end
exec(*a,&b) click to toggle source

Instance execution.

# File lib/meta_instance/proxy.rb, line 194
def exec(*a,&b)
  delegate.instance_exec(*a, &b)
end
get(name) click to toggle source

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
Also aliased as: []
id() click to toggle source

Get object's instance id.

Returns [Integer]

# File lib/meta_instance/proxy.rb, line 251
def id
  delegate.object_id
end
keys() click to toggle source

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
Also aliased as: names
method(name) click to toggle source

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
methods(*selection) click to toggle source

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
names()
Alias for: keys
of?(a_class) click to toggle source

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(name) click to toggle source

Remove instance variable.

# File lib/meta_instance/proxy.rb, line 134
def remove(name)
  name = atize(name)
  delegate.remove_instance_variable(name)
end
set(name, value) click to toggle source

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
Also aliased as: []=
size() click to toggle source

Number of instance variables.

# File lib/meta_instance/proxy.rb, line 66
def size
  variables.size
end
to_h(at=false) click to toggle source

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
update(hash) click to toggle source

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
Also aliased as: assign
values() click to toggle source

Instance variable values.

Returns [Array<Object>].

# File lib/meta_instance/proxy.rb, line 182
def values
  variables.collect do |name|
    get(name)
  end
end
variable_defined?(name) click to toggle source

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
variables() click to toggle source

Same as instance_variables.

# File lib/meta_instance/proxy.rb, line 162
def variables
  delegate.instance_variables
end

Private Instance Methods

atize(name) click to toggle source
# File lib/meta_instance/proxy.rb, line 260
def atize(name)
  name.to_s !~ /^@/ ? "@#{name}" : name
end