Class: Instance

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/instance.rb

Overview

Instance class is a delgator for any object which provides an elegant and protected interface to an object’s state, i.e. its instance.

Examples

class Friend
  attr_accessor :name, :age, :phone
  def initialize(name, age, phone)
    @name, @age, @phone = name, age, phone
  end
end

f1 = Friend.new("John", 30, "555-1212")
f1.instance

f1.instance.update({:name=>'Jerry'})
f1.instance

TODO: Should we add ‘is_a?` and `kind_of?` too?

Defined Under Namespace

Modules: ModuleExtensions

Constant Summary collapse

METHODS =

Store Object methods so they cannot be overriden by the delegate class.

{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(delegate) ⇒ Instance

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



71
72
73
74
# File 'lib/instance.rb', line 71

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

Class Method Details

.freeze_method(name) ⇒ Object



28
29
30
# File 'lib/instance.rb', line 28

def self.freeze_method(name)
  METHODS[name.to_sym] = Object.instance_method(name)
end

.instance(delegate) ⇒ Object

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



63
64
65
# File 'lib/instance.rb', line 63

def self.instance(delegate)
  @cache[delegate] ||= Instance.new(delegate)
end

Instance Method Details

#<<(pair) ⇒ Object



142
143
144
145
146
# File 'lib/instance.rb', line 142

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

#classObject

Get object’s instance id.

Returns [Class]



284
285
286
287
# File 'lib/instance.rb', line 284

def class
  #@delegate.variable_defined?(name)
  METHODS[:class].bind(@delegate).call
end

#delegateObject

The delegated object.



77
78
79
# File 'lib/instance.rb', line 77

def delegate
  @delegate
end

#eachObject

Iterate over instance variables.



82
83
84
85
86
# File 'lib/instance.rb', line 82

def each
  variables.each do |name|
    yield(name[1..-1].to_sym, get(name))
  end
end

#eval(*a, &b) ⇒ Object

Instance evaluation.



209
210
211
212
# File 'lib/instance.rb', line 209

def eval(*a,&b)
  #@delegate.instance_eval(*a,&b)
  METHODS[:instance_eval].bind(@delegate).call(*a,&b)
end

#exec(*a, &b) ⇒ Object

Instance execution.



215
216
217
218
# File 'lib/instance.rb', line 215

def exec(*a,&b)
  #@delegate.instance_exec(*a,&b)
  METHODS[:instance_exec].bind(@delegate).call(*a,&b)
end

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



126
127
128
129
130
# File 'lib/instance.rb', line 126

def get(name)
  name = atize(name)
  #@delegate.instance_variable_get(name)
  METHODS[:instance_variable_get].bind(@delegate).call(name)
end

#idObject

Get object’s instance id.

Returns [Integer]



273
274
275
276
# File 'lib/instance.rb', line 273

def id
  #@delegate.variable_defined?(name)
  METHODS[:object_id].bind(@delegate).call
end

#keysObject

Instance vairable names as symbols.

Returns [Array<Symbols>].



184
185
186
187
188
# File 'lib/instance.rb', line 184

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



226
227
228
229
# File 'lib/instance.rb', line 226

def method(name)
  #@delegate.instance_exec(*a,&b)
  METHODS[:method].bind(@delegate).call(name)
end

#methods(*selection) ⇒ Object

Returns list of method names.

Returns [Array<Symbol>].



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/instance.rb', line 234

def methods(*selection)
  list = []

  if selection.empty?
    list.concat METHODS[:methods].bind(@delegate).call
  end

  selection.each do |s|
    case s
    when :public, :all
      list.concat METHODS[:public_methods].bind(@delegate).call
    when :protected, :all
      list.concat METHODS[:protected_methods].bind(@delegate).call
    when :private, :all
      list.concat METHODS[:private_methods].bind(@delegate).call
    end
  end

  return list
end

#namesObject

Instance variable names as strings.

Returns [Array<String>].



193
194
195
196
197
# File 'lib/instance.rb', line 193

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

#object_classObject

Fallback to get the real class of the Instance delegate itself.



279
# File 'lib/instance.rb', line 279

alias :object_class :class

#of?(a_class) ⇒ Boolean

Is the object an instance of a given class?

Returns [Boolean]

Returns:

  • (Boolean)


258
259
260
261
# File 'lib/instance.rb', line 258

def of?(a_class)
  #@delegate.instance_of?(aclass)
  METHODS[:instance_of?].bind(@delegate).call(a_class)
end

#remove(name) ⇒ Object

Remove instance variable.



149
150
151
152
# File 'lib/instance.rb', line 149

def remove(name)
  name = atize(name)
  METHODS[:remove_instance_variable].bind(@delegate).call(name)
end

#send(*a, &b) ⇒ Object

Send message to instance.



290
291
292
# File 'lib/instance.rb', line 290

def send(*a, &b)
  METHODS[:send].bind(@delegate).call(*a, &b)
end

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



134
135
136
137
138
# File 'lib/instance.rb', line 134

def set(name, value)
  name = atize(name)
  #@delegate.instance_variable_set(name,value)
  METHODS[:instance_variable_set].bind(@delegate).call(name,value)
end

#sizeObject

Number of instance variables.



89
90
91
# File 'lib/instance.rb', line 89

def size
  variables.size
end

#to_h(at = false) ⇒ Object Also known as: to_hash

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



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/instance.rb', line 108

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


166
167
168
169
170
# File 'lib/instance.rb', line 166

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

#valuesObject

Instance variable values.

Returns [Array<Object>].



202
203
204
205
206
# File 'lib/instance.rb', line 202

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

#variable_defined?(name) ⇒ Boolean

Returns:

  • (Boolean)


264
265
266
267
268
# File 'lib/instance.rb', line 264

def variable_defined?(name)
  name = atize(name)
  #@delegate.variable_defined?(name)
  METHODS[:instance_variable_defined?].bind(@delegate).call(name)
end

#variablesObject

Same as #instance_variables.



176
177
178
179
# File 'lib/instance.rb', line 176

def variables
  #@delegate.instance_variables
  METHODS[:instance_variables].bind(@delegate).call
end