Class: Prosereflect::User

Inherits:
Node
  • Object
show all
Defined in:
lib/prosereflect/user.rb

Overview

User class represents a user mention in ProseMirror.

Constant Summary collapse

PM_TYPE =
'user'

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Node

#find_all, #find_children, #find_first, #marks, #marks=, #parse_content, #process_attrs_data, #raw_marks, #text_content, #to_yaml

Constructor Details

#initialize(attributes = {}) ⇒ User

Returns a new instance of User.



20
21
22
23
24
25
26
27
28
# File 'lib/prosereflect/user.rb', line 20

def initialize(attributes = {})
  attributes[:content] = []
  super

  return unless attributes[:attrs]

  @id = attributes[:attrs]['id']
  self.attrs = { 'id' => @id }
end

Class Method Details

.create(attrs = nil) ⇒ Object



30
31
32
# File 'lib/prosereflect/user.rb', line 30

def self.create(attrs = nil)
  new(attrs: attrs)
end

Instance Method Details

#add_childObject

Override content-related methods since user mentions don’t have content

Raises:

  • (NotImplementedError)


46
47
48
# File 'lib/prosereflect/user.rb', line 46

def add_child(*)
  raise NotImplementedError, 'User mention nodes cannot have children'
end

#contentObject



50
51
52
# File 'lib/prosereflect/user.rb', line 50

def content
  []
end

#idObject



41
42
43
# File 'lib/prosereflect/user.rb', line 41

def id
  @id || attrs&.[]('id')
end

#id=(user_id) ⇒ Object

Update the user ID



35
36
37
38
39
# File 'lib/prosereflect/user.rb', line 35

def id=(user_id)
  @id = user_id
  self.attrs ||= {}
  attrs['id'] = user_id
end

#to_hObject



54
55
56
57
58
59
60
61
# File 'lib/prosereflect/user.rb', line 54

def to_h
  hash = super
  hash['attrs'] = {
    'id' => id
  }
  hash['content'] = []
  hash
end