Class: Duplicator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(excluded_objects = [], options = {}) ⇒ Duplicator

Create an instance of Duplicator to track duplicated objects.

Options are used to store information that persists across duplication of objects.


11
12
13
14
15
16
17
# File 'lib/autoload/duplicator.rb', line 11

def initialize(excluded_objects = [], options = {})
  @duplicated_objects = {} # hash to check what has been duplicated
  @exclusion_set = excluded_objects.to_set # set to check what should be excluded
  @options = options
  @time_shift_amount = options[:time_shift]
  @mode = options[:mode]
end

Instance Attribute Details

#modeObject (readonly)

Returns the value of attribute mode


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

def mode
  @mode
end

#optionsObject (readonly)

Returns the value of attribute options


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

def options
  @options
end

Instance Method Details

#duplicate(array_of_stuff) ⇒ Object #duplicate(something) ⇒ Object

Deep copies the given item(s) and initializes the duplicates by calling initialize_duplicate on the duplicates.

initialize_duplicate may further trigger duplication of the source item's children. If a collection is given, some of the items to be duplicated might be associated. initialize_duplicate may be used to associate the duplicates of associated items.

Since the duplicator does not have any knowledge of what these items are, expect for the fact that they respond to initialize_duplicate, the duplicator does not impose an order on which items are duplicated first. To simplify the process of associating duplicated objects, we give the responsibility of forming the association to the object that is duplicated later.

E.g. Suppose that Group has many Students and the instance student_a belongs to the instance 'group_a'. If group_a is duplicated first, then calling duplicate_student_a.initialize_duplicate should add duplicate_student_a to duplicate_group_a's list of students. If student_a is duplicated first, then vice versa. Thus, the code to form the association can be found in both Student#initialize_duplicate and Group#initialize_duplicate.


44
45
46
# File 'lib/autoload/duplicator.rb', line 44

def duplicate(stuff)
  map_item_or_collection(stuff) { |item| duplicate_object(item) }
end

#duplicated?(source_object) ⇒ Boolean

Checks if an item has been duplicated.


66
67
68
# File 'lib/autoload/duplicator.rb', line 66

def duplicated?(source_object)
  @duplicated_objects.key?(source_object)
end

#set_option(key, value) ⇒ Object


70
71
72
# File 'lib/autoload/duplicator.rb', line 70

def set_option(key, value)
  @options[key] = value
end

#time_shift(original_time) ⇒ DateTime

Time shifts the datetime passed to this function. Cap the maximum datetime to 31 December 9999 0:00:00 as the frontend JS cannot support 5 digit years.


54
55
56
57
58
59
60
# File 'lib/autoload/duplicator.rb', line 54

def time_shift(original_time)
  # TODO: Move max_time declaration to facilitate re-use.
  # config/application.rb could be unsuitable as `Time.zone.local` does not work there.
  max_time = Time.zone.local(9999, 12, 31, 0, 0, 0)
  shifted_time = original_time + @time_shift_amount
  shifted_time < max_time ? shifted_time : max_time
end