Class: JiraCache::Data::IssueRepository

Inherits:
Object
  • Object
show all
Defined in:
lib/jira_cache/data/issue_repository.rb

Overview

Superclass for repositories. Simply provide some shared methods.

Class Method Summary collapse

Class Method Details

.countObject



85
86
87
# File 'lib/jira_cache/data/issue_repository.rb', line 85

def self.count
  table.count
end

.delete_where(where_data) ⇒ Object



69
70
71
# File 'lib/jira_cache/data/issue_repository.rb', line 69

def self.delete_where(where_data)
  table.where(where_data).delete
end

.exist_with_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/jira_cache/data/issue_repository.rb', line 33

def self.exist_with_key?(key)
  table.where(key: key).count != 0
end

.find_by_key(key) ⇒ Object



29
30
31
# File 'lib/jira_cache/data/issue_repository.rb', line 29

def self.find_by_key(key)
  table.where(key: key).first
end

.first_where(where_data) ⇒ Object



77
78
79
# File 'lib/jira_cache/data/issue_repository.rb', line 77

def self.first_where(where_data)
  table.where(where_data).first
end

.indexObject



81
82
83
# File 'lib/jira_cache/data/issue_repository.rb', line 81

def self.index
  table.entries
end

.insert(key:, data:, synced_at:, deleted_from_jira_at: nil) ⇒ Object

It inserts a new issue row with the specified data. If the issue already exists (checking on the “key”), the row is updated instead.



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/jira_cache/data/issue_repository.rb', line 15

def self.insert(key:, data:, synced_at:, deleted_from_jira_at: nil)
  attributes = {
    key: key,
    data: Sequel.pg_json(data),
    synced_at: synced_at,
    deleted_from_jira_at: deleted_from_jira_at
  }
  if exist_with_key?(key)
    update_where({ key: key }, attributes)
  else
    table.insert row(attributes)
  end
end

.keys_for_deleted_issuesObject



61
62
63
64
65
66
67
# File 'lib/jira_cache/data/issue_repository.rb', line 61

def self.keys_for_deleted_issues
  table
    .where("deleted_from_jira_at IS NOT NULL")
    .select(:key)
    .map(&:values)
    .flatten
end

.keys_for_non_deleted_issuesObject



53
54
55
56
57
58
59
# File 'lib/jira_cache/data/issue_repository.rb', line 53

def self.keys_for_non_deleted_issues
  table
    .where("deleted_from_jira_at IS NULL")
    .select(:key)
    .map(&:values)
    .flatten
end

.keys_in_all_projectsObject



46
47
48
49
50
51
# File 'lib/jira_cache/data/issue_repository.rb', line 46

def self.keys_in_all_projects
  table.
    select(:key).
    map(&:values).
    flatten
end

.keys_in_project(project_key) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/jira_cache/data/issue_repository.rb', line 37

def self.keys_in_project(project_key)
  return keys_in_all_projects if project_key.nil?
  table.
    where("(data #>> '{fields,project,key}') = ?", project_key).
    select(:key).
    map(&:values).
    flatten
end

.latest_sync_timeObject



89
90
91
# File 'lib/jira_cache/data/issue_repository.rb', line 89

def self.latest_sync_time
  table.order(:synced_at).select(:synced_at).last&.dig(:synced_at)
end

.row(attributes, time = nil) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/jira_cache/data/issue_repository.rb', line 93

def self.row(attributes, time = nil)
  time ||= Time.now
  attributes.merge(
    created_at: time,
    updated_at: time
  )
end

.tableObject



101
102
103
# File 'lib/jira_cache/data/issue_repository.rb', line 101

def self.table
  DB[:jira_cache_issues]
end

.update_where(where_data, values) ⇒ Object



73
74
75
# File 'lib/jira_cache/data/issue_repository.rb', line 73

def self.update_where(where_data, values)
  table.where(where_data).update(values)
end