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/text.rb CHANGED
@@ -25,7 +25,7 @@ module Y
25
25
 
26
26
  # Create a new text instance
27
27
  #
28
- # @param [Y::Doc] doc
28
+ # @param doc [Y::Doc]
29
29
  def initialize(doc = nil)
30
30
  @document = doc || Y::Doc.new
31
31
 
@@ -34,9 +34,10 @@ module Y
34
34
 
35
35
  # Appends a string at the end of the text
36
36
  #
37
+ # @param str [String]
37
38
  # @return [void]
38
39
  def <<(str)
39
- ytext_push(transaction, str)
40
+ document.current_transaction { |tx| ytext_push(tx, str) }
40
41
  end
41
42
 
42
43
  # Attach listener to text changes
@@ -62,8 +63,8 @@ module Y
62
63
  # # todo: required, otherwise segfault
63
64
  # local.commit
64
65
  #
65
- # @param [Proc] callback
66
- # @param [Block] block
66
+ # @param callback [Proc]
67
+ # @param block [Block]
67
68
  # @return [Integer]
68
69
  def attach(callback, &block)
69
70
  return ytext_observe(callback) unless callback.nil?
@@ -73,7 +74,7 @@ module Y
73
74
 
74
75
  # Detach listener
75
76
  #
76
- # @param [Integer] subscription_id
77
+ # @param subscription_id [Integer]
77
78
  # @return [void]
78
79
  def detach(subscription_id)
79
80
  ytext_unobserve(subscription_id)
@@ -92,7 +93,7 @@ module Y
92
93
  length.zero?
93
94
  end
94
95
 
95
- # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
96
+ # rubocop:disable Metrics/MethodLength
96
97
 
97
98
  # Insert a value at position and with optional attributes. This method is
98
99
  # similar to [String#insert](https://ruby-doc.org/core-3.1.2/String.html),
@@ -114,32 +115,34 @@ module Y
114
115
  # - Array (where element types must be supported)
115
116
  # - Hash (where the the types of key and values must be supported)
116
117
  #
117
- # @param [Integer] index
118
- # @param [String, Numeric, Array, Hash] value
119
- # @param [Hash|nil] attrs
118
+ # @param index [Integer]
119
+ # @param value [String|Numeric|Array|Hash]
120
+ # @param attrs [Hash|nil]
120
121
  # @return [void]
121
122
  def insert(index, value, attrs = nil)
122
- if value.is_a?(String)
123
- ytext_insert(transaction, index, value) if attrs.nil?
124
- unless attrs.nil?
125
- ytext_insert_with_attributes(transaction, index, value, attrs)
123
+ document.current_transaction do |tx|
124
+ if value.is_a?(String)
125
+ ytext_insert(tx, index, value) if attrs.nil?
126
+ unless attrs.nil?
127
+ ytext_insert_with_attributes(tx, index, value, attrs)
128
+ end
129
+ return nil
126
130
  end
127
- return nil
128
- end
129
131
 
130
- if can_insert?(value)
131
- ytext_insert_embed(transaction, index, value) if attrs.nil?
132
- unless attrs.nil?
133
- ytext_insert_embed_with_attributes(transaction, index, value, attrs)
132
+ if can_insert?(value)
133
+ ytext_insert_embed(tx, index, value) if attrs.nil?
134
+ unless attrs.nil?
135
+ ytext_insert_embed_with_attributes(tx, index, value, attrs)
136
+ end
137
+ return nil
134
138
  end
135
- return nil
136
- end
137
139
 
138
- raise ArgumentError,
139
- "Can't insert value. `#{value.class.name}` isn't supported."
140
+ raise ArgumentError,
141
+ "Can't insert value. `#{value.class.name}` isn't supported."
142
+ end
140
143
  end
141
144
 
142
- # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
145
+ # rubocop:enable Metrics/MethodLength
143
146
 
144
147
  # Applies formatting to text
145
148
  #
@@ -150,19 +153,21 @@ module Y
150
153
  # attrs = {format: "bold"}
151
154
  # text.format(0, 2, attrs)
152
155
  #
153
- # @param [Integer] index
154
- # @param [Integer] length
155
- # @param [Hash] attrs
156
+ # @param index [Integer]
157
+ # @param length [Integer]
158
+ # @param attrs [Hash]
156
159
  # @return [void]
