y-rb 0.3.2-x86_64-linux-musl → 0.4.0-x86_64-linux-musl

Sign up to get free protection for your applications and to get access to all the features.
data/lib/y/xml.rb CHANGED
@@ -21,7 +21,7 @@ module Y
21
21
 
22
22
  # Create a new XMLElement instance
23
23
  #
24
- # @param [Y::Doc] doc
24
+ # @param doc [Y::Doc]
25
25
  def initialize(doc = nil)
26
26
  @document = doc || Y::Doc.new
27
27
 
@@ -30,22 +30,24 @@ module Y
30
30
 
31
31
  # Retrieve node at index
32
32
  #
33
- # @param [Integer] index
33
+ # @param index [Integer]
34
34
  # @return [Y::XMLElement|nil]
35
35
  def [](index)
36
- node = yxml_element_get(index)
36
+ node = document.current_transaction { |tx| yxml_element_get(tx, index) }
37
37
  node&.document = document
38
38
  node
39
39
  end
40
40
 
41
41
  # Create a node at index
42
42
  #
43
- # @param [Integer] index
44
- # @param [String] name Name of node, e.g. `<p />`
43
+ # @param index [Integer]
44
+ # @param name [String] Name of node, e.g. `<p />`
45
45
  # @return [Y::XMLElement]
46
46
  # rubocop:disable Lint/Void
47
47
  def []=(index, name)
48
- node = yxml_element_insert_element(transaction, index, name)
48
+ node = document.current_transaction do |tx|
49
+ yxml_element_insert_element(tx, index, name)
50
+ end
49
51
  node.document = document
50
52
  node
51
53
  end
@@ -55,7 +57,7 @@ module Y
55
57
  #
56
58
  # @return [Hash]
57
59
  def attrs
58
- yxml_element_attributes
60
+ document.current_transaction { |tx| yxml_element_attributes(tx) }
59
61
  end
60
62
 
61
63
  alias attributes attrs
@@ -64,7 +66,7 @@ module Y
64
66
  #
65
67
  # @return [Y::XMLElement]
66
68
  def first_child
67
- child = yxml_element_first_child
69
+ child = document.current_transaction { |tx| yxml_element_first_child(tx) }
68
70
  child&.document = document
69
71
  child
70
72
  end
@@ -73,13 +75,14 @@ module Y
73
75
  #
74
76
  # Optional input is pushed to the text if provided
75
77
  #
76
- # @param [Integer] index
77
- # @param [String|nil] input
78
+ # @param index [Integer]
79
+ # @param input [String|nil]
78
80
  # @return [Y::XMLText]
79
- def insert_text(index, input = nil)
80
- text = yxml_element_insert_text(transaction, index)
81
+ def insert_text(index, input = "")
82
+ text = document.current_transaction do |tx|
83
+ yxml_element_insert_text(tx, index, input)
84
+ end
81
85
  text.document = document
82
- text << input unless input.nil?
83
86
  text
84
87
  end
85
88
 
@@ -87,7 +90,7 @@ module Y
87
90
  #
88
91
  # @return [Y::XMLElement|Y::XMLText|nil]
89
92
  def next_sibling
90
- node = yxml_element_next_sibling
93
+ node = document.current_transaction { |tx| yxml_element_next_sibling(tx) }
91
94
  node&.document = document
92
95
  node
93
96
  end
@@ -106,8 +109,8 @@ module Y
106
109
  # xml_element = doc.get_xml_element("my xml element")
107
110
  # xml_element.attach { |changes| … }
108
111
  #
109
- # @param [Proc] callback
110
- # @param [Block] block
112
+ # @param callback [Proc]
113
+ # @param block [Block]
111
114
  # @return [Integer] The subscription ID
112
115
  def attach(callback = nil, &block)
113
116
  return yxml_element_observe(callback) unless callback.nil?
@@ -128,17 +131,19 @@ module Y
128
131
  #
129
132
  # @return [Y::XMLElement|Y::XMLText|nil]
130
133
  def prev_sibling
131
- node = yxml_element_prev_sibling
134
+ node = document.current_transaction { |tx| yxml_element_prev_sibling(tx) }
132
135
  node&.document = document
