Class: MetaInstance::Proxy

Inherits:
Object
  • Object
show all
Includes:
Enumerable, FreezeMethod, InstanceMethodDefine, ModuleExtensions
Defined in:
lib/meta_instance/proxy.rb

Constant Summary

Constants included from InstanceMethodDefine

InstanceMethodDefine::METHOD_BACKUP_KEY

Constants included from FreezeMethod

FreezeMethod::METHODS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from InstanceMethodDefine

#backup_method, #define_method, #instance_override, #restore_method

Methods included from ModuleExtensions

#method_definition, #method_definitions

Constructor Details

#initialize(delegate) ⇒ Proxy

Initialize new Instance instance. If the delegate is a type of Module or Class then the instance will be extended with the ModuleExtensions mixin.



48
49
50
51
# File 'lib/meta_instance/proxy.rb', line 48

def initialize(delegate)
  @delegate = delegate
  extend ModuleExtensions if Module === delegate
end

Class Method Details

.instance(delegate) ⇒ Object

Instance is multiton. Use this method instead of #new to get a cached instance.



40
41
42
# File 'lib/meta_instance/proxy.rb', line 40

def self.instance(delegate)
  @cache[delegate] ||= MetaInstance::Proxy.new(delegate)
end

Instance Method Details

#<<(pair) ⇒ Object

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.



127
128
129
130
131
# File 'lib/meta_instance/proxy.rb', line 127

def <<(pair)
  name, value = *pair
  name = atize(name)
  set(name, value)
end

#delegateObject

The delegated object.



54
55
56
# File 'lib/meta_instance/proxy.rb', line 54

def delegate
  @delegate
end

#eachObject

Iterate over instance variables.



59
60
61
62
63
# 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) ⇒ Object

Instance evaluation.



189
190
191
# File 'lib/meta_instance/proxy.rb', line 189

def eval(*a,&b)
  delegate.instance_eval(*a, &b)
end

#exec(*a, &b) ⇒ Object

Instance execution.



194
195
196
# File 'lib/meta_instance/proxy.rb', line 194

def exec(*a,&b)
  delegate.instance_exec(*a, &b)
end

#get(name) ⇒ Object Also known as: []

Get instance variable’s value. Will return ‘nil` if the variable does not exist.

Returns the value of the instance variable.



103
104
105
106
# File 'lib/meta_instance/proxy.rb', line 103

def get(name)
  name = atize(name)
  delegate.instance_variable_get(name)
end

#idObject

Get object’s instance id.

Returns [Integer]



251
252
253
# File 'lib/meta_instance/proxy.rb', line 251

def id
  delegate.object_id
end

#keysObject Also known as: names

Instance vairable names as symbols.

Returns [Array<Symbols>].



171
172
173
174
175
# File 'lib/meta_instance/proxy.rb', line 171

def keys
  variables.collect do |name|
    name[1..-1].to_sym
  end
end

#method(name) ⇒ Object

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].



204
205
206
# File 'lib/meta_instance/proxy.rb', line 204

def method(name)
  bind_call(:method, name)
end

#methods(*selection) ⇒ Object

Returns list of method names.

Returns [Array<Symbol>].



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# 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

#of?(a_class) ⇒ Boolean

Is the object an instance of a given class?

Returns [Boolean]

Returns:

  • (Boolean)


235
236
237
# File 'lib/meta_instance/proxy.rb', line 235

def of?(a_class)
  delegate.instance_of?(a_class)
end

#remove(name) ⇒ Object

Remove instance variable.



134
135
136
137
# File 'lib/meta_instance/proxy.rb', line 134

def remove(name)
  name = atize(name)
  delegate.remove_instance_variable(name)
end

#set(name, value) ⇒ Object Also known as: []=

Set instance variable.

Returns the set value.



112
113
114
115
# File 'lib/meta_instance/proxy.rb', line 112

def set(name, value)
  name = atize(name)
  delegate.instance_variable_set(name, value)
end

#sizeObject

Number of instance variables.



66
67
68
# File 'lib/meta_instance/proxy.rb', line 66

def size
  variables.size
end

#to_h(at = false) ⇒ Object

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].



85
86
87
88
89
90
91
92
93
94
95
96
97
# 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) ⇒ Object Also known as: assign

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.



152
153
154
155
156
# File 'lib/meta_instance/proxy.rb', line 152

def update(hash)
  hash.each do |pair|
    self << pair
  end
end

#valuesObject

Instance variable values.

Returns [Array<Object>].



182
183
184
185
186
# File 'lib/meta_instance/proxy.rb', line 182

def values
  variables.collect do |name|
    get(name)
  end
end

#variable_defined?(name) ⇒ Boolean

Is an instaance variable defined?

Returns [Boolean]

Returns:

  • (Boolean)


243
244
245
246
# File 'lib/meta_instance/proxy.rb', line 243

def variable_defined?(name)
  name = atize(name)
  delegate.instance_variable_defined?(name)
end

#variablesObject

Same as #instance_variables.



162
163
164
# File 'lib/meta_instance/proxy.rb', line 162

def variables
  delegate.instance_variables
end