Class: Grist::Type::Table

Inherits:
Base
  • Object
show all
Defined in:
lib/grist/type/table.rb

Overview

Defines a Grist Workspace

Constant Summary collapse

KEYS =
%w[
  id
  fields
  columns
].freeze

Constants inherited from Base

Base::PATH

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#deleted?, #keys

Methods included from Searchable

find_by

Methods included from Accessible

#access

Methods included from Rest

#create, #delete, #get, #list, #path, #request, #update

Constructor Details

#initialize(params = {}) ⇒ Table

Returns a new instance of Table.



15
16
17
18
# File 'lib/grist/type/table.rb', line 15

def initialize(params = {})
  @doc_id = params[:doc_id]
  super params
end

Class Method Details

.access(id) ⇒ Object



121
122
123
124
125
126
127
128
129
# File 'lib/grist/type/table.rb', line 121

def self.access(id)
  org = find(id)
  grist_res = org.access
  return unless grist_res.success? && grist_res.data

  grist_res.data["users"].map do |access|
    Access.new(access)
  end
end

.all(org_id) ⇒ Object

List all workspaces # # @return [Array] Array of workspaces



83
84
85
86
87
88
# File 'lib/grist/type/table.rb', line 83

def self.all(org_id)
  grist_res = new(org_id: org_id).list
  return [] unless grist_res&.data.is_a?(Array)

  grist_res.data&.map { |org| Workspace.new(org) }
end

.create(doc_id, data) ⇒ Object

Updates the workspace # @param id [Integer] The ID of the workspace to delete # # @param data [Hash] The data to update the workspace with # @return [self | nil] The updated workspace or nil if not found



76
77
78
79
# File 'lib/grist/type/table.rb', line 76

def self.create(doc_id, data)
  obj = new(doc_id: doc_id)
  obj.create(data)
end

.delete(id) ⇒ Object

Deletes the workspace # @param id [Integer] The ID of the workspace to delete # @return [self | nil] The deleted workspace or nil if not found



114
115
116
117
118
119
# File 'lib/grist/type/table.rb', line 114

def self.delete(id)
  org = find(id)
  return unless org

  org.delete
end

.find(id) ⇒ Object

Finds an workspace by ID # @param id [Integer] The ID of the workspace to find # # return [self | nil] The workspace or nil if not found



93
94
95
96
97
98
# File 'lib/grist/type/table.rb', line 93

def self.find(id)
  grist_res = new.get(id)
  return unless grist_res.success? && grist_res.data

  new(grist_res.data)
end

.update(id, data) ⇒ Object

Updates the workspace # @param id [Integer] The ID of the workspace to delete # # @param data [Hash] The data to update the workspace with # @return [self | nil] The updated workspace or nil if not found



104
105
106
107
108
109
# File 'lib/grist/type/table.rb', line 104

def self.update(id, data)
  org = find(id)
  return unless org

  org.update(data)
end

Instance Method Details

#columnsObject



32
33
34
35
36
37
38
39
# File 'lib/grist/type/table.rb', line 32

def columns
  grist_res = request(:get, columns_path)
  return [] unless grist_res.success? && grist_res.data

  grist_res.data.map do |column|
    Column.new(column)
  end
end

#columns_pathObject



28
29
30
# File 'lib/grist/type/table.rb', line 28

def columns_path
  "/docs/#{@doc_id}/tables/#{@id}/columns"
end

#create_records(data) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/grist/type/table.rb', line 57

def create_records(data)
  grist_res = request(:post, records_path, data)
  return [] unless grist_res.success? && grist_res.data

  @records = grist_res.data["records"].map do |record|
    Record.new(record.merge(doc_id: @doc_id, table_id: @id))
  end

  @records
end

#records(params = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/grist/type/table.rb', line 45

def records(params = {})
  grist_res = request(:get, records_path, params)

  return [] unless grist_res.success? && grist_res.data

  @records = grist_res.data["records"].map do |record|
    Record.new(record.merge(doc_id: @doc_id, table_id: @id))
  end

  @records
end

#records_pathObject



41
42
43
# File 'lib/grist/type/table.rb', line 41

def records_path
  "/docs/#{@doc_id}/tables/#{@id}/records"
end

#to_hashObject



20
21
22
23
24
25
26
# File 'lib/grist/type/table.rb', line 20

def to_hash
  {
    "id" => @id,
    "fields" => @fields,
    "columns" => @columns
  }
end