y-rb 0.4.5-x86_64-darwin → 0.4.6-x86_64-darwin

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f09c8870afffdce23d6c39e5a3a169ca1ecc8dceadbc92071a97fc7ed521d477
4
- data.tar.gz: 072e804d167a223e21b9ede5b3a770795f9a25136a9c43a64387a8a99c348190
3
+ metadata.gz: d8524eaada505c11472a6bfed19a7d90a7682a984b265aaabeb05c118515b2b8
4
+ data.tar.gz: 98c9e33d0d23a924ee29c21bd28d2046050da5097ed9963d776d74eccd7836a3
5
5
  SHA512:
6
- metadata.gz: d45cac876b5bc55c5912108a7cef7056269d68f86dfa8b5c0056fe53339a82d0c3748d6ddadaf3cdc49b375df726afb429e5b671a8fc26af0743b25777bfbe97
7
- data.tar.gz: e3a18ca611828d82bd7ab5a69d8652c9d49749618337a6f86ba2295162c2d6762e1023eb788564ca96f22ccadae77d68c1fb116414cdc84c2097f70534310174
6
+ metadata.gz: a9f7f501634b8defe5766783b9b216d30a10180eae686e75913a998ef74a7cee2b057161d038146b256abf324db589509e89fbe1c9d7fafc6b2e4a42cbed915c
7
+ data.tar.gz: eca43c92f7af63112e1d622871df292cf1fbb5188bfc078496d8d73a85a78997794b7665bfd7b964db4df2b90a5f28d458bae78bdfcadad9989f84bcbde2866f
data/ext/yrb/Cargo.toml CHANGED
@@ -1,19 +1,19 @@
1
1
  [package]
2
2
  name = "yrb"
3
- version = "0.4.5"
3
+ version = "0.4.6"
4
4
  authors = ["Hannes Moser <box@hannesmoser.at>", "Hannes Moser <hmoser@gitlab.com>"]
5
5
  edition = "2021"
6
6
  homepage = "https://github.com/y-crdt/yrb"
7
7
  repository = "https://github.com/y-crdt/yrb"
8
8
 
9
9
  [dependencies]
10
- lib0 = "0.16.1" # must match yrs version
11
- magnus = { git = "https://github.com/matsadler/magnus", rev = "ab982c3643421b38f3293f1fe014aa373abfd6dc" }
10
+ lib0 = "0.16.2" # must match yrs version
11
+ magnus = { git = "https://github.com/matsadler/magnus", rev = "2c2024920a403daadbe23fe63270440dfac86288" }
12
12
  thiserror = "1.0.38"
13
- yrs = "0.16.1"
13
+ yrs = "0.16.2"
14
14
 
15
15
  [dev-dependencies]
16
- magnus = { git = "https://github.com/matsadler/magnus", rev = "ab982c3643421b38f3293f1fe014aa373abfd6dc", features = ["embed"] }
16
+ magnus = { git = "https://github.com/matsadler/magnus", rev = "2c2024920a403daadbe23fe63270440dfac86288", features = ["embed"] }
17
17
 
18
18
  [lib]
19
19
  name = "yrb"
data/ext/yrb/src/lib.rs CHANGED
@@ -7,6 +7,7 @@ use crate::ymap::YMap;
7
7
  use crate::ytext::YText;
8
8
  use crate::ytransaction::YTransaction;
9
9
  use crate::yxml_element::YXmlElement;
10
+ use crate::yxml_fragment::YXmlFragment;
10
11
  use crate::yxml_text::YXmlText;
11
12
  use magnus::{define_module, function, method, Error, Module, Object};
12
13
 
@@ -296,6 +297,12 @@ fn init() -> Result<(), Error> {
296
297
  method!(YXmlElement::yxml_element_insert_text, 3),
297
298
  )
298
299
  .expect("cannot define private method: yxml_element_insert_text");
300
+ yxml_element
301
+ .define_private_method(
302
+ "yxml_element_len",
303
+ method!(YXmlElement::yxml_element_len, 1),
304
+ )
305
+ .expect("cannot define private method: yxml_element_len");
299
306
  yxml_element
