Class: Dgrid::API::Workspace

Inherits:
Object
  • Object
show all
Includes:
Dgrid::ArgumentValidation, SetMembersFromHash
Defined in:
lib/dgrid/api/connection.rb,
lib/dgrid/api/workspace.rb

Overview

just a forward declaration

Defined Under Namespace

Classes: Backup, Backup_0_1, Backup_0_2, Cache

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SetMembersFromHash

#change_string_keys_to_symbol_keys, #set_members_from_hash, #split_hash

Methods included from Dgrid::ArgumentValidation

included

Constructor Details

#initialize(connection, name = 'DEFAULT WORKSPACE', description = "") ⇒ Workspace

Returns a new instance of Workspace.



12
13
14
15
16
17
18
# File 'lib/dgrid/api/workspace.rb', line 12

def initialize(connection, name='DEFAULT WORKSPACE', description = "")
  @id = nil
  @connection = connection
  @name = name
  @item_ids = {} # lens_id => [item_id]
  @entity_cache = Cache.new
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



7
8
9
# File 'lib/dgrid/api/workspace.rb', line 7

def connection
  @connection
end

#descriptionObject

Returns the value of attribute description.



7
8
9
# File 'lib/dgrid/api/workspace.rb', line 7

def description
  @description
end

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/dgrid/api/workspace.rb', line 7

def id
  @id
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/dgrid/api/workspace.rb', line 7

def name
  @name
end

Class Method Details

.pluralizedObject



20
21
22
# File 'lib/dgrid/api/workspace.rb', line 20

def self.pluralized
  'workspaces'
end

.singularObject



24
25
26
# File 'lib/dgrid/api/workspace.rb', line 24

def self.singular
  name.split('::').last.downcase
end

Instance Method Details

#add_entity(entity) ⇒ Object



159
160
161
162
163
164
165
166
167
168
# File 'lib/dgrid/api/workspace.rb', line 159

def add_entity(entity)
  if entity.new_record?
    entity = connection.create_entity(entity,self.id)
  end
  if !entity.in_workspace?(self)
    entity.add_workspace(self)
  end
  @entity_cache.invalidate(entity.class)
  entity
end

#add_incident(incident, item = nil) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/dgrid/api/workspace.rb', line 54

def add_incident(incident, item = nil)
  if item
    raise "Cannot add an Incident in a Workspace unless owning Item is already in the Workspace" unless item.in_workspace?(self)
    raise "Cannot add an Incident to an unsaved Item" if item.new_record?
  end
  if incident.new_record?
    incident = connection.create_entity(incident, self.id)
  end
  if !incident.in_workspace?(self)
    incident.add_workspace(self)
  end
  if item
    connection.subordinate_entity_to_other_entity_in_workspace(incident, item, self.id)
  end
  incident
end

#add_item(item) ⇒ Object



48
49
50
# File 'lib/dgrid/api/workspace.rb', line 48

def add_item(item)
  add_entity(item)
end

#add_keyword(keyword) ⇒ Object



72
73
74
# File 'lib/dgrid/api/workspace.rb', line 72

def add_keyword(keyword)
  add_entity(keyword)
end

#add_organization(organization) ⇒ Object



43
44
45
# File 'lib/dgrid/api/workspace.rb', line 43

def add_organization(organization)
  add_entity(organization)
end

#add_person(person) ⇒ Object



33
34
35
# File 'lib/dgrid/api/workspace.rb', line 33

def add_person(person)
  add_entity(person)
end

#add_place(place) ⇒ Object



38
39
40
# File 'lib/dgrid/api/workspace.rb', line 38

def add_place(place)
  add_entity(place)
end

#backupObject

of class Backup_0_2



528
529
530
# File 'lib/dgrid/api/workspace.rb', line 528

def backup()
  backup = Backup.create(self)
end

#clone_members_into(other_workspace) ⇒ Object



533
534
535
# File 'lib/dgrid/api/workspace.rb', line 533

def clone_members_into(other_workspace)
  backup().restore_into(other_workspace)
end

#create_item(options = {}) {|item| ... } ⇒ Object

Yields:

  • (item)


100
101
102
103
104
# File 'lib/dgrid/api/workspace.rb', line 100

