Class: Arango::Task

Inherits:
Object
  • Object
show all
Includes:
Database_Return, Helper_Error, Helper_Return
Defined in:
lib/Task.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Database_Return

#database=

Methods included from Helper_Return

#return_delete, #return_directly?, #return_element

Methods included from Helper_Error

#satisfy_category?, #satisfy_class?, #warning_deprecated

Constructor Details

#initialize(id: nil, name: nil, type: nil, period: nil, command: nil, params: nil, created: nil, body: {}, database:, cache_name: nil) ⇒ Task

Returns a new instance of Task.



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/Task.rb', line 28

def initialize(id: nil, name: nil, type: nil, period: nil, command: nil,
  params: nil, created: nil, body: {}, database:, cache_name: nil)
  assign_database(database)
  unless cache_name.nil?
    @cache_name = cache_name
    @server.cache.save(:task, cache_name, self)
  end
  [:id, :name, :type, :period, :command, :params, :created].each do |k|
    body[k] ||= binding.local_variable_get(k)
  end
  assign_attributes(body)
end

Instance Attribute Details

#bodyObject

DEFINE ===



43
44
45
# File 'lib/Task.rb', line 43

def body
  @body
end

#cache_nameObject (readonly)

DEFINE ===



43
44
45
# File 'lib/Task.rb', line 43

def cache_name
  @cache_name
end

#commandObject

Returns the value of attribute command.



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

def command
  @command
end

#createdObject

Returns the value of attribute created.



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

def created
  @created
end

#databaseObject (readonly)

DEFINE ===



43
44
45
# File 'lib/Task.rb', line 43

def database
  @database
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#offsetObject

Returns the value of attribute offset.



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

def offset
  @offset
end

#paramsObject

Returns the value of attribute params.



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

def params
  @params
end

#periodObject

Returns the value of attribute period.



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

def period
  @period
end

#serverObject (readonly)

DEFINE ===



43
44
45
# File 'lib/Task.rb', line 43

def server
  @server
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

Class Method Details

.new(*args) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/Task.rb', line 9

def self.new(*args)
  hash = args[0]
  super unless hash.is_a?(Hash)
  database = hash[:database]
  if database.is_a?(Arango::Database) && database.server.active_cache && !hash[:id].nil?
    cache_name = "#{database.name}/#{hash[:id]}"
    cached = database.server.cache.cache.dig(:task, cache_name)
    if cached.nil?
      hash[:cache_name] = cache_name
      return super
    else
      body = hash[:body] || {}
      [:name, :type, :period, :command, :params, :created].each{|k| body[k] ||= hash[k]}
      cached.assign_attributes(body)
    end
  end
  super
end

Instance Method Details

#create(command: @command, period: @period, offset: @offset, params: @params) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/Task.rb', line 87

def create(command: @command, period: @period, offset: @offset, params: @params)
  body = {
    "id": @id,
    "name": @name,
    "command": command,
    "period": period,
    "offset": offset,
    "params": params,
    "database": @database.name
  }
  result = @database.request("POST", "_api/tasks", body: body)
  return return_element(result)
end

#destroyObject



115
116
117
118
# File 'lib/Task.rb', line 115

def destroy
  result = @server.request("DELETE", "_api/tasks/#{@id}")
  return return_directly?(result) ? result : true
end

#retrieveObject

RETRIEVE



82
83
84
85
# File 'lib/Task.rb', line 82

def retrieve
  result = @database.request("GET", "_api/tasks/#{@id}")
  return return_element(result)
end

#to_hObject

TO HASH ===



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/Task.rb', line 66

def to_h
  {
    "id": @id,
    "name": @name,
    "type": @type,
    "period": @period,
    "command": @command,
    "params": @params,
    "created": @created,
    "cache_name": @cache_name,
    "database": @database.name
  }.delete_if{|k,v| v.nil?}
end

#update(command: @command, period: @period, offset: @offset, params: @params) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/Task.rb', line 101

def update(command: @command, period: @period, offset: @offset,
  params: @params)
  body = {
    "id": @id,
    "name": @name,
    "command": command,
    "period": period,
    "offset": offset,
    "params": params
  }
  result = @database.request("PUT", "_api/tasks/#{@id}", body: body)
  return return_element(result)
end