Class: ObjectCopy

Inherits:
Object
  • Object
show all
Defined in:
lib/translate/copy/object_copy.rb

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ ObjectCopy

Returns a new instance of ObjectCopy.



4
5
6
# File 'lib/translate/copy/object_copy.rb', line 4

def initialize(object)
  @object = object
end

Instance Method Details

#copy(new_workspace, additional_values = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/translate/copy/object_copy.rb', line 8

def copy(new_workspace, additional_values = {})
  values = shallow_copy(@object, new_workspace)
  values.merge! additional_values

  values[:workspace] = new_workspace
  values[:project] = find_project_in_new_workspace(new_workspace)
  
  @new_object = create_object(new_workspace.rally_rest, values)
  remember @object, @new_object
  copy_children(new_workspace)

  @new_object
end

#copy_children(new_workspace) ⇒ Object



71
72
# File 'lib/translate/copy/object_copy.rb', line 71

def copy_children(new_workspace)
end

#create_object(rally_rest, values) ⇒ Object



27
28
29
# File 'lib/translate/copy/object_copy.rb', line 27

def create_object(rally_rest, values)
  rally_rest.create(@object.type_as_symbol, values)
end

#excluded_attributes(object) ⇒ Object



67
68
69
# File 'lib/translate/copy/object_copy.rb', line 67

def excluded_attributes(object)
  object.typedef.reference_attributes(true).keys
end

#fetch_object(type, oid, workspace) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/translate/copy/object_copy.rb', line 88

def fetch_object(type, oid, workspace)
  @@cached_objects ||= {}
  new_oid = $TRANSLATED[oid]
  return @@cached_objects[new_oid] if @@cached_objects.key? oid
  
  object = @object.rally_rest.find(type, :workspace => workspace) { equal :object_i_d, new_oid }.first
  @@cached_objects[new_oid] = object
end

#fetch_project(project_name, workspace) ⇒ Object



41
42
43
44
45
46
# File 'lib/translate/copy/object_copy.rb', line 41

def fetch_project(project_name, workspace)
  @@cached_projects ||= {}
  return @@cached_projects[project_name] if @@cached_projects.key? project_name
  project = workspace.rally_rest.find(:project, :workspace => workspace) { equal :name, project_name }.first
  @@cached_projects[project_name] = project
end

#find_project_in_new_workspace(new_workspace) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/translate/copy/object_copy.rb', line 31

def find_project_in_new_workspace(new_workspace)
  # I'm assuming (after talking to nate) that if the project is nil, there there is more then one project in the 
  # workspace. At least for single project workspaces that were migrated for project scoping, this is true.
  if @object.project.nil?
    return nil
  else
    fetch_project(@object.project.name, new_workspace)
  end
end

#remember(old, new) ⇒ Object



22
23
24
25
# File 'lib/translate/copy/object_copy.rb', line 22

def remember old, new
  $TRANSLATED ||= {}
  $TRANSLATED[old.oid] = new.oid
end

#shallow_copy(object, new_workspace) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/translate/copy/object_copy.rb', line 48

def shallow_copy(object, new_workspace)
  keys_to_exclude = excluded_attributes(object)
  values = object.elements.reject { |key, value| keys_to_exclude.include? key }
  values.delete(:owner) unless user_exists? @object.owner

  # Here we need to fix any deleted custom dropdown values that have been deleted
  values.each do |attribute, value|
    if @object.typedef.custom_dropdown_attributes.key? attribute
	attrdef = @object.typedef.custom_dropdown_attributes[attribute]
	unless attrdef.allowed_values.include? value
 values[attribute] = nil 
 puts "Deleteing #{attribute} = #{value} from @{object.name} because it no longer exists"
	end
    end
  end

  values
end

#user_exists?(username) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/translate/copy/object_copy.rb', line 74

def user_exists?(username)
  @@cached_users ||= {}

  return true if username.nil?
  return @@cached_users[username] if @@cached_users.key? username

  result = @object.rally_rest.find(:user) { equal :login_name, username }
  if result.total_result_count > 0
    @@cached_users[username] = true
  else
    @@cached_users[username] = false
  end
end