133
136
  node
134
137
  end
135
138
 
136
139
  # Creates a new child an inserts at the end of the children list
137
140
  #
138
- # @param [String] name
141
+ # @param name [String]
139
142
  # @return [Y::XMLElement]
140
143
  def <<(name)
141
- xml_element = yxml_element_push_elem_back(transaction, name)
144
+ xml_element = document.current_transaction do |tx|
145
+ yxml_element_push_element_back(tx, name)
146
+ end
142
147
  xml_element.document = document
143
148
  xml_element
144
149
  end
@@ -149,12 +154,13 @@ module Y
149
154
  #
150
155
  # The optional str argument initializes the text node with its value
151
156
  #
152
- # @param [String] str
157
+ # @param str [String]
153
158
  # @return [Y::XMLText]
154
- def push_text(str = nil)
155
- text = yxml_element_push_text_back(transaction)
159
+ def push_text(str = "")
160
+ text = document.current_transaction do |tx|
161
+ yxml_element_push_text_back(tx, str)
162
+ end
156
163
  text.document = document
157
- text << str unless str.nil?
158
164
  text
159
165
  end
160
166
 
@@ -162,7 +168,7 @@ module Y
162
168
  #
163
169
  # @return [Integer]
164
170
  def size
165
- yxml_element_size
171
+ document.current_transaction { |tx| yxml_element_size(tx) }
166
172
  end
167
173
 
168
174
  # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
@@ -192,42 +198,44 @@ module Y
192
198
  #
193
199
  # @return [void]
194
200
  def slice!(*args)
195
- if args.size.zero?
196
- raise ArgumentError,
197
- "Provide one of `index`, `range`, `start, length` as arguments"
198
- end
199
-
200
- if args.size == 1
201
- arg = args.first
201
+ document.current_transaction do |tx| # rubocop:disable Metrics/BlockLength
202
+ if args.size.zero?
203
+ raise ArgumentError,
204
+ "Provide one of `index`, `range`, `start, length` as arguments"
205
+ end
202
206
 
203
- if arg.is_a?(Range)
204
- if arg.exclude_end?
205
- yxml_element_remove_range(transaction, arg.first,
206
- arg.last - arg.first)
207
+ if args.size == 1
208
+ arg = args.first
209
+
210
+ if arg.is_a?(Range)
211
+ if arg.exclude_end?
212
+ yxml_element_remove_range(tx, arg.first,
213
+ arg.last - arg.first)
214
+ end
215
+ unless arg.exclude_end?
216
+ yxml_element_remove_range(tx, arg.first,
217
+ arg.last + 1 - arg.first)
218
+ end
219
+ return nil
207
220
  end
208
- unless arg.exclude_end?
209
- yxml_element_remove_range(transaction, arg.first,
210
- arg.last + 1 - arg.first)
211
- end
212
- return nil
213
- end
214
221
 
215
- if arg.is_a?(Numeric)
216
- yxml_element_remove_range(transaction, arg.to_int, 1)
217
- return nil
222
+ if arg.is_a?(Numeric)
223
+ yxml_element_remove_range(tx, arg.to_int, 1)
224
+ return nil
225
+ end
218
226
  end
219
- end
220
227
 
221
- if args.size == 2
222
- first, second = args
228
+ if args.size == 2
229
+ first, second = args
223
230
 
224
- if first.is_a?(Numeric) && second.is_a?(Numeric)
225
- yxml_element_remove_range(transaction, first, second)
226
- return nil
231
+ if first.is_a?(Numeric) && second.is_a?(Numeric)
232
+ yxml_element_remove_range(tx, first, second)
233
+ return nil
234
+ end
227
235
  end
228
- end
229
236
 
230
- raise ArgumentError, "Please check your arguments, can't slice."
237
+ raise ArgumentError, "Please check your arguments, can't slice."
238
+ end
231
239
  end
232
240
 
233
241
  # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
@@ -243,12 +251,12 @@ module Y
243
251
  #
244
252
  # @return [String]
245
253
  def to_s