300
307
  .define_private_method(
301
308
  "yxml_element_next_sibling",
@@ -387,6 +394,71 @@ fn init() -> Result<(), Error> {
387
394
  )
388
395
  .expect("cannot define private method: yxml_element_unobserve");
389
396
 
397
+ let yxml_fragment = module
398
+ .define_class("XMLFragment", Default::default())
399
+ .expect("cannot define class: Y::XMLFragment");
400
+
401
+ yxml_fragment
402
+ .define_private_method(
403
+ "yxml_fragment_first_child",
404
+ method!(YXmlFragment::yxml_fragment_first_child, 0),
405
+ )
406
+ .expect("cannot define private method: yxml_fragment_first_child");
407
+ yxml_fragment
408
+ .define_private_method(
409
+ "yxml_fragment_get",
410
+ method!(YXmlFragment::yxml_fragment_get, 2),
411
+ )
412
+ .expect("cannot define private method: yxml_fragment_get");
413
+ yxml_fragment
414
+ .define_private_method(
415
+ "yxml_fragment_insert",
416
+ method!(YXmlFragment::yxml_fragment_insert, 3),
417
+ )
418
+ .expect("cannot define private method: yxml_fragment_insert");
419
+ yxml_fragment
420
+ .define_private_method(
421
+ "yxml_fragment_len",
422
+ method!(YXmlFragment::yxml_fragment_len, 1),
423
+ )
424
+ .expect("cannot define private method: yxml_fragment_len");
425
+ yxml_fragment
426
+ .define_private_method(
427
+ "yxml_fragment_parent",
428
+ method!(YXmlFragment::yxml_fragment_parent, 0),
429
+ )
430
+ .expect("cannot define private method: yxml_fragment_parent");
431
+ yxml_fragment
432
+ .define_private_method(
433
+ "yxml_fragment_push_back",
434
+ method!(YXmlFragment::yxml_fragment_push_back, 2),
435
+ )
436
+ .expect("cannot define private method: yxml_fragment_push_back");
437
+ yxml_fragment
438
+ .define_private_method(
439
+ "yxml_fragment_push_front",
440
+ method!(YXmlFragment::yxml_fragment_push_front, 2),
441
+ )
442
+ .expect("cannot define private method: yxml_fragment_push_front");
443
+ yxml_fragment
444
+ .define_private_method(
445
+ "yxml_fragment_remove_range",
446
+ method!(YXmlFragment::yxml_fragment_remove_range, 3),
447
+ )
448
+ .expect("cannot define private method: yxml_fragment_remove_range");
449
+ yxml_fragment
450
+ .define_private_method(
451
+ "yxml_fragment_successors",
452
+ method!(YXmlFragment::yxml_fragment_successors, 1),
453
+ )
454
+ .expect("cannot define private method: yxml_fragment_successors");
455
+ yxml_fragment
456
+ .define_private_method(
457
+ "yxml_fragment_to_s",
458
+ method!(YXmlFragment::yxml_fragment_to_s, 1),
459
+ )
460
+ .expect("cannot define private method: yxml_fragment_to_s");
461
+
390
462
  let yxml_text = module
391
463
  .define_class("XMLText", Default::default())
392
464
  .expect("cannot define class Y::XMLText");
data/ext/yrb/src/ydoc.rs CHANGED
@@ -6,8 +6,7 @@ use crate::yxml_fragment::YXmlFragment;
6
6
  use crate::yxml_text::YXmlText;
7
7
  use crate::YTransaction;
8
8
  use magnus::block::Proc;
9
- use magnus::exception::runtime_error;
10
- use magnus::{exception, Error, Integer, RArray, Value};
9
+ use magnus::{exception::runtime_error, Error, Integer, RArray, Value};
11
10
  use std::borrow::Borrow;
12
11
  use std::cell::RefCell;
13
12
  use yrs::updates::decoder::Decode;
@@ -41,19 +40,22 @@ impl YDoc {
41
40
 
42
41
  StateVector::decode_v1(state_vector.borrow())
43
42
  .map(|sv| tx.encode_diff_v1(&sv))
44
- .map_err(|_e| Error::new(exception::runtime_error(), "cannot encode diff"))
43
+ .map_err(|_e| Error::new(runtime_error(), "cannot encode diff"))
45
44
  }
46
45
 
47
46
  pub(crate) fn ydoc_get_or_insert_array(&self, name: String) -> YArray {
48
- self.0.borrow().get_or_insert_array(name.as_str()).into()
47
+ let array_ref = self.0.borrow().get_or_insert_array(name.as_str());
48
+ YArray::from(array_ref)
49
49
  }
50
50
 
51
51
  pub(crate) fn ydoc_get_or_insert_map(&self, name: String) -> YMap {
52
- self.0.borrow().get_or_insert_map(name.as_str()).into()
52
+ let map_ref = self.0.borrow().get_or_insert_map(name.as_str());
53
+ YMap::from(map_ref)
53
54
  }
54
55
 
55
56
  pub(crate) fn ydoc_get_or_insert_text(&self, name: String) -> YText {
56
- self.0.borrow().get_or_insert_text(name.as_str()).into()
57
+ let text_ref = self.0.borrow().get_or_insert_text(name.as_str());
58
+ YText::from(text_ref)
57
59
  }
58
60
 
59
61
  pub(crate) fn ydoc_get_or_insert_xml_element(&self, name: String) -> YXmlElement {
@@ -62,14 +64,13 @@ impl YDoc {
62
64
  }
63
65
 
64
66
  pub(crate) fn ydoc_get_or_insert_xml_fragment(&self, name: String) -> YXmlFragment {
65
- self.0
66
- .borrow()
67
- .get_or_insert_xml_fragment(name.as_str())
68
- .into()
67
+ let xml_fragment_ref = self.0.borrow().get_or_insert_xml_fragment(name.as_str());
68
+ YXmlFragment::from(xml_fragment_ref)
69
69
  }
70
70
 
71
71
  pub(crate) fn ydoc_get_or_insert_xml_text(&self, name: String) -> YXmlText {
72
- self.0.borrow().get_or_insert_xml_text(name.as_str()).into()
72
+ let xml_text_ref = self.0.borrow().get_or_insert_xml_text(name.as_str());
73
+ YXmlText::from(xml_text_ref)
73
74
  }
74
75
 
75
76
  pub(crate) fn ydoc_transact<'doc>(&self) -> YTransaction {
@@ -82,6 +82,12 @@ impl YXmlElement {
82
82
 
83
83
  YXmlText::from(self.0.borrow_mut().insert(tx, index, text))
84
84
  }
85
+ pub(crate) fn yxml_element_len(&self, transaction: &YTransaction) -> u32 {
86
+ let mut tx = transaction.transaction();
87
+ let tx = tx.as_mut().unwrap();
88
+
89
+ self.0.borrow().len(tx)
90
+ }
85
91
  pub(crate) fn yxml_element_next_sibling(&self, transaction: &YTransaction) -> Option<Value> {
86
92
  let tx = transaction.transaction();
87
93
  let tx = tx.as_ref().unwrap();
@@ -1,5 +1,9 @@
1
+ use crate::ytransaction::YTransaction;
2
+ use crate::yxml_element::YXmlElement;
3
+ use crate::yxml_text::YXmlText;
4
+ use magnus::{RArray, Value};
1
5
  use std::cell::RefCell;
2
- use yrs::XmlFragmentRef;
6
+ use yrs::{GetString, XmlElementPrelim, XmlFragment, XmlFragmentRef, XmlNode};
3
7
 
4
8
  #[magnus::wrap(class = "Y::XMLFragment")]
5
9
  pub(crate) struct YXmlFragment(pub(crate) RefCell<XmlFragmentRef>);
@@ -7,7 +11,116 @@ pub(crate) struct YXmlFragment(pub(crate) RefCell<XmlFragmentRef>);
7
11
  /// SAFETY: This is safe because we only access this data when the GVL is held.
8
12
  unsafe impl Send for YXmlFragment {}
9
13
 
10
- impl YXmlFragment {}
14
+ impl YXmlFragment {
15
+ pub(crate) fn yxml_fragment_first_child(&self) -> Option<Value> {
16
+ self.0.borrow().first_child().map(|node| match node {
17
+ XmlNode::Element(element) => Value::from(YXmlElement::from(element)),
18
+ XmlNode::Fragment(fragment) => Value::from(YXmlFragment::from(fragment)),
19
+ XmlNode::Text(text) => Value::from(YXmlText::from(text)),
20
+ })
21
+ }
22
+
23
+ pub(crate) fn yxml_fragment_get(
24
+ &self,
25
+ transaction: &YTransaction,
26
+ index: u32,
27
+ ) -> Option<Value> {
28
+ let tx = transaction.transaction();
29
+ let tx = tx.as_ref().unwrap();
30
+
31
+ self.0.borrow().get(tx, index).map(|node| match node {
32
+ XmlNode::Element(element) => Value::from(YXmlElement::from(element)),
33
+ XmlNode::Fragment(fragment) => Value::from(YXmlFragment::from(fragment)),
34
+ XmlNode::Text(text) => Value::from(YXmlText::from(text)),
35
+ })
36
+ }
37
+
38
+ pub(crate) fn yxml_fragment_insert(
39
+ &self,
40
+ transaction: &YTransaction,
41
+ index: u32,
42
+ tag: String,
43
+ ) -> YXmlElement {
44
+ let mut tx = transaction.transaction();
45
+ let tx = tx.as_mut().unwrap();
46
+
47
+ let node = XmlElementPrelim::empty(tag);
48
+ YXmlElement::from(self.0.borrow_mut().insert(tx, index, node))
49
+ }
50
+
51
+ pub(crate) fn yxml_fragment_len(&self, transaction: &YTransaction) -> u32 {
52
+ let tx = transaction.transaction();
53
+ let tx = tx.as_ref().unwrap();
54
+
55
+ self.0.borrow().len(tx)
56
+ }
57
+
58
+ pub(crate) fn yxml_fragment_parent(&self) -> Option<Value> {
59
+ self.0.borrow().parent().map(|item| match item {
60
+ XmlNode::Element(el) => Value::from(YXmlElement::from(el)),
61
+ XmlNode::Fragment(fragment) => Value::from(YXmlFragment::from(fragment)),
62
+ XmlNode::Text(text) => Value::from(YXmlText::from(text)),
63
+ })
64
+ }
65
+
66
+ pub(crate) fn yxml_fragment_push_back(
67
+ &self,
68
+ transaction: &YTransaction,
69
+ tag: String,
70
+ ) -> YXmlElement {
71
+ let mut tx = transaction.transaction();
72
+ let tx = tx.as_mut().unwrap();
73
+
74
+ let node = XmlElementPrelim::empty(tag);
75
+ YXmlElement::from(self.0.borrow_mut().push_back(tx, node))
76
+ }
77
+
78
+ pub(crate) fn yxml_fragment_push_front(
79
+ &self,
80
+ transaction: &YTransaction,
81
+ tag: String,
82
+ ) -> YXmlElement {
83
+ let mut tx = transaction.transaction();
84
+ let tx = tx.as_mut().unwrap();
85
+
86
+ let node = XmlElementPrelim::empty(tag);
87
+ YXmlElement::from(self.0.borrow_mut().push_front(tx, node))
88
+ }
89
+
90
+ pub(crate) fn yxml_fragment_remove_range(
91
+ &self,
92
+ transaction: &YTransaction,
93
+ index: u32,
94
+ length: u32,
95
+ ) {
96
+ let mut tx = transaction.transaction();
97
+ let tx = tx.as_mut().unwrap();
98
+
99
+ self.0.borrow_mut().remove_range(tx, index, length);
100
+ }
101
+
102
+ pub(crate) fn yxml_fragment_successors(&self, transaction: &YTransaction) -> RArray {
103
+ let tx = transaction.transaction();
104
+ let tx = tx.as_ref().unwrap();
105
+
106
+ let fragment = self.0.borrow();
107
+
108
+ let result = fragment.successors(tx).map(|item| match item {
109
+ XmlNode::Element(el) => Value::from(YXmlElement::from(el)),
110
+ XmlNode::Fragment(fragment) => Value::from(YXmlFragment::from(fragment)),
111
+ XmlNode::Text(text) => Value::from(YXmlText::from(text)),
112
+ });
113
+
114
+ RArray::from_iter(result)
115
+ }
116
+
117
+ pub(crate) fn yxml_fragment_to_s(&self, transaction: &YTransaction) -> String {
118
+ let tx = transaction.transaction();
119
+ let tx = tx.as_ref().unwrap();
120
+
121
+ self.0.borrow().get_string(tx)
122
+ }
123
+ }
11
124
 
12
125
  impl From<XmlFragmentRef> for YXmlFragment {
13
126
  fn from(v: XmlFragmentRef) -> Self {
data/lib/2.7/yrb.bundle CHANGED
Binary file
data/lib/3.0/yrb.bundle CHANGED
Binary file
data/lib/3.1/yrb.bundle CHANGED
Binary file
data/lib/3.2/yrb.bundle CHANGED
Binary file
data/lib/y/array.rb CHANGED
@@ -56,8 +56,11 @@ module Y
56
56
  #
57
57
  # @param value [true|false|Float|Integer|String|::Array|Hash]
58
58
  # @return [void]
59
- def <<(value)
60
- document.current_transaction { |tx| yarray_push_back(tx, value) }
59
+ def <<(value, *values)
60
+ document.current_transaction do |tx|
61
+ yarray_push_back(tx, value)
62
+ values.each { |v| yarray_push_back(tx, v) }
63
+ end
61
64
  end
62
65
 
63
66
  # Attach listener to array changes
@@ -218,7 +221,7 @@ module Y
218
221
  # @return [void]
219
222
  def slice!(*args)
220
223
  document.current_transaction do |tx| # rubocop:disable Metrics/BlockLength
221
- if args.size.zero?
224
+ if args.empty?
222
225
  raise ArgumentError,
223
226
  "Provide one of `index`, `range`, `start, length` as arguments"
224
227
  end
data/lib/y/doc.rb CHANGED
@@ -221,26 +221,51 @@ module Y
221
221
  # @return [Y::Transaction] The transaction object
222
222
  # @!visibility private
223
223
 
224
+ # @!method ydoc_get_or_insert_array(name)
225
+ # Creates a new array for the document
226
+ #
227
+ # @param [String] name
228
+ # @return [Y::Array]
229
+ # @!visibility private
230
+
231
+ # @!method ydoc_get_or_insert_map(name)
232
+ # Creates a new map for the document
233
+ #
234
+ # @param [String] name
235
+ # @return [Y::Map]
236
+ # @!visibility private
237
+
238
+ # @!method ydoc_get_or_insert_text(name)
239
+ # Creates a new text for the document
240
+ #
241
+ # @param [String] name
242
+ # @return [Y::Text]
243
+ # @!visibility private
244
+
224
245
  # @!method ydoc_get_or_insert_xml_element(name)
225
246
  # Creates a new XMLText for the document
226
247
  #
248
+ # @param [String] name
227
249
  # @return [Y::XMLElement]
228
250
  # @!visibility private
229
251
 
230
252
  # @!method ydoc_get_or_insert_xml_fragment(name)
231
253
  # Creates a new XMLFragment for the document
232
254
  #
255
+ # @param [String] name
233
256
  # @return [Y::XMLFragment]
234
257
  # @!visibility private
235
258
 
236
259
  # @!method ydoc_get_or_insert_xml_text(name)
237
260
  # Creates a new XMLText for the document
238
261
  #
262
+ # @param [String] name
239
263
  # @return [Y::XMLText]
240
264
  # @!visibility private
241
265
 
242
266
  # @!method ydoc_observe_update(block)
243
267
  # Creates a subscription to observe changes to the document
268
+ #
244
269
  # @param [Proc] block
245
270
  # @return [Integer]
246
271
  # @!visibility private
data/lib/y/text.rb CHANGED
@@ -225,7 +225,7 @@ module Y
225
225
  # @return [void]
226
226
  def slice!(*args)
227
227
  document.current_transaction do |tx|
228
- if args.size.zero?
228
+ if args.empty?
229
229
  raise ArgumentError,
230
230
  "Provide one of `index`, `range`, `start, length` as arguments"
231
231
  end
data/lib/y/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Y
4
- VERSION = "0.4.5"
4
+ VERSION = "0.4.6"
5
5
  end
data/lib/y/xml.rb CHANGED
@@ -31,7 +31,7 @@ module Y
31
31
  # Retrieve node at index
32
32
  #
33
33
  # @param index [Integer]
34
- # @return [Y::XMLElement|nil]
34
+ # @return [Y::XMLElement, nil]
35
35
  def [](index)
36
36
  node = document.current_transaction { |tx| yxml_element_get(tx, index) }
37
37
  node&.document = document
@@ -76,7 +76,7 @@ module Y
76
76
  # Optional input is pushed to the text if provided
77
77
  #
78
78
  # @param index [Integer]
79
- # @param input [String|nil]
79
+ # @param input [String, nil]
80
80
  # @return [Y::XMLText]
81
81
  def insert_text(index, input = "")
82
82
  text = document.current_transaction do |tx|
@@ -88,7 +88,7 @@ module Y
88
88
 
89
89
  # Retrieve element or text adjacent (next) to this element
90
90
  #
91
- # @return [Y::XMLElement|Y::XMLText|nil]
91
+ # @return [Y::XMLElement, Y::XMLText, nil]
92
92
  def next_sibling
93
93
  node = document.current_transaction { |tx| yxml_element_next_sibling(tx) }
94
94
  node&.document = document
@@ -120,7 +120,7 @@ module Y
120
120
 
121
121
  # Retrieve parent element
122
122
  #
123
- # @return [Y::XMLElement|nil]
123
+ # @return [Y::XMLElement, nil]
124
124
  def parent
125
125
  node = yxml_element_parent
126
126
  node.document = document
@@ -129,7 +129,7 @@ module Y
129
129
 
130
130
  # Retrieve element or text adjacent (previous) to this element
131
131
  #
132
- # @return [Y::XMLElement|Y::XMLText|nil]
132
+ # @return [Y::XMLElement, Y::XMLText, nil]
133
133
  def prev_sibling
134
134
  node = document.current_transaction { |tx| yxml_element_prev_sibling(tx) }
135
135
  node&.document = document
@@ -199,7 +199,7 @@ module Y
199
199
  # @return [void]
200
200
  def slice!(*args)
201
201
  document.current_transaction do |tx| # rubocop:disable Metrics/BlockLength
202
- if args.size.zero?
202
+ if args.empty?
203
203
  raise ArgumentError,
204
204
  "Provide one of `index`, `range`, `start, length` as arguments"
205
205
  end
@@ -349,26 +349,26 @@ module Y
349
349
  # @!method yxml_element_first_child(tx)
350
350
  #
351
351
  # @param tx [Y::Transaction]
352
- # @return [Y::XMLElement|Y::XMLText]
352
+ # @return [Y::XMLElement, Y::XMLText]
353
353
 
354
354
  # @!method yxml_element_get_attribute(tx, name)
355
355
  #
356
356
  # @param tx [Y::Transaction]
357
357
  # @param name [String]
358
- # @return [String|nil]
358
+ # @return [String, nil]
359
359
 
360
360
  # @!method yxml_element_get(tx, index)
361
361
  #
362
362
  # @param tx [Y::Transaction]
363
363
  # @param index [Integer]
364
- # @return [Y::XMLElement|Y::XMLText|nil]
364
+ # @return [Y::XMLElement, Y::XMLText, nil]
365
365
 
366
366
  # @!method yxml_element_insert_attribute(tx, name, value)
367
367
  #
368
368
  # @param tx [Y::Transaction]
369
369
  # @param name [String]
370
370
  # @param value [String]
371
- # @return [String|nil]
371
+ # @return [String, nil]
372
372
 
373
373
  # @!method yxml_element_insert_element(tx, index, name)
374
374
  # Insert XML element into this XML element
@@ -389,7 +389,7 @@ module Y
389
389
  # @!method yxml_element_next_sibling(tx)
390
390
  #
391
391
  # @param tx [Y::Transaction]
392
- # @return [Y::XMLElement|XMLText|nil]
392
+ # @return [Y::XMLElement, XMLText, nil]
393
393
 
394
394
  # @!method yxml_element_observe(callback)
395
395
  #
@@ -398,12 +398,12 @@ module Y
398
398
 
399
399
  # @!method yxml_element_parent()
400
400
  #
401
- # @return [Y::XMLElement|nil]
401
+ # @return [Y::XMLElement, nil]
402
402
 
403
403
  # @!method yxml_element_prev_sibling(tx)
404
404
  #
405
405
  # @param tx [Y::Transaction]
406
- # @return [Y::XMLElement|XMLText|nil]
406
+ # @return [Y::XMLElement, XMLText, nil]
407
407
 
408
408
  # @!method yxml_element_push_element_back(tx, name)
409
409
  #
@@ -562,7 +562,7 @@ module Y
562
562
  #
563
563
  # @param index [Integer]
564
564
  # @param value [String, Float, Integer, Array, Hash, Boolean]
565
- # @param attrs [Hash|nil]
565
+ # @param attrs [Hash, nil]
566
566
  # @return [void]
567
567
  def insert(index, value, attrs = nil)
568
568
  document.current_transaction do |tx|
@@ -604,7 +604,7 @@ module Y
604
604
 
605
605
  # Return adjacent XMLElement or XMLText node (next)
606
606
  #
607
- # @return [Y::XMLElement|Y::XMLText|nil]
607
+ # @return [Y::XMLElement, Y::XMLText, nil]
608
608
  def next_sibling
609
609
  node = document.current_transaction { |tx| yxml_text_next_sibling(tx) }
610
610
  node.document = document
@@ -613,7 +613,7 @@ module Y
613
613
 
614
614
  # Return parent XMLElement
615
615
  #
616
- # @return [Y::XMLElement|nil]
616
+ # @return [Y::XMLElement, nil]
617
617
  def parent
618
618
  node = yxml_text_parent
619
619
  node.document = document
@@ -622,7 +622,7 @@ module Y
622
622
 
623
623
  # Return adjacent XMLElement or XMLText node (prev)
624
624
  #
625
- # @return [Y::XMLElement|Y::XMLText|nil]
625
+ # @return [Y::XMLElement, Y::XMLText, nil]
626
626
  def prev_sibling
627
627
  node = document.current_transaction { |tx| yxml_text_prev_sibling(tx) }
628
628
  node&.document = document
@@ -682,7 +682,7 @@ module Y
682
682
  # @return [void]
683
683
  def slice!(*args)
684
684
  document.current_transaction do |tx|
685
- if args.size.zero?
685
+ if args.empty?
686
686
  raise ArgumentError,
687
687
  "Provide one of `index`, `range`, `start, length` as arguments"
688
688
  end
@@ -742,7 +742,7 @@ module Y
742
742
  setter = method_name
743
743
  setter += "=" unless is_setter
744
744
  getter = method_name
745
- getter = getter.to_s.slice(0...-1).to_sym if is_setter
745
+ getter = getter.to_s.slice(0...-1)&.to_sym if is_setter
746
746
 
747
747
  define_singleton_method(setter.to_sym) do |new_val|
748
748
  document.current_transaction do |tx|
@@ -803,7 +803,7 @@ module Y
803
803
  #
804
804
  # @param tx [Y::Transaction]
805
805
  # @param name [String]
806
- # @return [String|nil]
806
+ # @return [String, nil]
807
807
 
808
808
  # @!method yxml_text_insert(tx, index, str)
809
809
  #
@@ -838,7 +838,7 @@ module Y
838
838
  #
839
839
  # @param tx [Y::Transaction]
840
840
  # @param index [Integer]
841
- # @param value [true|false|Float|Integer|Array|Hash]
841
+ # @param value [true, false, Float, Integer, Array, Hash]
842
842
  # @param attrs [Hash]
843
843
  # @return [void]
844
844
 
@@ -850,7 +850,7 @@ module Y
850
850
  # @!method yxml_text_next_sibling(tx)
851
851
  #
852
852
  # @param tx [Y::Transaction]
853
- # @return [Y::XMLElement|Y::XMLText|nil]
853
+ # @return [Y::XMLElement, Y::XMLText, nil]
854
854
 
855
855
  # @!method yxml_text_observe(callback)
856
856
  #
@@ -859,12 +859,12 @@ module Y
859
859
 
860
860
  # @!method yxml_text_parent
861
861
  #
862
- # @return [Y::XMLElement|nil]
862
+ # @return [Y::XMLElement, nil]
863
863
 
864
864
  # @!method yxml_text_prev_sibling(tx)
865
865
  #
866
866
  # @param tx [Y::Transaction]
867
- # @return [Y::XMLElement|Y::XMLText|nil]
867
+ # @return [Y::XMLElement, Y::XMLText, nil]
868
868
 
869
869
  # @!method yxml_text_push(tx, str)
870
870
  #
@@ -896,6 +896,245 @@ module Y
896
896
  #
897
897
  # @return [Y::Doc] The document this array belongs to
898
898
  attr_accessor :document
899
+
900
+ # Create a new XMLElement instance
901
+ #
902
+ # @param doc [Y::Doc]
903
+ def initialize(doc = nil)
904
+ @document = doc || Y::Doc.new
905
+
906
+ super()
907
+ end
908
+
909
+ # Retrieve node at index
910
+ #
911
+ # @param index [Integer]
912
+ # @return [Y::XMLElement, Y::XMLFragment, Y::XMLText, nil]
913
+ def [](index)
914
+ node = document.current_transaction { |tx| yxml_fragment_get(tx, index) }
915
+ node&.document = document
916
+ node
917
+ end
918
+
919
+ # Create a node at index
920
+ #
921
+ # @param index [Integer]
922
+ # @param name [String] Name of node, e.g. `<p />`
923
+ # @return [Y::XMLElement]
924
+ # rubocop:disable Lint/Void
925
+ def []=(index, name)
926
+ node = document.current_transaction do |tx|
927
+ yxml_fragment_insert(tx, index, name)
928
+ end
929
+ node.document = document
930
+ node
931
+ end
932
+ # rubocop:enable Lint/Void
933
+
934
+ # Retrieve first child
935
+ #
936
+ # @return [Y::XMLElement, Y::XMLFragment, Y::XMLText, nil]
937
+ def first_child
938
+ node = yxml_fragment_first_child
939
+ node&.document = document
940
+ node
941
+ end
942
+
943
+ # Retrieve child at index
944
+ #
945
+ # @param [Integer] index
946
+ # @param [String] tag
947
+ # @return [Y::XMLElement, Y::XMLFragment, Y::XMLText, nil]
948
+ def insert(index, tag)
949
+ document.current_transaction { |tx| yxml_fragment_insert(tx, index, tag) }
950
+ end
951
+
952
+ # Length of the fragment
953
+ #
954
+ # @return [Integer]
955
+ def length
956
+ document.current_transaction { |tx| yxml_fragment_len(tx) }
957
+ end
958
+
959
+ alias size length
960
+
961
+ # Retrieve parent element
962
+ #
963
+ # @return [Y::XMLElement, Y::XMLFragment, Y::XMLText, nil]
964
+ def parent
965
+ node = yxml_fragment_parent
966
+ node.document = document
967
+ node
968
+ end
969
+
970
+ # Creates a new child an inserts at the end of the children list
971
+ #
972
+ # @param name [String]
973
+ # @return [Y::XMLElement]
974
+ def <<(name)
975
+ xml_element = document.current_transaction do |tx|
976
+ yxml_fragment_push_back(tx, name)
977
+ end
978
+ xml_element.document = document
979
+ xml_element
980
+ end
981
+
982
+ alias push <<
983
+
984
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
985
+
986
+ # Removes one or more children from XML Fragment
987
+ #
988
+ # @example Removes a single element
989
+ # doc = Y::Doc.new
990
+ #
991
+ # xml_fragment = doc.get_xml_fragment("my xml fragment")
992
+ # xml_fragment << "A"
993
+ # xml_fragment << "B"
994
+ # xml_fragment << "C"
995
+ #
996
+ # xml_fragment.slice!(1)
997
+ #
998
+ # xml_fragment.to_s # <UNDEFINED><A></A><C></C></UNDEFINED>
999
+ #
1000
+ # @overload slice!(n)
1001
+ # Removes nth node from child list
1002
+ #
1003
+ # @overload slice!(start, length)
1004
+ # Removes a range of nodes
1005
+ #
1006
+ # @overload slice!(range)
1007
+ # Removes a range of nodes
1008
+ #
1009
+ # @return [void]
1010
+ def slice!(*args)
1011
+ document.current_transaction do |tx| # rubocop:disable Metrics/BlockLength
1012
+ if args.empty?
1013
+ raise ArgumentError,
1014
+ "Provide one of `index`, `range`, `start, length` as arguments"
1015
+ end
1016
+
1017
+ if args.size == 1
1018
+ arg = args.first
1019
+
1020
+ if arg.is_a?(Range)
1021
+ if arg.exclude_end?
1022
+ yxml_fragment_remove_range(tx, arg.first,
1023
+ arg.last - arg.first)
1024
+ end
1025
+ unless arg.exclude_end?
1026
+ yxml_fragment_remove_range(tx, arg.first,
1027
+ arg.last + 1 - arg.first)
1028
+ end
1029
+ return nil
1030
+ end
1031
+
1032
+ if arg.is_a?(Numeric)
1033
+ yxml_fragment_remove_range(tx, arg.to_int, 1)
1034
+ return nil
1035
+ end
1036
+ end
1037
+
1038
+ if args.size == 2
1039
+ first, second = args
1040
+
1041
+ if first.is_a?(Numeric) && second.is_a?(Numeric)
1042
+ yxml_fragment_remove_range(tx, first, second)
1043
+ return nil
1044
+ end
1045
+ end
1046
+
1047
+ raise ArgumentError, "Please check your arguments, can't slice."
1048
+ end
1049
+ end
1050
+
1051
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
1052
+
1053
+ # Traverse over the successors of the current XML element and return
1054
+ # a flat representation of all successors.
1055
+ #
1056
+ # @return [Array<Y::XMLElement, Y::XMLFragment, Y::XMLText]
1057
+ def successors
1058
+ document.current_transaction { |tx| yxml_fragment_successors(tx) }
1059
+ end
1060
+
1061
+ # Returns string representation of XMLFragment
1062
+ #
1063
+ # @return [String]
1064
+ def to_s
1065
+ document.current_transaction { |tx| yxml_fragment_to_s(tx) }
1066
+ end
1067
+
1068
+ # Creates a new node and puts it in front of the child list
1069
+ #
1070
+ # @param name [String]
1071
+ # @return [Y::XMLElement]
1072
+ def unshift(name)
1073
+ xml_element = document.current_transaction do |tx|
1074
+ yxml_fragment_push_front(tx, name)
1075
+ end
1076
+ xml_element.document = document
1077
+ xml_element
1078
+ end
1079
+
1080
+ # @!method yxml_fragment_first_child
1081
+ #
1082
+ # @return [Y::XMLElement, Y::XMLFragment, Y::XMLText, nil]
1083
+
1084
+ # @!method yxml_fragment_get(tx, index)
1085
+ #
1086
+ # @param [Y::Transaction] tx
1087
+ # @param [Integer] index
1088
+ # @return [Y::XMLElement, Y::XMLFragment, Y::XMLText, nil]
1089
+
1090
+ # @!method yxml_fragment_insert(tx, index, tag)
1091
+ #
1092
+ # @param tx [Y::Transaction]
1093
+ # @param [Integer] index
1094
+ # @param [String] tag
1095
+ # @return [Y::XMLElement, Y::XMLFragment, Y::XMLText, nil]
1096
+
1097
+ # @!method yxml_fragment_len(tx)
1098
+ #
1099
+ # @param tx [Y::Transaction]
1100
+ # @return [Integer]
1101
+
1102
+ # @!method yxml_fragment_parent
1103
+ #
1104
+ # @return [Y::XMLElement, Y::XMLFragment, Y::XMLText, nil]
1105
+
1106
+ # @!method yxml_fragment_push_back(tx, tag)
1107
+ #
1108
+ # @param tx [Y::Transaction]
1109
+ # @param [String] tag
1110
+ # @return [Y::XMLElement]
1111
+
1112
+ # @!method yxml_fragment_push_front(tx, tag)
1113
+ #
1114
+ # @param tx [Y::Transaction]
1115
+ # @param [String] tag
1116
+ # @return [Y::XMLElement]
1117
+
1118
+ # @!method yxml_fragment_remove(tx, index)
1119
+ #
1120
+ # @param tx [Y::Transaction]
1121
+ # @param [Integer] index
1122
+
1123
+ # @!method yxml_fragment_remove_range(tx, index, length)
1124
+ #
1125
+ # @param tx [Y::Transaction]
1126
+ # @param [Integer] index
1127
+ # @param [Integer] length
1128
+
1129
+ # @!method yxml_fragment_successors(tx)
1130
+ #
1131
+ # @param tx [Y::Transaction]
1132
+ # @return [Array<Y::XMLElement, Y::XMLFragment, Y::XMLText>]
1133
+
1134
+ # @!method yxml_fragment_to_s(tx)
1135
+ #
1136
+ # @param tx [Y::Transaction]
1137
+ # @return [String]
899
1138
  end
900
1139
 
901
1140
  # rubocop:enable Metrics/ClassLength
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: y-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.5
4
+ version: 0.4.6
5
5
  platform: x86_64-darwin
6
6
  authors:
7
7
  - Hannes Moser
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-02-01 00:00:00.000000000 Z
11
+ date: 2023-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake