Class: Grist::Type::Workspace

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

Overview

Defines a Grist Workspace

Constant Summary collapse

PATH =
"/workspaces"
KEYS =
%w[
  id
  name
  createdAt
  updatedAt
  isSupportWorkspace
  docs
  access
  owner
  orgDomain
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

delete, #deleted?, find, #keys, update

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 = {}) ⇒ Workspace

Returns a new instance of Workspace.



23
24
25
26
27
# File 'lib/grist/type/workspace.rb', line 23

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

Instance Attribute Details

#org_idObject (readonly)

Returns the value of attribute org_id.



21
22
23
# File 'lib/grist/type/workspace.rb', line 21

def org_id
  @org_id
end

Class Method Details

.all(org_id) ⇒ Array

List all workspaces

Parameters:

  • org_id (Integer)

    The ID of the organization to list workspaces for

Returns:

  • (Array)

    Array of workspaces



70
71
72
73
74
75
76
77
# File 'lib/grist/type/workspace.rb', line 70

def self.all(org_id)
  grist_res = new(org_id: org_id).list

  return [] unless grist_res&.data.is_a?(Array)
  return [] unless grist_res&.data&.any?

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

.create(org_id, data) ⇒ Grist::Type::Workspace?

Creates the workspace

Parameters:

  • id (Integer)

    The ID of the workspace to create

  • data (Hash)

    The data to create the workspace with

Returns:



62
63
64
65
# File 'lib/grist/type/workspace.rb', line 62

def self.create(org_id, data)
  org = Type::Organization.find(org_id)
  org.create_workspace(data)
end

Instance Method Details

#create_doc(data) ⇒ Grist::Type::Doc?

Create a new Doc in the workspace

Parameters:

  • data (Hash)

    The data to create the doc with

Returns:



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

def create_doc(data)
  grist_res = request(:post, doc_path, data)

  return unless grist_res.success?

  data["id"] = grist_res.data
  data.transform_keys!(&:to_s)
  doc = Doc.new(data)
  @docs ||= []
  @docs << doc

  doc
end

#docsObject



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

def docs
  return @docs if @docs&.any? && @docs.first.is_a?(Doc)

  if @docs.any? && @docs.first.is_a?(Hash)
    @docs = @docs.map do |doc|
      Doc.new(doc.merge(ws_id: @id))
    end
  end

  @docs
end