246
- yxml_element_to_s
254
+ document.current_transaction { |tx| yxml_element_to_s(tx) }
247
255
  end
248
256
 
249
257
  # Detach a listener
250
258
  #
251
- # @param [Integer] subscription_id
259
+ # @param subscription_id [Integer]
252
260
  # @return [void]
253
261
  def detach(subscription_id)
254
262
  yxml_element_unobserve(subscription_id)
@@ -256,10 +264,12 @@ module Y
256
264
 
257
265
  # Creates a new node and puts it in front of the child list
258
266
  #
259
- # @param [String] name
267
+ # @param name [String]
260
268
  # @return [Y::XMLElement]
261
269
  def unshift_child(name)
262
- xml_element = yxml_element_push_elem_front(transaction, name)
270
+ xml_element = document.current_transaction do |tx|
271
+ yxml_element_push_element_front(tx, name)
272
+ end
263
273
  xml_element.document = document
264
274
  xml_element
265
275
  end
@@ -268,12 +278,13 @@ module Y
268
278
  #
269
279
  # The optional str argument initializes the text node with its value
270
280
  #
271
- # @param [String] str
281
+ # @param str [String]
272
282
  # @return [Y::XMLText]
273
- def unshift_text(str = nil)
274
- text = yxml_element_push_text_front(transaction)
283
+ def unshift_text(str = "")
284
+ text = document.current_transaction do |tx|
285
+ yxml_element_push_text_front(tx, str)
286
+ end
275
287
  text.document = document
276
- text << args.first unless str.nil?
277
288
  text
278
289
  end
279
290
 
@@ -299,15 +310,20 @@ module Y
299
310
  getter = getter.to_s.slice(0...-1)&.to_sym if is_setter
300
311
 
301
312
  define_singleton_method(setter.to_sym) do |new_val|
302
- yxml_element_insert_attribute(transaction,
303
- method_name.to_s
304
- .delete_suffix("=")
305
- .delete_prefix("attr_"),
306
- new_val)
313
+ document.current_transaction do |tx|
314
+ yxml_element_insert_attribute(tx,
315
+ method_name.to_s
316
+ .delete_suffix("=")
317
+ .delete_prefix("attr_"),
318
+ new_val)
319
+ end
307
320
  end
308
321
 
309
322
  define_singleton_method(getter) do
310
- yxml_element_get_attribute(method_name.to_s.delete_prefix("attr_"))
323
+ document.current_transaction do |tx|
324
+ yxml_element_get_attribute(tx,
325
+ method_name.to_s.delete_prefix("attr_"))
326
+ end
311
327
  end
312
328
 
313
329
  if is_setter
@@ -326,131 +342,131 @@ module Y
326
342
  method_name.to_s.start_with?("attr_") || super
327
343
  end
328
344
 
329
- private
330
-
331
345
  # @!method yxml_element_attributes
332
346
  #
333
347
  # @return [Hash]
334
348
 
335
- # @!method yxml_element_first_child
349
+ # @!method yxml_element_first_child(tx)
336
350
  #
351
+ # @param tx [Y::Transaction]
337
352
  # @return [Y::XMLElement|Y::XMLText]
338
353
 
339
- # @!method yxml_element_get_attribute(name)
354
+ # @!method yxml_element_get_attribute(tx, name)
340
355
  #
341
- # @param [String] name
356
+ # @param tx [Y::Transaction]
357
+ # @param name [String]
342
358
  # @return [String|nil]
343
359
 
344
- # @!method yxml_element_get(index)
360
+ # @!method yxml_element_get(tx, index)
345
361
  #
346
- # @param [Integer] index
362
+ # @param tx [Y::Transaction]
363
+ # @param index [Integer]
347
364
  # @return [Y::XMLElement|Y::XMLText|nil]
348
365
 
349
- # @!method yxml_element_insert_attribute(transaction, name, value)
366
+ # @!method yxml_element_insert_attribute(tx, name, value)
350
367
  #
351
- # @param [Y::Transaction] transaction
352
- # @param [String] name
353
- # @param [String] value
368
+ # @param tx [Y::Transaction]
369
+ # @param name [String]
370
+ # @param value [String]
354
371
  # @return [String|nil]
