Class: RubyRich::Transcript::Store

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ruby_rich/transcript.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStore

Returns a new instance of Store.



165
166
167
168
169
# File 'lib/ruby_rich/transcript.rb', line 165

def initialize
  @entries = []
  @sequence = 0
  @mutex = Mutex.new
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



163
164
165
# File 'lib/ruby_rich/transcript.rb', line 163

def entries
  @entries
end

Instance Method Details

#add(type:, content: "", metadata: {}, status: nil, collapsed: nil, id: nil, name: nil) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/ruby_rich/transcript.rb', line 171

def add(type:, content: "", metadata: {}, status: nil, collapsed: nil, id: nil, name: nil)
  normalized_type = normalize_type(type)
  collapsed = default_collapsed(normalized_type) if collapsed.nil?

  @mutex.synchronize do
    entry = Entry.new(
      id: id || next_id(normalized_type),
      type: normalized_type,
      content: content,
      metadata: ,
      status: status,
      collapsed: collapsed,
      name: name
    )
    @entries << entry
    entry
  end
end

#append(id, delta) ⇒ Object



190
191
192
# File 'lib/ruby_rich/transcript.rb', line 190

def append(id, delta)
  mutate(id) { |entry| entry.append(delta) }
end

#collapse(id) ⇒ Object



224
225
226
# File 'lib/ruby_rich/transcript.rb', line 224

def collapse(id)
  update(id) { |entry| entry.collapsed = true }
end

#each(&block) ⇒ Object



232
233
234
# File 'lib/ruby_rich/transcript.rb', line 232

def each(&block)
  @entries.each(&block)
end

#expand(id) ⇒ Object



220
221
222
# File 'lib/ruby_rich/transcript.rb', line 220

def expand(id)
  update(id) { |entry| entry.collapsed = false }
end

#find(id) ⇒ Object



212
213
214
# File 'lib/ruby_rich/transcript.rb', line 212

def find(id)
  @mutex.synchronize { @entries.find { |entry| entry.id == id } }
end

#index(id) ⇒ Object



216
217
218
# File 'lib/ruby_rich/transcript.rb', line 216

def index(id)
  @mutex.synchronize { @entries.index { |entry| entry.id == id } }
end

#remove(id) ⇒ Object



198
199
200
201
202
203
204
205
206
# File 'lib/ruby_rich/transcript.rb', line 198

def remove(id)
  @mutex.synchronize do
    index = @entries.index { |entry| entry.id == id }
    return false unless index

    @entries.delete_at(index)
    true
  end
end

#replace(id, new_content) ⇒ Object



194
195
196
# File 'lib/ruby_rich/transcript.rb', line 194

def replace(id, new_content)
  mutate(id) { |entry| entry.replace(new_content) }
end

#toggle(id) ⇒ Object



228
229
230
# File 'lib/ruby_rich/transcript.rb', line 228

def toggle(id)
  update(id) { |entry| entry.collapsed = !entry.collapsed }
end

#update(id) ⇒ Object



208
209
210
# File 'lib/ruby_rich/transcript.rb', line 208

def update(id)
  mutate(id) { |entry| entry.update { |target| yield target } }
end