157
160
  def format(index, length, attrs)
158
- ytext_format(transaction, index, length, attrs)
161
+ document.current_transaction do |tx|
162
+ ytext_format(tx, index, length, attrs)
163
+ end
159
164
  end
160
165
 
161
166
  # Returns length of text
162
167
  #
163
168
  # @return [Integer] Length of text
164
169
  def length
165
- ytext_length
170
+ document.current_transaction { |tx| ytext_length(tx) }
166
171
  end
167
172
 
168
173
  alias size length
@@ -219,35 +224,37 @@ module Y
219
224
  #
220
225
  # @return [void]
221
226
  def slice!(*args)
222
- if args.size.zero?
223
- raise ArgumentError,
224
- "Provide one of `index`, `range`, `start, length` as arguments"
225
- end
227
+ document.current_transaction do |tx|
228
+ if args.size.zero?
229
+ raise ArgumentError,
230
+ "Provide one of `index`, `range`, `start, length` as arguments"
231
+ end
226
232
 
227
- if args.size == 1
228
- arg = args.first
233
+ if args.size == 1
234
+ arg = args.first
229
235
 
230
- if arg.is_a?(Range)
231
- ytext_remove_range(transaction, arg.first, arg.last - arg.first)
232
- return nil
233
- end
236
+ if arg.is_a?(Range)
237
+ ytext_remove_range(tx, arg.first, arg.last - arg.first)
238
+ return nil
239
+ end
234
240
 
235
- if arg.is_a?(Numeric)
236
- ytext_remove_range(transaction, arg.to_int, 1)
237
- return nil
241
+ if arg.is_a?(Numeric)
242
+ ytext_remove_range(tx, arg.to_int, 1)
243
+ return nil
244
+ end
238
245
  end
239
- end
240
246
 
241
- if args.size == 2
242
- start, length = args
247
+ if args.size == 2
248
+ start, length = args
243
249
 
244
- if start.is_a?(Numeric) && length.is_a?(Numeric)
245
- ytext_remove_range(transaction, start, length)
246
- return nil
250
+ if start.is_a?(Numeric) && length.is_a?(Numeric)
251
+ ytext_remove_range(tx, start, length)
252
+ return nil
253
+ end
247
254
  end
248
- end
249
255
 
250
- raise ArgumentError, "Please check your arguments, can't slice."
256
+ raise ArgumentError, "Please check your arguments, can't slice."
257
+ end
251
258
  end
252
259
 
253
260
  # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
@@ -263,7 +270,7 @@ module Y
263
270
  #
264
271
  # @return [String]
265
272
  def to_s
266
- ytext_to_s
273
+ document.current_transaction { |tx| ytext_to_s(tx) }
267
274
  end
268
275
 
269
276
  private
@@ -277,75 +284,74 @@ module Y
277
284
  value.is_a?(Hash)
278
285
  end
279
286
 
280
- # rubocop:disable Layout/LineLength
281
-
282
- # @!method ytext_insert(transaction, index, chunk)
287
+ # @!method ytext_insert(tx, index, chunk)
283
288
  # Insert into text at position
284
289
  #
285
- # @param [Y::Transaction] transaction
286
- # @param [Integer] index
287
- # @param [String] chunk
290
+ # @param transaction [Y::Transaction]
291
+ # @param index [Integer]
292
+ # @param chunk [String]
288
293
  # @return [nil]
289
294
 
290
- # @!method ytext_insert_embed(transaction, index, content)
295
+ # @!method ytext_insert_embed(tx, index, content)
291
296
  # Insert into text at position
292
297
  #
293
- # @param [Y::Transaction] transaction
294
- # @param [Integer] index
295
- # @param [Y::Text, Y::Array, Y::Map] content
298
+ # @param tx [Y::Transaction]
299
+ # @param index [Integer]
300
+ # @param content [Y::Text|Y::Array|Y::Map]
296
301
  # @return [nil]
297
302
 
298
- # @!method ytext_insert_embed_with_attributes(transaction, index, embed, attrs)
303
+ # @!method ytext_insert_embed_with_attributes(tx, index, embed, attrs)
299
304
  # Insert into text at position
300
305
  #
301
- # @param [Y::Transaction] transaction
302
- # @param [Integer] index
303
- # @param [Y::Text, Y::Array, Y::Map] embed
304
- # @param [Hash] attrs
306
+ # @param tx [Y::Transaction]
307
+ # @param index [Integer]
308
+ # @param embed [Y::Text, Y::Array, Y::Map]
309
+ # @param attrs [Hash]
305
310
  # @return [nil]
306
311
 
307
- # @!method ytext_insert_with_attributes(transaction, index, chunk, attrs)
312
+ # @!method ytext_insert_with_attributes(tx, index, chunk, attrs)
308
313
  # Insert into text at position
309
314
  #
310
- # @param [Y::Transaction] transaction
311
- # @param [Integer] index
312
- # @param [String] chunk
313
- # @param [Hash] attrs
315
+ # @param tx [Y::Transaction]
316
+ # @param index [Integer]
317
+ # @param chunk [String]
318
+ # @param attrs [Hash]
314
319
  # @return [nil]
315
320
 
316
- # @!method ytext_push(transaction, value)
321
+ # @!method ytext_push(tx, value)
317
322
  # Returns length of text
318
323
  #
319
- # @param [Y::Transaction] transaction
320
- # @param [String] value
324
+ # @param tx [Y::Transaction]
325
+ # @param value [String]
321
326
  # @return [nil]
322
327
 
323
- # @!method ytext_remove_range(transaction, index, length)
328
+ # @!method ytext_remove_range(tx, index, length)
324
329
  # Removes a range from text
325
330
  #
326
- # @param [Y::Transaction] transaction
327
- # @param [Integer] index
328
- # @param [Integer] length
331
+ # @param tx [Y::Transaction]
332
+ # @param index [Integer]
333
+ # @param length [Integer]
329
334
  # @return [nil]
330
335
 
331
- # @!method ytext_format(transaction, index, length, attrs)
336
+ # @!method ytext_format(tx, index, length, attrs)
332
337
  # Formats a text range
333
338
  #
334
- # @param [Y::Transaction] transaction
335
- # @param [Integer] index
336
- # @param [Integer] length
337
- # @param [Hash] attrs
339
+ # @param tx [Y::Transaction]
340
+ # @param index [Integer]
341
+ # @param length [Integer]
342
+ # @param attrs [Hash]
338
343
  # @return [nil]
339
344
 
340
- # @!method ytext_length()
345
+ # @!method ytext_length(tx)
341
346
  # Returns length of text
342
347
  #
348
+ # @param tx [Y::Transaction]
343
349
  # @return [Integer]
344
350
 
345
351
  # @!method ytext_observe(proc)
346
352
  # Observe text changes
347
353
  #
348
- # @param [Proc] proc
354
+ # @param proc [Proc]
349
355
  # @return [Integer]
350
356
 
351
357
  # @!method ytext_to_s()
@@ -356,17 +362,7 @@ module Y
356
362
  # @!method ytext_unobserve(subscription_id)
357
363
  # Detach listener
358
364
  #
359
- # @param [Integer] subscription_id
365
+ # @param subscription_id [Integer]
360
366
  # @return [void]
361
-
362
- # rubocop:enable Layout/LineLength
363
-
364
- # A reference to the current active transaction of the document this map
365
- # belongs to.
366
- #
367
- # @return [Y::Transaction] A transaction object
368
- def transaction
369
- document.current_transaction
370
- end
371
367
  end
372
368
  end
data/lib/y/transaction.rb CHANGED
@@ -13,10 +13,10 @@ module Y
13
13
  super()
14
14
  end
15
15
 
16
- # Applies the binary encoded update for this document. This will bring the
16
+ # Applies the encoded update on this document. This will bring the
17
17
  # the document to the same state as the one the update is from.
18
18
  #
19
- # @param [::Array<Integer>] update
19
+ # @param update [::Array<Integer>]
20
20
  # @return [void]
21
21
  def apply(update)
22
22
  ytransaction_apply_update(update)
@@ -31,7 +31,7 @@ module Y
31
31
 
32
32
  # Create or get array type
33
33
  #
34
- # @param [String] name
34
+ # @param name [String]
35
35
  # @return [Y::Array]
36
36
  def get_array(name)
37
37
  array = ytransaction_get_array(name)