355
372
 
356
- # @!method yxml_element_insert_element(transaction, index, name)
373
+ # @!method yxml_element_insert_element(tx, index, name)
357
374
  # Insert XML element into this XML element
375
+ #
358
376
  # @!visibility private
359
- # @param [Y::Transaction] transaction
360
- # @param [Integer] index
361
- # @param [String] name
377
+ # @param tx [Y::Transaction]
378
+ # @param index [Integer]
379
+ # @param name [String]
362
380
  # @return [Y::XMLElement]
363
381
 
364
- # @!method yxml_element_insert_text(transaction, index)
382
+ # @!method yxml_element_insert_text(tx, index, text)
365
383
  #
366
- # @param [Y::Transaction] transaction
367
- # @param [Integer] index
384
+ # @param tx [Y::Transaction]
385
+ # @param index [Integer]
386
+ # @param text [String]
368
387
  # @return [Y::XMLText]
369
388
 
370
- # @!method yxml_element_next_sibling()
389
+ # @!method yxml_element_next_sibling(tx)
371
390
  #
391
+ # @param tx [Y::Transaction]
372
392
  # @return [Y::XMLElement|XMLText|nil]
373
393
 
374
394
  # @!method yxml_element_observe(callback)
375
395
  #
376
- # @param [Proc] callback
396
+ # @param callback [Proc]
377
397
  # @return [Integer] The subscription ID
378
398
 
379
399
  # @!method yxml_element_parent()
380
400
  #
381
401
  # @return [Y::XMLElement|nil]
382
402
 
383
- # @!method yxml_element_prev_sibling()
403
+ # @!method yxml_element_prev_sibling(tx)
384
404
  #
405
+ # @param tx [Y::Transaction]
385
406
  # @return [Y::XMLElement|XMLText|nil]
386
407
 
387
- # @!method yxml_element_push_elem_back(transaction, name)
408
+ # @!method yxml_element_push_element_back(tx, name)
388
409
  #
389
- # @param [Y::Transaction] transaction
390
- # @param [String] name
410
+ # @param tx [Y::Transaction]
411
+ # @param name [String]
391
412
  # @return [Y::XMLElement]
392
413
 
393
- # @!method yxml_element_push_elem_front(transaction, name)
414
+ # @!method yxml_element_push_element_front(tx, name)
394
415
  #
395
- # @param [Y::Transaction] transaction
396
- # @param [String] name
416
+ # @param tx [Y::Transaction]
417
+ # @param name [String]
397
418
  # @return [Y::XMLElement]
398
419
 
399
- # @!method yxml_element_push_text_back(transaction)
420
+ # @!method yxml_element_push_text_back(tx, text)
400
421
  #
401
- # @param [Y::Transaction] transaction
422
+ # @param tx [Y::Transaction]
423
+ # @param text [string]
402
424
  # @return [Y::XMLText]
403
425
 
404
- # @!method yxml_element_push_text_front(transaction)
426
+ # @!method yxml_element_push_text_front(tx, text)
405
427
  #
406
- # @param [Y::Transaction] transaction
428
+ # @param tx [Y::Transaction]
429
+ # @param text [string]
407
430
  # @return [Y::XMLText]
408
431
 
409
- # @!method yxml_element_remove_attribute(transaction, name)
410
- #
411
- # @param [Y::Transaction] transaction
412
- # @param [String] name
432
+ # @!method yxml_element_remove_attribute(tx, name)
413
433
  #
434
+ # @param tx [Y::Transaction]
435
+ # @param name [String] name
414
436
  # @return [void]
415
437
 
416
- # @!method yxml_element_remove_range(transaction, index, length)
438
+ # @!method yxml_element_remove_range(tx, index, length)
417
439
  #
418
- # @param [Y::Transaction] transaction
419
- # @param [Integer] index
420
- # @param [Integer] length
440
+ # @param tx [Y::Transaction]
441
+ # @param index [Integer]
442
+ # @param length [Integer]
421
443
  #
422
444
  # @return [void]
423
445
 
424
- # @!method yxml_element_size()
446
+ # @!method yxml_element_size(tx)
425
447
  #
448
+ # @param tx [Y::Transaction]
426
449
  # @return [Integer]
427
450
 
428
- # @!method yxml_element_tag()
451
+ # @!method yxml_element_tag
429
452
  #
430
453
  # @return [String]
431
454
 
432
- # @!method yxml_element_to_s()
455
+ # @!method yxml_element_to_s(tx)
433
456
  #
457
+ # @param tx [Y::Transaction]
434
458
  # @return [String]
435
459
 
436
460
  # @!method yxml_element_unobserve(subscription_id)
437
461
  #
438
- # @param [Integer] subscription_id
462
+ # @param subscription_id [Integer]
439
463
  # @return [void]
440
-
441
- # A reference to the current active transaction of the document this element
442
- # belongs to.
443
- #
444
- # @return [Y::Transaction] A transaction object
445
- def transaction
446
- document.current_transaction
447
- end
448
464
  end
449
465
 
450
466
  # A XMLText
451
467
  #
452
468
  # Someone should not instantiate a text directly, but use
453
- # {Y::Doc#get_text_element}, {Y::XMLElement#insert_text},
469
+ # {Y::Doc#get_xml_text}, {Y::XMLElement#insert_text},
454
470
  # {Y::XMLElement#push_text}, {Y::XMLElement#unshift_text} instead.
455
471
  #
456
472
  # The XMLText API is similar to {Y::Text}, but adds a few methods to make it
@@ -469,7 +485,7 @@ module Y
469
485
 
470
486
  # Create a new XMLText instance
471
487
  #
472
- # @param [Y::Doc] doc
488
+ # @param doc [Y::Doc]
473
489
  def initialize(doc = nil)
474
490
  @document = doc || Y::Doc.new
475
491
 
@@ -478,17 +494,17 @@ module Y
478
494
 
479
495
  # Push a string to the end of the text node
480
496
  #
481
- # @param [String] str
497
+ # @param str [String]
482
498
  # @return {void}
483
499
  def <<(str)
484
- yxml_text_push(transaction, str)
500
+ document.current_transaction { |tx| yxml_text_push(tx, str) }
485
501
  end
486
502
 
487
503
  alias push <<
488
504
 
489
505
  # Attach a listener to get notified about changes
490
506
  #
491
- # @param [Proc] callback
507
+ # @param callback [Proc]
492
508
  # @return [Integer] subscription_id
493
509
  def attach(callback = nil, &block)
494
510
  yxml_text_observe(callback) unless callback.nil?
@@ -499,12 +515,12 @@ module Y
499
515
  #
500
516
  # @return [Hash]
501
517
  def attrs
502
- yxml_text_attributes
518
+ document.current_transaction { |tx| yxml_text_attributes(tx) }
503
519
  end
504
520
 
505
521
  # Detach a listener
506
522
  #
507
- # @param [Integer] subscription_id
523
+ # @param subscription_id [Integer]
508
524
  # @return [void]
509
525
  def detach(subscription_id)
510
526
  yxml_text_unobserve(subscription_id)
@@ -512,15 +528,17 @@ module Y
512
528
 
513
529
  # Format text
514
530
  #
515
- # @param [Integer] index
516
- # @param [Integer] length
517
- # @param [Hash] attrs
531
+ # @param index [Integer]
532
+ # @param length [Integer]
533
+ # @param attrs [Hash]
518
534
  # @return [void]
519
535
  def format(index, length, attrs)
520
- yxml_text_format(transaction, index, length, attrs)
536
+ document.current_transaction do |tx|
537
+ yxml_text_format(tx, index, length, attrs)
538
+ end
521
539
  end
522
540
 
523
- # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
541
+ # rubocop:disable Metrics/MethodLength
524
542
 
525
543
  # Insert a value at position and with optional attributes. This method is
526
544
  # similar to [String#insert](https://ruby-doc.org/core-3.1.2/String.html),
@@ -542,42 +560,44 @@ module Y
542
560
  # - Array (where element types must be supported)
543
561
  # - Hash (where the the types of key and values must be supported)
544
562
  #
545
- # @param [Integer] index
546
- # @param [String, Float, Array, Hash, Boolean] value
547
- # @param [Hash|nil] attrs
563
+ # @param index [Integer]
564
+ # @param value [String, Float, Integer, Array, Hash, Boolean]
565
+ # @param attrs [Hash|nil]
548
566
  # @return [void]
549
567
  def insert(index, value, attrs = nil)
550
- if value.is_a?(String)
551
- yxml_text_insert(transaction, index, value) if attrs.nil?
552
- unless attrs.nil?
553
- yxml_text_insert_with_attrs(transaction, index, value,
554
- attrs)
568
+ document.current_transaction do |tx|
569
+ if value.is_a?(String)
570
+ yxml_text_insert(tx, index, value) if attrs.nil?
571
+ unless attrs.nil?
572
+ yxml_text_insert_with_attrs(tx, index, value,
573
+ attrs)
574
+ end
575
+
576
+ return nil
555
577
  end
556
578
 
557
- return nil
558
- end
579
+ if can_insert?(value)
580
+ yxml_text_insert_embed(tx, index, value) if attrs.nil?
581
+ unless attrs.nil?
582
+ yxml_text_insert_embed_with_attrs(tx, index, value,
583
+ attrs)
584
+ end
559
585
 
560
- if can_insert?(value)
561
- yxml_text_insert_embed(transaction, index, value) if attrs.nil?
562
- unless attrs.nil?
563
- yxml_text_insert_embed_with_attrs(transaction, index, value,
564
- attrs)
586
+ return nil
565
587
  end
566
588
 
567
- return nil
589
+ raise ArgumentError,
590
+ "Can't insert value. `#{value.class.name}` isn't supported."
568
591
  end
569
-
570
- raise ArgumentError,
571
- "Can't insert value. `#{value.class.name}` isn't supported."
572
592
  end
573
593
 
574
- # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
594
+ # rubocop:enable Metrics/MethodLength
575
595
 
576
596
  # Return length of string
577
597
  #
578
598
  # @return [void]
579
599
  def length
580
- yxml_text_length
600
+ document.current_transaction { |tx| yxml_text_length(tx) }
581
601
  end
582
602
 
583
603
  alias size length
@@ -586,7 +606,7 @@ module Y
586
606
  #
587
607
  # @return [Y::XMLElement|Y::XMLText|nil]
588
608
  def next_sibling
589
- node = yxml_text_next_sibling
609
+ node = document.current_transaction { |tx| yxml_text_next_sibling(tx) }
590
610
  node.document = document
591
611
  node
592
612
  end
@@ -604,7 +624,7 @@ module Y
604
624
  #
605
625
  # @return [Y::XMLElement|Y::XMLText|nil]
606
626
  def prev_sibling
607
- node = yxml_text_prev_sibling
627
+ node = document.current_transaction { |tx| yxml_text_prev_sibling(tx) }
608
628
  node&.document = document
609
629
  node
610
630
  end
@@ -661,35 +681,37 @@ module Y
661
681
  #
662
682
  # @return [void]
663
683
  def slice!(*args)
664
- if args.size.zero?
665
- raise ArgumentError,
666
- "Provide one of `index`, `range`, `start, length` as arguments"
667
- end
684
+ document.current_transaction do |tx|
685
+ if args.size.zero?
686
+ raise ArgumentError,
687
+ "Provide one of `index`, `range`, `start, length` as arguments"
688
+ end
668
689
 
669
- if args.size == 1
670
- arg = args.first
690
+ if args.size == 1
691
+ arg = args.first
671
692
 
672
- if arg.is_a?(Range)
673
- yxml_text_remove_range(transaction, arg.first, arg.last - arg.first)
674
- return nil
675
- end
693
+ if arg.is_a?(Range)
694
+ yxml_text_remove_range(tx, arg.first, arg.last - arg.first)
695
+ return nil
696
+ end
676
697
 
677
- if arg.is_a?(Numeric)
678
- yxml_text_remove_range(transaction, arg.to_int, 1)
679
- return nil
698
+ if arg.is_a?(Numeric)
699
+ yxml_text_remove_range(tx, arg.to_int, 1)
700
+ return nil
701
+ end
680
702
  end
