Class: Perpetuity::Duplicator

Inherits:
Object
  • Object
show all
Defined in:
lib/perpetuity/duplicator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ Duplicator

Returns a new instance of Duplicator.



4
5
6
7
8
9
10
11
12
13
# File 'lib/perpetuity/duplicator.rb', line 4

def initialize object
  if object.is_a? Array
    @object = object.map { |i| Duplicator.new(i).object }
  else
    @object = object.dup rescue object
  end
  @object.instance_variables.each do |ivar|
    duplicate_attribute ivar
  end
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



3
4
5
# File 'lib/perpetuity/duplicator.rb', line 3

def object
  @object
end

Instance Method Details

#attribute(ivar) ⇒ Object



15
16
17
# File 'lib/perpetuity/duplicator.rb', line 15

def attribute ivar
  object.instance_variable_get ivar
end

#duplicate_attribute(ivar) ⇒ Object



23
24
25
# File 'lib/perpetuity/duplicator.rb', line 23

def duplicate_attribute ivar
  set_attribute ivar, Duplicator.new(attribute(ivar)).object
end

#set_attribute(ivar, value) ⇒ Object



19
20
21
# File 'lib/perpetuity/duplicator.rb', line 19

def set_attribute ivar, value
  object.instance_variable_set ivar, value
end