Class: Dgrid::API::Entity

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

Direct Known Subclasses

Link, NamedEntity

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(options) ⇒ Entity

Returns a new instance of Entity.



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

def initialize(options)
  @id = nil
  set_members_from_hash(options)

  # FIXME Remove this when ArgumentValidation::option is implemented
  @description ||= ''

  @workspaces = {}
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



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

def description
  @description
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#workspacesObject

Returns the value of attribute workspaces.



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

def workspaces
  @workspaces
end

Class Method Details

.pluralizedObject



58
59
60
# File 'lib/dgrid/api/entity.rb', line 58

def self.pluralized
  'entities'
end

.singularObject



29
30
31
# File 'lib/dgrid/api/entity.rb', line 29

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

.typeObject



21
22
23
# File 'lib/dgrid/api/entity.rb', line 21

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

Instance Method Details

#add_entity(entity) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/dgrid/api/entity.rb', line 62

def add_entity(entity)
  workspaces.each do |workspace_id,workspace|
    # STDERR.puts "adding #{entity.type} #{entity} to #{self.type} #{self} in workspace #{workspace}"
    if entity.in_workspace?(workspace)
      workspace.subordinate_entity_to_other_entity(entity, self)
    end
  end
end

#add_workspace(workspace) ⇒ Object



45
46
47
48
# File 'lib/dgrid/api/entity.rb', line 45

def add_workspace(workspace)
  @workspaces[workspace.id] = workspace
  self
end

#attach(filename) ⇒ Object



71
72
73
74
75
# File 'lib/dgrid/api/entity.rb', line 71

def attach(filename)
  conn = connection
  # TODO Adapt this to a workspace-independent model when available in the server
  conn.attach_file_to_entity_in_workspace(self,filename,first_workspace.id)
end

#connectionObject



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

def connection
  first_workspace.connection
end

#first_workspaceObject



81
82
83
# File 'lib/dgrid/api/entity.rb', line 81

def first_workspace
  workspaces.values.first
end

#in_workspace?(workspace) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/dgrid/api/entity.rb', line 41

def in_workspace?(workspace)
  return @workspaces.include?(workspace.id)
end

Raises:

  • (UnimplementedFunctionality)


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

def link_to(other, options = {})
  raise UnimplementedFunctionality
  self
end

#new_record?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/dgrid/api/entity.rb', line 54

def new_record?
  @id.nil?
end

#to_hashObject



50
51
52
# File 'lib/dgrid/api/entity.rb', line 50

def to_hash
  { :description => description }
end

#typeObject



25
26
27
# File 'lib/dgrid/api/entity.rb', line 25

def type
  self.class.type
end