681
- end
682
703
 
683
- if args.size == 2
684
- first, second = args
704
+ if args.size == 2
705
+ first, second = args
685
706
 
686
- if first.is_a?(Numeric) && second.is_a?(Numeric)
687
- yxml_text_remove_range(transaction, first, second)
688
- return nil
707
+ if first.is_a?(Numeric) && second.is_a?(Numeric)
708
+ yxml_text_remove_range(tx, first, second)
709
+ return nil
710
+ end
689
711
  end
690
- end
691
712
 
692
- raise ArgumentError, "Please check your arguments, can't slice."
713
+ raise ArgumentError, "Please check your arguments, can't slice."
714
+ end
693
715
  end
694
716
 
695
717
  # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
@@ -698,7 +720,7 @@ module Y
698
720
  #
699
721
  # @return [String]
700
722
  def to_s
701
- yxml_text_to_s
723
+ document.current_transaction { |tx| yxml_text_to_s(tx) }
702
724
  end
703
725
 
704
726
  # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
@@ -723,15 +745,19 @@ module Y
723
745
  getter = getter.to_s.slice(0...-1).to_sym if is_setter
724
746
 
725
747
  define_singleton_method(setter.to_sym) do |new_val|
726
- yxml_text_insert_attribute(transaction,
727
- method_name.to_s
728
- .delete_suffix("=")
729
- .delete_prefix("attr_"),
730
- new_val)
748
+ document.current_transaction do |tx|
749
+ yxml_text_insert_attribute(tx,
750
+ method_name.to_s
751
+ .delete_suffix("=")
752
+ .delete_prefix("attr_"),
753
+ new_val)
754
+ end
731
755
  end
732
756
 
733
757
  define_singleton_method(getter) do
734
- yxml_text_get_attribute(method_name.to_s.delete_prefix("attr_"))
758
+ document.current_transaction do |tx|
759
+ yxml_text_get_attribute(tx, method_name.to_s.delete_prefix("attr_"))
760
+ end
735
761
  end
736
762
 
737
763
  if is_setter
@@ -765,105 +791,111 @@ module Y
765
791
  #
766
792
  # @return [Hash]
767
793
 
768
- # @!method yxml_text_format(transaction, index, length, attrs)
794
+ # @!method yxml_text_format(tx, index, length, attrs)
769
795
  #
770
- # @param [Integer] index
771
- # @param [Integer] length
772
- # @param [Hash] attrs
796
+ # @param tx [Y::Transaction]
797
+ # @param index [Integer]
798
+ # @param length [Integer]
799
+ # @param attrs [Hash]
773
800
  # @return [void]
774
801
 
775
- # @!method yxml_text_get_attribute(name)
802
+ # @!method yxml_text_get_attribute(tx, name)
776
803
  #
777
- # @param [String] name
804
+ # @param tx [Y::Transaction]
805
+ # @param name [String]
778
806
  # @return [String|nil]
779
807
 
780
- # @!method yxml_text_insert(transaction, index, str)
808
+ # @!method yxml_text_insert(tx, index, str)
781
809
  #
782
- # @param [Y::Transaction] transaction
783
- # @param [Integer] index
784
- # @param [String] str
810
+ # @param tx [Y::Transaction]
811
+ # @param index [Integer]
812
+ # @param str [String]
785
813
  # @return [void]
786
814
 
787
- # @!method yxml_text_insert_attribute(transaction, name, value)
815
+ # @!method yxml_text_insert_attribute(tx, name, value)
788
816
  #
789
- # @param [Y::Transaction] transaction
790
- # @param [String] name
791
- # @param [String] value
817
+ # @param tx [Y::Transaction]
818
+ # @param name [String] name
819
+ # @param value [String] value
792
820
  # @return [void]
793
821
 
794
- # @!method yxml_text_insert_with_attrs(transaction, index, value, attrs)
822
+ # @!method yxml_text_insert_with_attrs(tx, index, value, attrs)
795
823
  #
