Class: RubyRich::Transcript::Store
- Inherits:
-
Object
- Object
- RubyRich::Transcript::Store
- Includes:
- Enumerable
- Defined in:
- lib/ruby_rich/transcript.rb
Instance Attribute Summary collapse
-
#entries ⇒ Object
readonly
Returns the value of attribute entries.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Instance Method Summary collapse
- #add(type:, content: "", metadata: {}, status: nil, collapsed: nil, id: nil, name: nil) ⇒ Object
- #append(id, delta) ⇒ Object
- #collapse(id) ⇒ Object
- #each(&block) ⇒ Object
- #expand(id) ⇒ Object
- #find(id) ⇒ Object
- #index(id) ⇒ Object
-
#initialize ⇒ Store
constructor
A new instance of Store.
- #remove(id) ⇒ Object
- #replace(id, new_content) ⇒ Object
- #toggle(id) ⇒ Object
- #update(id) ⇒ Object
Constructor Details
#initialize ⇒ Store
Returns a new instance of Store.
165 166 167 168 169 170 |
# File 'lib/ruby_rich/transcript.rb', line 165 def initialize @entries = [] @sequence = 0 @version = 0 @mutex = Mutex.new end |
Instance Attribute Details
#entries ⇒ Object (readonly)
Returns the value of attribute entries.
163 164 165 |
# File 'lib/ruby_rich/transcript.rb', line 163 def entries @entries end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
163 164 165 |
# File 'lib/ruby_rich/transcript.rb', line 163 def version @version end |
Instance Method Details
#add(type:, content: "", metadata: {}, status: nil, collapsed: nil, id: nil, name: nil) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/ruby_rich/transcript.rb', line 172 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 touch entry end end |
#append(id, delta) ⇒ Object
192 193 194 |
# File 'lib/ruby_rich/transcript.rb', line 192 def append(id, delta) mutate(id) { |entry| entry.append(delta) } end |
#collapse(id) ⇒ Object
227 228 229 |
# File 'lib/ruby_rich/transcript.rb', line 227 def collapse(id) update(id) { |entry| entry.collapsed = true } end |
#each(&block) ⇒ Object
235 236 237 |
# File 'lib/ruby_rich/transcript.rb', line 235 def each(&block) @entries.each(&block) end |
#expand(id) ⇒ Object
223 224 225 |
# File 'lib/ruby_rich/transcript.rb', line 223 def (id) update(id) { |entry| entry.collapsed = false } end |
#find(id) ⇒ Object
215 216 217 |
# File 'lib/ruby_rich/transcript.rb', line 215 def find(id) @mutex.synchronize { @entries.find { |entry| entry.id == id } } end |
#index(id) ⇒ Object
219 220 221 |
# File 'lib/ruby_rich/transcript.rb', line 219 def index(id) @mutex.synchronize { @entries.index { |entry| entry.id == id } } end |
#remove(id) ⇒ Object
200 201 202 203 204 205 206 207 208 209 |
# File 'lib/ruby_rich/transcript.rb', line 200 def remove(id) @mutex.synchronize do index = @entries.index { |entry| entry.id == id } return false unless index @entries.delete_at(index) touch true end end |
#replace(id, new_content) ⇒ Object
196 197 198 |
# File 'lib/ruby_rich/transcript.rb', line 196 def replace(id, new_content) mutate(id) { |entry| entry.replace(new_content) } end |
#toggle(id) ⇒ Object
231 232 233 |
# File 'lib/ruby_rich/transcript.rb', line 231 def toggle(id) update(id) { |entry| entry.collapsed = !entry.collapsed } end |
#update(id) ⇒ Object
211 212 213 |
# File 'lib/ruby_rich/transcript.rb', line 211 def update(id) mutate(id) { |entry| entry.update { |target| yield target } } end |