Class: Qyu::Workflow

Inherits:
Object
  • Object
show all
Defined in:
lib/qyu/models/workflow.rb

Overview

Qyu::Workflow

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, name, descriptor, created_at = nil, updated_at = nil) ⇒ Workflow


8
9
10
11
12
13
14
# File 'lib/qyu/models/workflow.rb', line 8

def initialize(id, name, descriptor, created_at = nil, updated_at = nil)
  @id = id
  @name = name
  @descriptor = descriptor
  @created_at = created_at
  @updated_at = updated_at
end

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at


6
7
8
# File 'lib/qyu/models/workflow.rb', line 6

def created_at
  @created_at
end

#descriptorObject (readonly)

Returns the value of attribute descriptor


6
7
8
# File 'lib/qyu/models/workflow.rb', line 6

def descriptor
  @descriptor
end

#idObject (readonly)

Returns the value of attribute id


6
7
8
# File 'lib/qyu/models/workflow.rb', line 6

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name


6
7
8
# File 'lib/qyu/models/workflow.rb', line 6

def name
  @name
end

#updated_atObject (readonly)

Returns the value of attribute updated_at


6
7
8
# File 'lib/qyu/models/workflow.rb', line 6

def updated_at
  @updated_at
end

Class Method Details

.countObject


61
62
63
# File 'lib/qyu/models/workflow.rb', line 61

def count
  Qyu.store.count_workflows
end

.create(name:, descriptor:) ⇒ Object


17
18
19
20
21
22
23
# File 'lib/qyu/models/workflow.rb', line 17

def create(name:, descriptor:)
  validator = Qyu::Concerns::WorkflowDescriptorValidator.new(descriptor)
  fail Qyu::Errors::WorkflowDescriptorValidationError, validator.errors unless validator.valid?
  id = persist(name, descriptor)
  time = Time.now
  new(id, name, descriptor, time, time)
end

.delete(id) ⇒ Object


51
52
53
# File 'lib/qyu/models/workflow.rb', line 51

def delete(id)
  Qyu.store.delete_workflow(id)
end

.delete_by(name: nil, id: nil) ⇒ Object

Raises:

  • (ArgumentError)

55
56
57
58
59
# File 'lib/qyu/models/workflow.rb', line 55

def delete_by(name: nil, id: nil)
  raise ArgumentError, 'specify either name or id' if (name && id) || (name.nil? && id.nil?)
  Qyu.store.delete_workflow_by_name(name) if name
  delete(id) if id
end

.find(id, raise_error: true) ⇒ Object


25
26
27
28
29
30
# File 'lib/qyu/models/workflow.rb', line 25

def find(id, raise_error: true)
  workflow_attrs = Qyu.store.find_workflow(id)
  raise Qyu::Errors::WorkflowNotFound.new(:id, id) if workflow_attrs.nil? && raise_error
  return nil if workflow_attrs.nil?
  new(id, workflow_attrs['name'], workflow_attrs['descriptor'])
end

.find_by(name: nil, id: nil) ⇒ Object


32
33
34
35
# File 'lib/qyu/models/workflow.rb', line 32

def find_by(name: nil, id: nil)
  return find_by_name(name) if name
  return find(id, raise_error: false) if id
end

.find_by!(name: nil, id: nil) ⇒ Object


37
38
39
40
41
42
# File 'lib/qyu/models/workflow.rb', line 37

def find_by!(name: nil, id: nil)
  workflow = find_by(name: name, id: id)
  raise Qyu::Errors::WorkflowNotFound.new(:id, id) if workflow.nil? && id
  raise Qyu::Errors::WorkflowNotFound.new(:id, id) if workflow.nil? && id
  workflow
end

.select(limit: 30, offset: 0, order: :asc) ⇒ Object


44
45
46
47
48
49
# File 'lib/qyu/models/workflow.rb', line 44

def select(limit: 30, offset: 0, order: :asc)
  workflow_records = Qyu.store.select_workflows(limit, offset, order)
  workflow_records.map do |record|
    new(record['id'], record['name'], record['descriptor'])
  end
end

Instance Method Details

#[](attribute) ⇒ Object


78
79
80
# File 'lib/qyu/models/workflow.rb', line 78

def [](attribute)
  public_send(attribute)
end