796
- # @param [Y::Transaction] transaction
797
- # @param [Integer] index
798
- # @param [String] value
799
- # @param [Hash] attrs
824
+ # @param tx [Y::Transaction]
825
+ # @param index [Integer]
826
+ # @param value [String]
827
+ # @param attrs [Hash]
800
828
  # @return [void]
801
829
 
802
- # @!method yxml_text_insert_embed(transaction, index, value)
830
+ # @!method yxml_text_insert_embed(tx, index, value)
803
831
  #
804
- # @param [Y::Transaction] transaction
805
- # @param [Integer] index
806
- # @param [String] value
832
+ # @param tx [Y::Transaction]
833
+ # @param index [Integer]
834
+ # @param value [String]
807
835
  # @return [void]
808
836
 
809
- # @!method yxml_text_insert_embed_with_attrs(txn, index, value, attrs)
837
+ # @!method yxml_text_insert_embed_with_attrs(tx, index, value, attrs)
810
838
  #
811
- # @param [Y::Transaction] txn
812
- # @param [Integer] index
813
- # @param [true|false|Float|Integer|Array|Hash] value
814
- # @param [Hash] attrs
839
+ # @param tx [Y::Transaction]
840
+ # @param index [Integer]
841
+ # @param value [true|false|Float|Integer|Array|Hash]
842
+ # @param attrs [Hash]
815
843
  # @return [void]
816
844
 
817
- # @!method yxml_text_length
845
+ # @!method yxml_text_length(tx)
818
846
  #
847
+ # @param tx [Y::Transaction]
819
848
  # @return [Integer]
820
849
 
821
- # @!method yxml_text_next_sibling
850
+ # @!method yxml_text_next_sibling(tx)
822
851
  #
852
+ # @param tx [Y::Transaction]
823
853
  # @return [Y::XMLElement|Y::XMLText|nil]
824
854
 
825
855
  # @!method yxml_text_observe(callback)
826
856
  #
827
- # @param [Proc] callback
857
+ # @param callback [Proc]
828
858
  # @return [Integer] A subscription ID
829
859
 
830
860
  # @!method yxml_text_parent
831
861
  #
832
862
  # @return [Y::XMLElement|nil]
833
863
 
834
- # @!method yxml_text_prev_sibling
864
+ # @!method yxml_text_prev_sibling(tx)
835
865
  #
866
+ # @param tx [Y::Transaction]
836
867
  # @return [Y::XMLElement|Y::XMLText|nil]
837
868
 
838
- # @!method yxml_text_push(transaction, str)
869
+ # @!method yxml_text_push(tx, str)
839
870
  #
840
- # @param [Y::Transaction] transaction
841
- # @param [String] str
871
+ # @param tx [Y::Transaction]
872
+ # @param str [String]
842
873
  # @return [void]
843
874
 
844
- # @!method yxml_text_remove_range(transaction, index, length)
875
+ # @!method yxml_text_remove_range(tx, index, length)
845
876
  #
846
- # @param [Y::Transaction] transaction
847
- # @param [Integer] index
848
- # @param [Integer] length
877
+ # @param tx [Y::Transaction]
878
+ # @param index [Integer]
879
+ # @param length [Integer]
849
880
  # @return [void]
850
881
 
851
- # @!method yxml_text_to_s()
882
+ # @!method yxml_text_to_s(tx)
852
883
  #
884
+ # @param tx [Y::Transaction]
853
885
  # @return [void]
854
886
 
855
887
  # @!method yxml_text_unobserve(subscription_id)
856
888
  #
857
- # @param [Integer] subscription_id
889
+ # @param subscription_id [Integer]
858
890
  # @return [void]
891
+ end
859
892
 
860
- # A reference to the current active transaction of the document this text
861
- # belongs to.
893
+ # @!visibility private
894
+ class XMLFragment
895
+ # @!attribute [r] document
862
896
  #
863
- # @return [Y::Transaction] A transaction object
864
- def transaction
865
- document.current_transaction
866
- end
897
+ # @return [Y::Doc] The document this array belongs to
898
+ attr_accessor :document
867
899
  end
868
900
 
869
901
  # rubocop:enable Metrics/ClassLength