Class: RubyRich::Transcript::Entry

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, type:, content: "", metadata: {}, status: nil, collapsed: false, name: nil) ⇒ Entry

Returns a new instance of Entry.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ruby_rich/transcript.rb', line 23

def initialize(id:, type:, content: "", metadata: {}, status: nil, collapsed: false, name: nil)
  @id = id
  @type = type
  @content = +content.to_s
  @metadata = .dup
  @status = status
  @collapsed = collapsed
  @name = name
  @version = 0
  @render_cache = {}
end

Instance Attribute Details

#collapsedObject

Returns the value of attribute collapsed.



20
21
22
# File 'lib/ruby_rich/transcript.rb', line 20

def collapsed
  @collapsed
end

#contentObject

Returns the value of attribute content.



20
21
22
# File 'lib/ruby_rich/transcript.rb', line 20

def content
  @content
end

#idObject

Returns the value of attribute id.



20
21
22
# File 'lib/ruby_rich/transcript.rb', line 20

def id
  @id
end

#metadataObject

Returns the value of attribute metadata.



20
21
22
# File 'lib/ruby_rich/transcript.rb', line 20

def 
  @metadata
end

#nameObject

Returns the value of attribute name.



20
21
22
# File 'lib/ruby_rich/transcript.rb', line 20

def name
  @name
end

#statusObject

Returns the value of attribute status.



20
21
22
# File 'lib/ruby_rich/transcript.rb', line 20

def status
  @status
end

#typeObject

Returns the value of attribute type.



20
21
22
# File 'lib/ruby_rich/transcript.rb', line 20

def type
  @type
end

#versionObject (readonly)

Returns the value of attribute version.



21
22
23
# File 'lib/ruby_rich/transcript.rb', line 21

def version
  @version
end

Instance Method Details

#[](key) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/ruby_rich/transcript.rb', line 85

def [](key)
  case key.to_sym
  when :text, :content
    @content
  when :id
    @id
  when :type
    @type
  when :metadata
    @metadata
  when :status
    @status
  when :collapsed
    @collapsed
  when :name
    @name
  else
    @metadata[key] || @metadata[key.to_sym] || @metadata[key.to_s]
  end
end

#[]=(key, value) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/ruby_rich/transcript.rb', line 106

def []=(key, value)
  case key.to_sym
  when :text, :content
    replace(value)
  when :type
    @type = value
    touch
  when :metadata
    @metadata = value || {}
    touch
  when :status
    @status = value
    touch
  when :collapsed
    @collapsed = value
    touch
  when :name
    @name = value
    touch
  else
    @metadata[key] = value
    touch
  end
end

#append(delta) ⇒ Object



67
68
69
70
71
# File 'lib/ruby_rich/transcript.rb', line 67

def append(delta)
  @content << delta.to_s
  touch
  self
end

#cache_fetch(cache_key) ⇒ Object



131
132
133
134
135
136
137
# File 'lib/ruby_rich/transcript.rb', line 131

def cache_fetch(cache_key)
  versioned_key = [@version, cache_key]
  return @render_cache[versioned_key] if @render_cache.key?(versioned_key)

  @render_cache.clear
  @render_cache[versioned_key] = yield
end

#replace(new_content) ⇒ Object



73
74
75
76
77
# File 'lib/ruby_rich/transcript.rb', line 73

def replace(new_content)
  @content = +new_content.to_s
  touch
  self
end

#textObject



35
36
37
# File 'lib/ruby_rich/transcript.rb', line 35

def text
  @content
end

#text=(value) ⇒ Object



39
40
41
# File 'lib/ruby_rich/transcript.rb', line 39

def text=(value)
  replace(value)
end

#to_hObject



139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/ruby_rich/transcript.rb', line 139

def to_h
  {
    id: @id,
    type: @type,
    text: @content,
    content: @content,
    metadata: @metadata,
    status: @status,
    collapsed: @collapsed,
    name: @name
  }.compact
end

#update {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



79
80
81
82
83
# File 'lib/ruby_rich/transcript.rb', line 79

def update
  yield self
  touch
  self
end