Class: Prosereflect::Node

Inherits:
Lutaml::Model::Serializable
  • Object
show all
Defined in:
lib/prosereflect/node.rb

Constant Summary collapse

PM_TYPE =
'node'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = nil, attrs = nil) ⇒ Node

Returns a new instance of Node.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/prosereflect/node.rb', line 23

def initialize(data = nil, attrs = nil)
  if data.is_a?(String)
    super(type: data, attrs: attrs, content: [])
  elsif data.is_a?(Hash)
    # Handle marks in a special way to preserve expected behavior in tests
    if data[:marks] || data['marks']
      marks_data = data[:marks] || data['marks']
      data = data.dup
      data.delete('marks')
      data.delete(:marks)
      super(data)
      self.marks = marks_data
    else
      # Handle attrs properly
      if data[:attrs] || data['attrs']
        data = data.dup
        data[:attrs] = process_attrs_data(data[:attrs] || data['attrs'])
      end
      super(data)
    end
  else
    super()
  end
end

Class Method Details

.create(type = nil, attrs = nil) ⇒ Object



56
57
58
59
60
# File 'lib/prosereflect/node.rb', line 56

def self.create(type = nil, attrs = nil)
  new(type || self::PM_TYPE, attrs)
rescue NameError
  new(type || 'node', attrs)
end

Instance Method Details

#add_child(node) ⇒ Object

Add a child node to this node’s content



167
168
169
170
171
# File 'lib/prosereflect/node.rb', line 167

def add_child(node)
  self.content ||= []
  content << node
  node
end

#find_all(node_type) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
# File 'lib/prosereflect/node.rb', line 185

def find_all(node_type)
  results = []
  results << self if type == node_type

  content&.each do |child|
    child_results = child.find_all(node_type)
    results.concat(child_results) if child_results
  end

  results
end

#find_children(node_type) ⇒ Object



197
198
199
200
201
# File 'lib/prosereflect/node.rb', line 197

def find_children(node_type)
  return [] unless content

  content.select { |child| child.is_a?(node_type) }
end

#find_first(node_type) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
# File 'lib/prosereflect/node.rb', line 173

def find_first(node_type)
  return self if type == node_type

  return nil unless content

  content.each do |child|
    result = child.find_first(node_type)
    return result if result
  end
  nil
end

#marksObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/prosereflect/node.rb', line 105

def marks
  return nil if @marks.nil?
  return [] if @marks.empty?

  @marks.map do |mark|
    if mark.is_a?(Hash)
      mark
    elsif mark.respond_to?(:to_h)
      mark.to_h
    elsif mark.respond_to?(:type)
      { 'type' => mark.type.to_s }
    else
      raise ArgumentError, "Invalid mark type: #{mark.class}"
    end
  end
end

#marks=(value) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/prosereflect/node.rb', line 126

def marks=(value)
  if value.nil?
    @marks = nil
  elsif value.is_a?(Array) && value.empty?
    @marks = []
  elsif value.is_a?(Array)
    @marks = value.map do |v|
      if v.is_a?(Hash)
        type = v['type'] || v[:type]
        attrs = v['attrs'] || v[:attrs]
        begin
          mark_class = Prosereflect::Mark.const_get(type.to_s.capitalize)
          mark_class.new(attrs: attrs)
        rescue NameError
          Mark::Base.new(type: type, attrs: attrs)
        end
      elsif v.is_a?(Mark::Base)
        v
      elsif v.respond_to?(:type)
        begin
          mark_class = Prosereflect::Mark.const_get(v.type.to_s.capitalize)
          mark_class.new(attrs: v.attrs)
        rescue NameError
          Mark::Base.new(type: v.type, attrs: v.attrs)
        end
      else
        raise ArgumentError, "Invalid mark type: #{v.class}"
      end
    end
  else
    super
  end
end

#parse_content(content_data) ⇒ Object



160
161
162
163
164
# File 'lib/prosereflect/node.rb', line 160

def parse_content(content_data)
  return [] unless content_data

  content_data.map { |item| Parser.parse(item) }
end

#process_attrs_data(attrs_data) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/prosereflect/node.rb', line 48

def process_attrs_data(attrs_data)
  if attrs_data.is_a?(Hash)
    attrs_data.transform_keys(&:to_s)
  else
    attrs_data
  end
end

#raw_marksObject



122
123
124
# File 'lib/prosereflect/node.rb', line 122

def raw_marks
  @marks
end

#text_contentObject



203
204
205
206
207
# File 'lib/prosereflect/node.rb', line 203

def text_content
  return '' unless content

  content.map(&:text_content).join
end

#to_hObject Also known as: to_hash

Convert to hash for serialization



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/prosereflect/node.rb', line 63

def to_h
  result = { 'type' => type }

  if attrs && !attrs.empty?
    if attrs.is_a?(Hash)
      result['attrs'] = process_node_attributes(attrs, type)
    elsif attrs.is_a?(Array) && attrs.all? { |attr| attr.respond_to?(:to_h) }
      # Convert array of attribute objects to a hash
      attrs_array = attrs.map do |attr|
        attr.is_a?(Prosereflect::Attribute::Base) ? attr.to_h : attr
      end
      result['attrs'] = attrs_array unless attrs_array.empty?
    end
  end

  if marks && !marks.empty?
    result['marks'] = marks.map do |mark|
      if mark.is_a?(Hash)
        mark
      elsif mark.respond_to?(:to_h)
        mark.to_h
      elsif mark.respond_to?(:type)
        { 'type' => mark.type.to_s }
      else
        raise ArgumentError, "Invalid mark type: #{mark.class}"
      end
    end
  end

  if content && !content.empty?
    result['content'] = if content.is_a?(Array)
                          content.map { |item| item.respond_to?(:to_h) ? item.to_h : item }
                        else
                          [content]
                        end
  end

  result
end

#to_yaml(*args) ⇒ Object

Ensures YAML serialization outputs plain data instead of a Ruby object



210
211
212
# File 'lib/prosereflect/node.rb', line 210

def to_yaml(*args)
  to_h.to_yaml(*args)
end