Class: Dgrid::API::Item

Inherits:
NamedEntity show all
Includes:
Dgrid::ArgumentValidation
Defined in:
lib/dgrid/api/item.rb

Instance Attribute Summary collapse

Attributes inherited from NamedEntity

#name

Attributes inherited from Entity

#description, #id, #workspaces

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Dgrid::ArgumentValidation

included

Methods inherited from Entity

#add_entity, #add_workspace, #attach, #connection, #first_workspace, #in_workspace?, #link_to, #new_record?, singular, #type, type

Methods included from SetMembersFromHash

#change_string_keys_to_symbol_keys, #set_members_from_hash, #split_hash

Constructor Details

#initialize(options) ⇒ Item

Returns a new instance of Item.



10
11
12
13
14
15
16
# File 'lib/dgrid/api/item.rb', line 10

def initialize(options)
  parent_opts, my_opts = split_hash(options,[:name, :description])
  super(parent_opts)
  set_members_from_hash(my_opts)
  @body ||= ''
  @incidents = nil
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



5
6
7
# File 'lib/dgrid/api/item.rb', line 5

def body
  @body
end

Class Method Details

.db_fieldsObject



18
19
20
# File 'lib/dgrid/api/item.rb', line 18

def self.db_fields
  %w(id name body source)
end

.pluralizedObject



22
23
24
# File 'lib/dgrid/api/item.rb', line 22

def self.pluralized
  'items'
end

Instance Method Details

#create_incident(options = {}) {|incident| ... } ⇒ Object

Yields:

  • (incident)


48
49
50
51
52
53
54
55
56
57
# File 'lib/dgrid/api/item.rb', line 48

def create_incident(options = {}, &block)
  incident = Dgrid::API::Incident.new(options)
  workspaces.each do |workspace_id, workspace| 
    returned_incident = workspace.add_incident(incident)
    incident = returned_incident if incident.new_record?
    workspace.subordinate_entity_to_other_entity(incident,self)
  end
  yield incident if block_given?
  incident
end

#incident_idsObject



36
37
38
# File 'lib/dgrid/api/item.rb', line 36

def incident_ids
  workspace.incident_ids_in_item(self)
end

#incidentsObject



40
41
42
43
44
45
# File 'lib/dgrid/api/item.rb', line 40

def incidents
  incident_list = workspace.incidents
  incident_hash = {}
  incident_list.each { |incident| incident_hash[incident.id] = incident}
  return incident_ids.map { |id| incident_hash[id]}
end

#to_hashObject



26
27
28
29
30
# File 'lib/dgrid/api/item.rb', line 26

def to_hash
  { :name => name,
    :body => body
  }
end

#workspaceObject



32
33
34
# File 'lib/dgrid/api/item.rb', line 32

def workspace
  workspace = workspaces.values.first # FIXME: assumes one workspace per item
end