def create_item(options = {}, &block)
  item = add_item(Dgrid::API::Item.new(options))
  yield item if block_given?
  item
end

#create_organization(options = {}) {|organization| ... } ⇒ Object

Yields:

  • (organization)


94
95
96
97
98
# File 'lib/dgrid/api/workspace.rb', line 94

def create_organization(options = {}, &block)
  organization = add_organization(Dgrid::API::Organization.new(options))
  yield organization if block_given?
  organization
end

#create_person(options = {}) ⇒ Object

TODO auto-generate this templatesque bullshit



84
85
86
87
# File 'lib/dgrid/api/workspace.rb', line 84

def create_person(options = {})
  person = add_person(Dgrid::API::Person.new(options))
  person
end

#create_place(options = {}) ⇒ Object



89
90
91
92
# File 'lib/dgrid/api/workspace.rb', line 89

def create_place(options = {})
  place = add_place(Dgrid::API::Place.new(options))
  place
end

#get_entities(klass) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/dgrid/api/workspace.rb', line 170

def get_entities(klass)
  pluralized_name = klass.pluralized
  return @entity_cache.get(klass) if @entity_cache.include?(klass)
  construction_fields = klass.db_fields
  entities_params = connection.get_in_workspace(self.id, pluralized_name)
  entities = []
  entities_params.each do |entity_params|
    construction_params, other_params = split_hash(entity_params, construction_fields)
    entity = klass.new(change_string_keys_to_symbol_keys(construction_params))
    entity.add_workspace(self)
    entities <<  entity
  end
  @entity_cache.store(klass,entities)
end

#incident_ids_in_item(item) ⇒ Object



155
156
157
# File 'lib/dgrid/api/workspace.rb', line 155

def incident_ids_in_item(item)
  incident_ids = connection.get_incidents_in_item(self.id, item.id)
end

#incidentsObject



141
142
143
# File 'lib/dgrid/api/workspace.rb', line 141

def incidents
  get_entities(Incident)
end

#invalidate_cache(klass) ⇒ Object



185
186
187
# File 'lib/dgrid/api/workspace.rb', line 185

def invalidate_cache(klass)
  @entity_cache.invalidate(klass)
end

#item_ids_in_lens(lens) ⇒ Object



150
151
152
# File 'lib/dgrid/api/workspace.rb', line 150

def item_ids_in_lens(lens)
  item_ids = connection.get_items_in_lens(self.id, lens.id)
end

#itemsObject



119
120
121
# File 'lib/dgrid/api/workspace.rb', line 119

def items
    get_entities(Item)
end

#keywordsObject



145
146
147
# File 'lib/dgrid/api/workspace.rb', line 145

def keywords
  get_entities(Keyword)
end

#lensesObject



137
138
139
# File 'lib/dgrid/api/workspace.rb', line 137

def lenses
  get_entities(Lens)
end


109
110
111
112
113
114
# File 'lib/dgrid/api/workspace.rb', line 109

def link(left_entity, right_entity, options = {})
  raise "Entity #{left_entity.to_s} is not in this workspace" unless left_entity.in_workspace?(self)
  raise "Entity #{right_entity.to_s} is not in this workspace" unless right_entity.in_workspace?(self)
  connection.create_link(left_entity, right_entity, self.id, options)
  @entity_cache.invalidate(Link)
end


133
134
135
# File 'lib/dgrid/api/workspace.rb', line 133

def links
  get_entities(Link)
end

#organizationsObject



129
130
131
# File 'lib/dgrid/api/workspace.rb', line 129

def organizations
    get_entities(Organization)
end

#peopleObject



122
123
124
# File 'lib/dgrid/api/workspace.rb', line 122

def people
    get_entities(Person)
end

#placesObject



125
126
127
# File 'lib/dgrid/api/workspace.rb', line 125

def places
    get_entities(Place)
end

#subordinate_entity_to_other_entity(entity, other) ⇒ Object



76
77
78
79
# File 'lib/dgrid/api/workspace.rb', line 76

def subordinate_entity_to_other_entity(entity, other)
  connection.subordinate_entity_to_other_entity_in_workspace(entity, other, self.id)
  @entity_cache.invalidate(Link)
end

#typeObject



28
29
30
# File 'lib/dgrid/api/workspace.rb', line 28

def type
  self.class.name.split('::').last
end