@@ -41,7 +41,7 @@ module Y
41
41
 
42
42
  # Create or get map type
43
43
  #
44
- # @param [String] name
44
+ # @param name [String]
45
45
  # @return [Y::Map]
46
46
  def get_map(name)
47
47
  map = ytransaction_get_map(name)
@@ -51,7 +51,7 @@ module Y
51
51
 
52
52
  # Create or get text type
53
53
  #
54
- # @param [String] name
54
+ # @param name [String]
55
55
  # @return [Y::Text]
56
56
  def get_text(name)
57
57
  text = ytransaction_get_text(name)
@@ -61,7 +61,7 @@ module Y
61
61
 
62
62
  # Create or get XMLElement type
63
63
  #
64
- # @param [String] name
64
+ # @param name [String]
65
65
  # @return [Y::XMLElement]
66
66
  def get_xml_element(name)
67
67
  xml_element = ytransaction_get_xml_element(name)
@@ -69,9 +69,19 @@ module Y
69
69
  xml_element
70
70
  end
71
71
 
72
+ # Create or get XMLFragment type
73
+ #
74
+ # @param name [String]
75
+ # @return [Y::XMLFragment]
76
+ def get_xml_fragment(name)
77
+ xml_fragment = ytransaction_get_xml_fragment(name)
78
+ xml_fragment.document = document
79
+ xml_fragment
80
+ end
81
+
72
82
  # Create or get XMLText type
73
83
  #
74
- # @param [String] name
84
+ # @param name [String]
75
85
  # @return [Y::XMLText]
76
86
  def get_xml_text(name)
77
87
  xml_text = ytransaction_get_xml_text(name)
@@ -89,7 +99,7 @@ module Y
89
99
  # @!method ytransaction_apply_update(update)
90
100
  # Returns or creates an array by name
91
101
  #
92
- # @param [::Array<Integer>] update
102
+ # @param update [::Array<Integer>]
93
103
  # @return [void]
94
104
  # @!visibility private
95
105
 
@@ -101,36 +111,44 @@ module Y
101
111
  # @!method ytransaction_get_array(name)
102
112
  # Returns or creates an array by name
103
113
  #
104
- # @param [String] name Name of the array structure to retrieve or create
114
+ # @param name [String] Name of the array structure to retrieve or create
105
115
  # @return [Y::Array] Array structure
106
116
  # @!visibility private
107
117
 
108
118
  # @!method ytransaction_get_map(name)
109
119
  # Returns or creates a map structure by name
110
120
  #
111
- # @param [String] name Name of the map structure to retrieve or create
121
+ # @param name [String] Name of the map structure to retrieve or create
112
122
  # @return [Y::Map] Map structure
113
123
  # @!visibility private
114
124
 
115
125
  # @!method ytransaction_get_text(name)
116
126
  # Returns or creates a text structure by name
117
127
  #
118
- # @param [String] name Name of the text structure to retrieve or create
128
+ # @param name [String] Name of the text structure to retrieve or create
119
129
  # @return [Y::Text] Text structure
120
130
  # @!visibility private
121
131
 
122
132
  # @!method ytransaction_get_xml_element(name)
123
133
  # Returns or creates a XML structure by name
124
134
  #
125
- # @param [String] name Name of the XML element structure to retrieve or
135
+ # @param name [String] Name of the XML element structure to retrieve or
126
136
  # create
127
137
  # @return [Y::XMLElement] XMLElement structure
128
138
  # @!visibility private
129
139
 
140
+ # @!method ytransaction_get_xml_fragment(name)
141
+ # Returns or creates a XML fragment
142
+ #
143
+ # @param name [String] Name of the XML fragment to retrieve or
144
+ # create by
145
+ # @return [Y::XMLFragment] XMLFragment structure
146
+ # @!visibility private
147
+
130
148
  # @!method ytransaction_get_xml_text(name)
131
149
  # Returns or creates a XML structure by name
132
150
  #
133
- # @param [String] name Name of the XML element structure to retrieve or
151
+ # @param name [String] Name of the XML element structure to retrieve or
134
152
  # create
135
153
  # @return [Y::XMLElement] XMLElement structure
136
154
  # @!visibility private
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.3.2"
4
+ VERSION = "0.4.0"
5
5
  end