y-rb 0.3.2-x64-mingw32

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,136 @@
1
+ use crate::utils::map_rhash_to_attrs;
2
+ use crate::yvalue::YValue;
3
+ use crate::{YTransaction, YXmlElement};
4
+ use lib0::any::Any;
5
+ use magnus::{Error, RHash, Value};
6
+ use std::cell::RefCell;
7
+ use yrs::{Xml, XmlText};
8
+
9
+ #[magnus::wrap(class = "Y::XMLText")]
10
+ pub(crate) struct YXmlText(pub(crate) RefCell<XmlText>);
11
+
12
+ /// SAFETY: This is safe because we only access this data when the GVL is held.
13
+ unsafe impl Send for YXmlText {}
14
+
15
+ impl YXmlText {
16
+ pub(crate) fn yxml_text_attributes(&self) -> RHash {
17
+ RHash::from_iter(self.0.borrow().attributes())
18
+ }
19
+ pub(crate) fn yxml_text_format(
20
+ &self,
21
+ transaction: &YTransaction,
22
+ index: u32,
23
+ length: u32,
24
+ attrs: RHash,
25
+ ) -> Result<(), Error> {
26
+ map_rhash_to_attrs(attrs).map(|a| {
27
+ self.0
28
+ .borrow_mut()
29
+ .format(&mut *transaction.0.borrow_mut(), index, length, a);
30
+ })
31
+ }
32
+ pub(crate) fn yxml_text_get_attribute(&self, name: String) -> Option<String> {
33
+ self.0.borrow().get_attribute(&*name)
34
+ }
35
+ pub(crate) fn yxml_text_insert(&self, transaction: &YTransaction, index: u32, content: String) {
36
+ self.0
37
+ .borrow_mut()
38
+ .insert(&mut *transaction.0.borrow_mut(), index, &*content)
39
+ }
40
+ pub(crate) fn yxml_text_insert_attribute(
41
+ &self,
42
+ transaction: &YTransaction,
43
+ name: String,
44
+ value: String,
45
+ ) {
46
+ self.0
47
+ .borrow_mut()
48
+ .insert_attribute(&mut *transaction.0.borrow_mut(), name, value)
49
+ }
50
+ pub(crate) fn yxml_text_insert_embed_with_attributes(
51
+ &self,
52
+ transaction: &YTransaction,
53
+ index: u32,
54
+ content: Value,
55
+ attrs: RHash,
56
+ ) -> Result<(), Error> {
57
+ let yvalue = YValue::from(content);
58
+ let avalue = Any::from(yvalue);
59
+
60
+ map_rhash_to_attrs(attrs).map(|a| {
61
+ self.0.borrow_mut().insert_embed_with_attributes(
62
+ &mut *transaction.0.borrow_mut(),
63
+ index,
64
+ avalue,
65
+ a,
66
+ );
67
+ })
68
+ }
69
+ pub(crate) fn yxml_text_insert_embed(
70
+ &self,
71
+ transaction: &YTransaction,
72
+ index: u32,
73
+ embed: Value,
74
+ ) {
75
+ self.0.borrow_mut().insert_embed(
76
+ &mut *transaction.0.borrow_mut(),
77
+ index,
78
+ Any::from(YValue::from(embed)),
79
+ )
80
+ }
81
+ pub(crate) fn yxml_text_insert_with_attributes(
82
+ &self,
83
+ transaction: &YTransaction,
84
+ index: u32,
85
+ content: String,
86
+ attrs: RHash,
87
+ ) -> Result<(), Error> {
88
+ map_rhash_to_attrs(attrs).map(|a| {
89
+ self.0.borrow_mut().insert_with_attributes(
90
+ &mut *transaction.0.borrow_mut(),
91
+ index,
92
+ &*content,
93
+ a,
94
+ );
95
+ })
96
+ }
97
+ pub(crate) fn yxml_text_length(&self) -> u32 {
98
+ self.0.borrow().len()
99
+ }
100
+ pub(crate) fn yxml_text_next_sibling(&self) -> Option<Value> {
101
+ self.0.borrow().next_sibling().map(|item| match item {
102
+ Xml::Element(el) => Value::from(YXmlElement(RefCell::from(el))),
103
+ Xml::Text(text) => Value::from(YXmlText(RefCell::from(text))),
104
+ })
105
+ }
106
+ pub(crate) fn yxml_text_parent(&self) -> Option<Value> {
107
+ self.0
108
+ .borrow()
109
+ .parent()
110
+ .map(|item| Value::from(YXmlElement(RefCell::from(item))))
111
+ }
112
+ pub(crate) fn yxml_text_prev_sibling(&self) -> Option<Value> {
113
+ self.0.borrow().prev_sibling().map(|item| match item {
114
+ Xml::Element(el) => Value::from(YXmlElement(RefCell::from(el))),
115
+ Xml::Text(text) => Value::from(YXmlText(RefCell::from(text))),
116
+ })
117
+ }
118
+ pub(crate) fn yxml_text_push(&self, transaction: &YTransaction, content: String) {
119
+ self.0
120
+ .borrow_mut()
121
+ .push(&mut *transaction.0.borrow_mut(), &*content)
122
+ }
123
+ pub(crate) fn yxml_text_remove_range(
124
+ &self,
125
+ transaction: &YTransaction,
126
+ index: u32,
127
+ length: u32,
128
+ ) {
129
+ self.0
130
+ .borrow_mut()
131
+ .remove_range(&mut *transaction.0.borrow_mut(), index, length);
132
+ }
133
+ pub(crate) fn yxml_text_to_s(&self) -> String {
134
+ self.0.borrow().to_string()
135
+ }
136
+ }
data/lib/2.7/yrb.so ADDED
Binary file
data/lib/3.0/yrb.so ADDED
Binary file
data/lib/y/array.rb ADDED
@@ -0,0 +1,354 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Y
4
+ # An array can be used to store and retrieve elements.
5
+ #
6
+ # The array is the replicated counterpart to a Ruby Array. It supports a
7
+ # subset of the Ruby Array operations, like adding, getting and deleting
8
+ # values by position or ranges.
9
+ #
10
+ # Someone should not instantiate an array directly, but use {Y::Doc#get_array}
11
+ # instead.
12
+ #
13
+ # @example
14
+ # doc = Y::Doc.new
15
+ # array = doc.get_array("my array")
16
+ #
17
+ # array << 1
18
+ # array.push(2)
19
+ # array.concat([3, 4, 5])
20
+ #
21
+ # array.to_a == [1, 2, 3, 4, 5] # true
22
+ class Array
23
+ include Enumerable
24
+
25
+ # @!attribute [r] document
26
+ #
27
+ # @return [Y::Doc] The document this array belongs to
28
+ attr_accessor :document
29
+
30
+ # Create a new array instance
31
+ #
32
+ # @param [Y::Doc] doc
33
+ def initialize(doc = nil)
34
+ @document = doc || Y::Doc.new
35
+
36
+ super()
37
+ end
38
+
39
+ # Retrieves element at position
40
+ #
41
+ # @return [Object]
42
+ def [](index)
43
+ yarray_get(index)
44
+ end
45
+
46
+ # Inserts value at position
47
+ #
48
+ # @param [Integer] index
49
+ # @param [true|false|Float|Integer|String|Array|Hash] value
50
+ # @return [void]
51
+ def []=(index, value)
52
+ yarray_insert(transaction, index, value)
53
+ end
54
+
55
+ # Adds an element to the end of the array
56
+ #
57
+ # @return [void]
58
+ def <<(value)
59
+ yarray_push_back(transaction, value)
60
+ end
61
+
62
+ # Attach listener to array changes
63
+ #
64
+ # @example Listen to changes in array type
65
+ # local = Y::Doc.new
66
+ #
67
+ # arr = local.get_array("my array")
68
+ # arr.attach(->(delta) { pp delta })
69
+ #
70
+ # local.transact do
71
+ # arr << 1
72
+ # end
73
+ #
74
+ # @param [Proc] callback
75
+ # @param [Block] block
76
+ # @return [Integer]
77
+ def attach(callback, &block)
78
+ return yarray_observe(callback) unless callback.nil?
79
+
80
+ yarray_observe(block.to_proc) unless block.nil?
81
+ end
82
+
83
+ # Adds to array all elements from each Array in `other_arrays`.
84
+ #
85
+ # If one of the arguments isn't an Array, it is silently ignored.
86
+ #
87
+ # @example Add multiple values to array
88
+ # doc = Y::Doc.new
89
+ # arr = doc.get_array("my array")
90
+ # arr.concat([1, 2, 3])
91
+ #
92
+ # arr.to_a == [1, 2, 3] # true
93
+ #
94
+ # @param [Array<Array<Object>>] other_arrays
95
+ # @return [void]
96
+ def concat(*other_arrays)
97
+ combined = other_arrays.reduce([]) do |values, arr|
98
+ values.concat(arr) if arr.is_a?(::Array)
99
+ end
100
+
101
+ yarray_insert_range(transaction, size, combined)
102
+ end
103
+
104
+ # Detach listener
105
+ #
106
+ # @param [Integer] subscription_id
107
+ # @return [void]
108
+ def detach(subscription_id)
109
+ yarray_unobserve(subscription_id)
110
+ end
111
+
112
+ # @return [void]
113
+ def each(&block)
114
+ yarray_each(block)
115
+ end
116
+
117
+ # Check if the array is empty
118
+ #
119
+ # @return [true|false]
120
+ def empty?
121
+ size.zero?
122
+ end
123
+
124
+ # Returns first element in array if there is at least one
125
+ #
126
+ # @return [Object|nil]
127
+ def first
128
+ yarray_get(0)
129
+ end
130
+
131
+ # Returns last element in array if there is at least one element
132
+ #
133
+ # @return [Object|nil]
134
+ def last
135
+ len = yarray_length
136
+ return yarray_get(yarray_length - 1) if len.positive?
137
+
138
+ nil
139
+ end
140
+
141
+ # rubocop:disable Naming/MethodParameterName
142
+
143
+ # Removes last (n) element(s) from array
144
+ #
145
+ # @param [Integer|nil] n Number of elements to remove
146
+ # @return [void]
147
+ def pop(n = nil)
148
+ len = size
149
+ yarray_remove(transaction, len - 1) if n.nil?
150
+ yarray_remove_range(transaction, len - n, n) unless n.nil?
151
+ end
152
+
153
+ # rubocop:enable Naming/MethodParameterName
154
+
155
+ alias push <<
156
+
157
+ # rubocop:disable Naming/MethodParameterName
158
+
159
+ # Removes first (n) element(s) from array
160
+ #
161
+ # @param [Integer|nil] n Number of elements to remove
162
+ # @return [void]
163
+ def shift(n = nil)
164
+ yarray_remove(transaction, 0) if n.nil?
165
+ yarray_remove_range(transaction, 0, n) unless nil?
166
+ end
167
+
168
+ # rubocop:enable Naming/MethodParameterName
169
+
170
+ # Size of array
171
+ #
172
+ # @return [Integer]
173
+ def size
174
+ yarray_length
175
+ end
176
+
177
+ alias length size
178
+
179
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
180
+
181
+ # Removes one or more elements from array
182
+ #
183
+ # **Attention:** In comparison to Array#slice, {Array#slice!} will not
184
+ # return the values that got removed. Even this being technically
185
+ # possible, it requires us to read the elements before removing them, which
186
+ # is not desirable in most situations.
187
+ #
188
+ # @example Removes a single element
189
+ # doc = Y::Doc.new
190
+ #
191
+ # arr = doc.get_text("my array")
192
+ # arr << 1
193
+ # arr << 2
194
+ # arr << 3
195
+ #
196
+ # arr.slice!(1)
197
+ #
198
+ # arr.to_a == [1, 3] # true
199
+ #
200
+ # @overload slice!(n)
201
+ # Removes nth element from array
202
+ #
203
+ # @overload slice!(start, length)
204
+ # Removes a range of elements
205
+ #
206
+ # @overload slice!(range)
207
+ # Removes a range of elements
208
+ #
209
+ # @return [void]
210
+ def slice!(*args)
211
+ if args.size.zero?
212
+ raise ArgumentError,
213
+ "Provide one of `index`, `range`, `start, length` as arguments"
214
+ end
215
+
216
+ if args.size == 1
217
+ arg = args.first
218
+
219
+ if arg.is_a?(Range)
220
+ if arg.exclude_end?
221
+ yarray_remove_range(transaction, arg.first,
222
+ arg.last - arg.first)
223
+ end
224
+ unless arg.exclude_end?
225
+ yarray_remove_range(transaction, arg.first,
226
+ arg.last + 1 - arg.first)
227
+ end
228
+ return nil
229
+ end
230
+
231
+ if arg.is_a?(Numeric)
232
+ yarray_remove(transaction, arg.to_int)
233
+ return nil
234
+ end
235
+ end
236
+
237
+ if args.size == 2
238
+ first, second = args
239
+
240
+ if first.is_a?(Numeric) && second.is_a?(Numeric)
241
+ yarray_remove_range(transaction, first, second)
242
+ return nil
243
+ end
244
+ end
245
+
246
+ raise ArgumentError, "Please check your arguments, can't slice."
247
+ end
248
+
249
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
250
+
251
+ # Convert this array to a Ruby Array
252
+ #
253
+ # @return [Array<Object>]
254
+ def to_a
255
+ yarray_to_a
256
+ end
257
+
258
+ # Adds an element to the beginning of the array
259
+ #
260
+ # @return [void]
261
+ def unshift(value)
262
+ yarray_push_front(transaction, value)
263
+ end
264
+
265
+ alias prepend unshift
266
+
267
+ private
268
+
269
+ # @!method yarray_each(proc)
270
+ # Iterates over all elements in Array by calling the provided proc
271
+ # with the value as argument.
272
+ #
273
+ # @param [Proc<Object>] proc A proc that is called for every element
274
+
275
+ # @!method yarray_get(index)
276
+ # Retrieves content as specified index
277
+ #
278
+ # @param [Integer] index
279
+ # @return [Object]
280
+
281
+ # @!method yarray_insert(transaction, index, content)
282
+ # Inserts content at specified index
283
+ #
284
+ # @param [Y::Transaction] transaction
285
+ # @param [Integer] index
286
+ # @param [Boolean, Float, Integer, Array, Hash, Text] content
287
+ # @return [void]
288
+
289
+ # @!method yarray_insert_range(transaction, index, arr)
290
+ # Inserts all elements of a given array at specified index
291
+ #
292
+ # @param [Y::Transaction] transaction
293
+ # @param [Integer] index
294
+ # @param [Array<Boolean, Float, Integer, Array, Hash, Text>] arr
295
+ # @return [void]
296
+
297
+ # @!method yarray_length
298
+ # Returns length of array
299
+ #
300
+ # @return [Integer] Length of array
301
+
302
+ # @!method yarray_push_back(transaction, value)
303
+ # Adds an element to the end of the array
304
+ #
305
+ # @param [Y::Transaction] transaction
306
+ # @param [Object] value
307
+ # @return [void]
308
+
309
+ # @!method yarray_push_front(transaction, value)
310
+ # Adds an element to the front of the array
311
+ #
312
+ # @param [Y::Transaction] transaction
313
+ # @param [Object] value
314
+ # @return [void]
315
+
316
+ # @!method yarray_observe(callback)
317
+ #
318
+ # @param [Proc] callback
319
+ # @return [Integer]
320
+
321
+ # @!method yarray_remove(transaction, index)
322
+ # Removes a single element from array at index
323
+ #
324
+ # @param [Y::Transaction] transaction
325
+ # @param [Integer] index
326
+ # @return [void]
327
+
328
+ # @!method yarray_remove_range(transaction, index, length)
329
+ # Removes a range of elements from array
330
+ #
331
+ # @param [Y::Transaction] transaction
332
+ # @param [Integer] index
333
+ # @param [Integer] length
334
+ # @return [void]
335
+
336
+ # @!method yarray_to_a
337
+ # Transforms the array into a Ruby array
338
+ #
339
+ # @return [Array]
340
+
341
+ # @!method yarray_unobserve(subscription_id)
342
+ #
343
+ # @param [Integer] subscription_id
344
+ # @return [void]
345
+
346
+ # A reference to the current active transaction of the document this map
347
+ # belongs to.
348
+ #
349
+ # @return [Y::Transaction] A transaction object
350
+ def transaction
351
+ document.current_transaction
352
+ end
353
+ end
354
+ end