code-ruby 3.0.13 → 3.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -33,6 +33,7 @@ class Code
33
33
  code_call(
34
34
  *code_arguments.raw,
35
35
  explicit_arguments: args.fetch(:explicit_arguments, true),
36
+ bound_self: args.fetch(:bound_self, nil),
36
37
  **globals
37
38
  )
38
39
  when "extend"
@@ -64,10 +65,11 @@ class Code
64
65
  code_arguments = arguments.to_code
65
66
  code_context = Context.new({}, definition_context || globals[:context])
66
67
  code_self = bound_self.to_code
67
- code_self = captured_self if code_self.nothing? && captured_self
68
68
  code_self = Dictionary.new if code_self.nil? || code_self.nothing?
69
+ code_parent = captured_self.to_code
69
70
 
70
71
  code_context.code_set("self", code_self)
72
+ bind_parent(code_context, code_self, code_parent)
71
73
 
72
74
  if parent.is_a?(Function)
73
75
  code_context.code_set(
@@ -110,7 +112,18 @@ class Code
110
112
  code_arguments.raw[index].to_code
111
113
  end
112
114
 
113
- code_argument = code_parameter.code_default if code_argument.nothing?
115
+ if code_argument.nothing?
116
+ code_default = code_parameter.code_default
117
+ code_argument =
118
+ if code_default.is_a?(Code)
119
+ code_default.code_evaluate(
120
+ **globals,
121
+ context: code_context
122
+ )
123
+ else
124
+ code_default
125
+ end
126
+ end
114
127
 
115
128
  code_context.code_set(code_parameter.code_name, code_argument)
116
129
  end
@@ -200,6 +213,18 @@ class Code
200
213
 
201
214
  private
202
215
 
216
+ def bind_parent(code_context, code_self, code_parent)
217
+ return if code_parent.nothing?
218
+
219
+ code_context.code_set("parent", code_parent)
220
+
221
+ return unless code_self.is_a?(Dictionary)
222
+ return if code_self == code_parent
223
+ return if code_self.code_has_key?("parent").truthy?
224
+
225
+ code_self.code_set("parent", code_parent)
226
+ end
227
+
203
228
  def captured_self
204
229
  self_from(definition_context)
205
230
  end
@@ -241,7 +266,7 @@ class Code
241
266
  if code_parameter.code_default.code_to_string.raw == "nothing"
242
267
  []
243
268
  else
244
- Code.parse(code_parameter.code_default.to_s)
269
+ ::Code.parse(code_parameter.code_default.to_s)
245
270
  end
246
271
  )
247
272
  end
@@ -188,10 +188,14 @@ class Code
188
188
 
189
189
  if code_function.something?
190
190
  code_content =
191
- code_function.call(
192
- arguments: List.new([code_name, code_attributes]),
193
- **globals
194
- )
191
+ begin
192
+ code_function.call(
193
+ arguments: List.new([code_name, code_attributes]),
194
+ **globals
195
+ )
196
+ rescue Error::Next => e
197
+ e.code_value
198
+ end
195
199
 
196
200
  content =
197
201
  if code_content.is_an?(Html)
@@ -206,33 +210,52 @@ class Code
206
210
  fragment.add_child(node)
207
211
 
208
212
  Html.new(fragment)
213
+ rescue Error::Break => e
214
+ e.code_value
209
215
  end
210
216
 
211
217
  def self.code_escape(value_or_function = nil, **globals)
212
218
  code_value =
213
219
  if value_or_function.is_a?(Function)
214
- value_or_function.to_code.call(**globals)
220
+ begin
221
+ value_or_function.to_code.call(**globals)
222
+ rescue Error::Next => e
223
+ e.code_value
224
+ end
215
225
  else
216
226
  value_or_function.to_code
217
227
  end
218
228
 
219
229
  String.new(CGI.escapeHTML(code_value.to_s))
230
+ rescue Error::Break => e
231
+ e.code_value
220
232
  end
221
233
 
222
234
  def self.code_unescape(value_or_function = nil, **globals)
223
235
  code_value =
224
236
  if value_or_function.is_a?(Function)
225
- value_or_function.to_code.call(**globals)
237
+ begin
238
+ value_or_function.to_code.call(**globals)
239
+ rescue Error::Next => e
240
+ e.code_value
241
+ end
226
242
  else
227
243
  value_or_function.to_code
228
244
  end
229
245
 
230
246
  String.new(Nokogiri::HTML.fragment(code_value.to_s).text)
247
+ rescue Error::Break => e
248
+ e.code_value
231
249
  end
232
250
 
233
251
  def self.code_join(first = nil, second = nil, **globals)
234
252
  if second.is_a?(Function)
235
- code_contents = second.to_code.call(**globals)
253
+ code_contents =
254
+ begin
255
+ second.to_code.call(**globals)
256
+ rescue Error::Next => e
257
+ e.code_value
258
+ end
236
259
  code_separator = first.to_code
237
260
  else
238
261
  code_contents = first.to_code
@@ -264,12 +287,18 @@ class Code
264
287
  end
265
288
 
266
289
  Html.new(fragment)
290
+ rescue Error::Break => e
291
+ e.code_value
267
292
  end
268
293
 
269
294
  def self.code_text(value_or_function = nil, **globals)
270
295
  code_value =
271
296
  if value_or_function.is_a?(Function)
272
- value_or_function.to_code.call(**globals)
297
+ begin
298
+ value_or_function.to_code.call(**globals)
299
+ rescue Error::Next => e
300
+ e.code_value
301
+ end
273
302
  else
274
303
  value_or_function.to_code
275
304
  end
@@ -280,12 +309,18 @@ class Code
280
309
  )
281
310
 
282
311
  Html.new(fragment)
312
+ rescue Error::Break => e
313
+ e.code_value
283
314
  end
284
315
 
285
316
  def self.code_raw(value_or_function = nil, **globals)
286
317
  code_value =
287
318
  if value_or_function.is_a?(Function)
288
- value_or_function.to_code.call(**globals)
319
+ begin
320
+ value_or_function.to_code.call(**globals)
321
+ rescue Error::Next => e
322
+ e.code_value
323
+ end
289
324
  else
290
325
  value_or_function.to_code
291
326
  end
@@ -295,6 +330,8 @@ class Code
295
330
  else
296
331
  Html.new(Nokogiri::HTML::DocumentFragment.parse(code_value.to_s))
297
332
  end
333
+ rescue Error::Break => e
334
+ e.code_value
298
335
  end
299
336
 
300
337
  def call(**args)
@@ -352,6 +389,8 @@ class Code
352
389
  e.code_value
353
390
  end
354
391
  )
392
+ rescue Error::Break => e
393
+ e.code_value
355
394
  end
356
395
 
357
396
  def to_s
@@ -584,9 +584,13 @@ class Code
584
584
  arguments: List.new([Integer.new(element), self]),
585
585
  **globals
586
586
  )
587
+ rescue Error::Next => e
588
+ e.code_value
587
589
  end
588
590
 
589
591
  self
592
+ rescue Error::Break => e
593
+ e.code_value
590
594
  end
591
595
 
592
596
  def code_truncate(n = nil)
@@ -870,6 +870,8 @@ class Code
870
870
  e.code_value.tap { index += 1 }
871
871
  end
872
872
  )
873
+ rescue Error::Break => e
874
+ e.code_value
873
875
  end
874
876
 
875
877
  def code_append(other)
@@ -909,6 +911,8 @@ class Code
909
911
  rescue Error::Next => e
910
912
  e.code_value
911
913
  end || Nothing.new
914
+ rescue Error::Break => e
915
+ e.code_value
912
916
  end
913
917
 
914
918
  def code_each(argument = nil, **globals)
@@ -928,6 +932,8 @@ class Code
928
932
  end
929
933
 
930
934
  self
935
+ rescue Error::Break => e
936
+ e.code_value
931
937
  end
932
938
 
933
939
  def code_first(value = nil)
@@ -1424,6 +1430,8 @@ class Code
1424
1430
  e.code_value
1425
1431
  end
1426
1432
  )
1433
+ rescue Error::Break => e
1434
+ e.code_value
1427
1435
  end
1428
1436
 
1429
1437
  def code_map!(argument = nil, **globals)
@@ -1445,6 +1453,8 @@ class Code
1445
1453
  end
1446
1454
 
1447
1455
  self
1456
+ rescue Error::Break => e
1457
+ e.code_value
1448
1458
  end
1449
1459
 
1450
1460
  def code_max(argument = nil, **globals)
@@ -1462,6 +1472,8 @@ class Code
1462
1472
  rescue Error::Next => e
1463
1473
  e.code_value
1464
1474
  end || Nothing.new
1475
+ rescue Error::Break => e
1476
+ e.code_value
1465
1477
  end
1466
1478
 
1467
1479
  def code_none?(argument = nil, **globals)
@@ -1488,6 +1500,8 @@ class Code
1488
1500
  e.code_value.truthy?.tap { index += 1 }
1489
1501
  end
1490
1502
  )
1503
+ rescue Error::Break => e
1504
+ e.code_value
1491
1505
  end
1492
1506
 
1493
1507
  def code_all?(argument = nil, **globals)
@@ -1514,6 +1528,8 @@ class Code
1514
1528
  e.code_value.truthy?.tap { index += 1 }
1515
1529
  end
1516
1530
  )
1531
+ rescue Error::Break => e
1532
+ e.code_value
1517
1533
  end
1518
1534
 
1519
1535
  def code_reduce(argument = nil, **globals)
@@ -1536,6 +1552,8 @@ class Code
1536
1552
  rescue Error::Next => e
1537
1553
  e.code_value.tap { index += 1 }
1538
1554
  end || Nothing.new
1555
+ rescue Error::Break => e
1556
+ e.code_value
1539
1557
  end
1540
1558
 
1541
1559
  def code_reverse
@@ -1545,53 +1563,51 @@ class Code
1545
1563
  def code_compact(argument = nil, **globals)
1546
1564
  code_argument = argument.to_code
1547
1565
 
1548
- index = 0
1549
-
1550
1566
  List.new(
1551
- raw.select do |code_element|
1552
- if code_argument.is_a?(Function)
1553
- code_argument
1554
- .call(
1555
- arguments: List.new([code_element, Integer.new(index), self]),
1556
- **globals
1557
- )
1558
- .truthy?
1559
- .tap { index += 1 }
1567
+ raw.reject.with_index do |code_element, index|
1568
+ if code_argument.nothing?
1569
+ code_element.nothing?
1570
+ elsif code_argument.is_a?(Function)
1571
+ code_argument.call(
1572
+ arguments: List.new([code_element, Integer.new(index), self]),
1573
+ **globals
1574
+ ).truthy?
1560
1575
  elsif code_argument.is_a?(Class)
1561
- code_element.is_a?(code_argument.raw).tap { index += 1 }
1576
+ code_element.is_a?(code_argument.raw)
1562
1577
  else
1563
- code_element.truthy?.tap { index += 1 }
1578
+ false
1564
1579
  end
1565
1580
  rescue Error::Next => e
1566
- e.code_value.truhty?.tap { index += 1 }
1581
+ e.code_value.truthy?
1567
1582
  end
1568
1583
  )
1584
+ rescue Error::Break => e
1585
+ e.code_value
1569
1586
  end
1570
1587
 
1571
1588
  def code_compact!(argument = nil, **globals)
1572
1589
  code_argument = argument.to_code
1573
1590
 
1574
- index = 0
1575
-
1576
- raw.select! do |code_element|
1577
- if code_argument.is_a?(Function)
1578
- code_argument
1579
- .call(
1580
- arguments: List.new([code_element, Integer.new(index), self]),
1581
- **globals
1582
- )
1583
- .truthy?
1584
- .tap { index += 1 }
1591
+ raw.reject!.with_index do |code_element, index|
1592
+ if code_argument.nothing?
1593
+ code_element.nothing?
1594
+ elsif code_argument.is_a?(Function)
1595
+ code_argument.call(
1596
+ arguments: List.new([code_element, Integer.new(index), self]),
1597
+ **globals
1598
+ ).truthy?
1585
1599
  elsif code_argument.is_a?(Class)
1586
- code_element.is_a?(code_argument.raw).tap { index += 1 }
1600
+ code_element.is_a?(code_argument.raw)
1587
1601
  else
1588
- code_element.truthy?.tap { index += 1 }
1602
+ false
1589
1603
  end
1590
1604
  rescue Error::Next => e
1591
- e.code_value.truhty?.tap { index += 1 }
1605
+ e.code_value.truthy?
1592
1606
  end
1593
1607
 
1594
1608
  self
1609
+ rescue Error::Break => e
1610
+ e.code_value
1595
1611
  end
1596
1612
 
1597
1613
  def code_select(argument = nil, **globals)
@@ -1613,6 +1629,8 @@ class Code
1613
1629
  e.code_value.truthy?
1614
1630
  end
1615
1631
  )
1632
+ rescue Error::Break => e
1633
+ e.code_value
1616
1634
  end
1617
1635
 
1618
1636
  def code_select!(argument = nil, **globals)
@@ -1634,6 +1652,8 @@ class Code
1634
1652
  end
1635
1653
 
1636
1654
  self
1655
+ rescue Error::Break => e
1656
+ e.code_value
1637
1657
  end
1638
1658
 
1639
1659
  def code_reject(argument = nil, **globals)
@@ -1655,6 +1675,8 @@ class Code
1655
1675
  e.code_value.truthy?
1656
1676
  end
1657
1677
  )
1678
+ rescue Error::Break => e
1679
+ e.code_value
1658
1680
  end
1659
1681
 
1660
1682
  def code_reject!(argument = nil, **globals)
@@ -1676,6 +1698,8 @@ class Code
1676
1698
  end
1677
1699
 
1678
1700
  self
1701
+ rescue Error::Break => e
1702
+ e.code_value
1679
1703
  end
1680
1704
 
1681
1705
  def code_join(separator = nil)
@@ -1701,6 +1725,8 @@ class Code
1701
1725
  e.code_value
1702
1726
  end
1703
1727
  )
1728
+ rescue Error::Break => e
1729
+ e.code_value
1704
1730
  end
1705
1731
 
1706
1732
  def code_size
@@ -1736,6 +1762,8 @@ class Code
1736
1762
  e.code_value.tap { index += 1 }
1737
1763
  end
1738
1764
  )
1765
+ rescue Error::Break => e
1766
+ e.code_value
1739
1767
  end
1740
1768
 
1741
1769
  def code_sum
@@ -90,8 +90,12 @@ class Code
90
90
  )
91
91
  .truthy?
92
92
  .tap { index += 1 }
93
+ rescue Error::Next => e
94
+ e.code_value.truthy?.tap { index += 1 }
93
95
  end
94
96
  )
97
+ rescue Error::Break => e
98
+ e.code_value
95
99
  end
96
100
 
97
101
  def code_any?(argument, **globals)
@@ -108,8 +112,12 @@ class Code
108
112
  )
109
113
  .truthy?
110
114
  .tap { index += 1 }
115
+ rescue Error::Next => e
116
+ e.code_value.truthy?.tap { index += 1 }
111
117
  end
112
118
  )
119
+ rescue Error::Break => e
120
+ e.code_value
113
121
  end
114
122
 
115
123
  def code_each(argument, **globals)
@@ -120,9 +128,13 @@ class Code
120
128
  arguments: List.new([code_element, Integer.new(index), self]),
121
129
  **globals
122
130
  )
131
+ rescue Error::Next => e
132
+ e.code_value
123
133
  end
124
134
 
125
135
  self
136
+ rescue Error::Break => e
137
+ e.code_value
126
138
  end
127
139
 
128
140
  def code_first
@@ -142,8 +154,12 @@ class Code
142
154
  arguments: List.new([code_element, Integer.new(index), self]),
143
155
  **globals
144
156
  )
157
+ rescue Error::Next => e
158
+ e.code_value
145
159
  end
146
160
  )
161
+ rescue Error::Break => e
162
+ e.code_value
147
163
  end
148
164
 
149
165
  def code_select(argument, **globals)
@@ -155,8 +171,12 @@ class Code
155
171
  arguments: List.new([code_element, Integer.new(index), self]),
156
172
  **globals
157
173
  ).truthy?
174
+ rescue Error::Next => e
175
+ e.code_value.truthy?
158
176
  end
159
177
  )
178
+ rescue Error::Break => e
179
+ e.code_value
160
180
  end
161
181
 
162
182
  def exclude_end?
data/lib/code/parser.rb CHANGED
@@ -407,12 +407,26 @@ class Code
407
407
  skip_newlines
408
408
 
409
409
  statement = parse_expression unless operator == "loop"
410
- body = parse_body(%w[end])
410
+
411
+ if operator == "loop" &&
412
+ (match?(:punctuation, "{") || match?(:keyword, "do"))
413
+ block = parse_block(current.value)
414
+ body = block[:body]
415
+ parameters = block[:parameters]
416
+ else
417
+ body = parse_body(%w[end])
418
+ end
419
+
411
420
  skip_newlines
412
421
  advance if match?(:keyword, "end")
413
422
 
414
423
  {
415
- while: { operator: operator, statement: statement, body: body }.compact
424
+ while: {
425
+ operator: operator,
426
+ statement: statement,
427
+ parameters: parameters,
428
+ body: body
429
+ }.compact
416
430
  }
417
431
  end
418
432
 
@@ -16,6 +16,9 @@ RSpec.describe Code::Format do
16
16
  ["true || false", "true or false"],
17
17
  ["true && false", "true and false"],
18
18
  ["{a:1}", "{ a: 1 }"],
19
+ ["{ :mobility: mobility }", "{ mobility: mobility }"],
20
+ ['{ "hello": "world" }', "{ hello: :world }"],
21
+ ['{ "hello" => "world" }', "{ hello: :world }"],
19
22
  ["[1,2,3]", "[1, 2, 3]"],
20
23
  ["[1, 2, 3].select { |n| n.even? }", "[1, 2, 3].select { |n| n.even? }"],
21
24
  [
@@ -54,6 +57,10 @@ RSpec.describe Code::Format do
54
57
  "blocks << { title: \"hello world\", description: \"lorem ipsum dolor es sit\", position: 1 }",
55
58
  "blocks << {\n title: \"hello world\",\n description: \"lorem ipsum dolor es sit\",\n position: 1\n}"
56
59
  ],
60
+ [
61
+ "query = { :mobility: mobility, :unit_organization_id: unit_organization_id, :limit: limit, :offset: offset }",
62
+ "query = {\n mobility: mobility,\n unit_organization_id: unit_organization_id,\n limit: limit,\n offset: offset\n}"
63
+ ],
57
64
  [
58
65
  "sections << Html.join([Html.p { Html.b { \"{index + 1}. {title}\" } }, Html.p { query } if query.presence, Html.p { Html.a(href: link || inline_url) { :source } } if (link || inline_url), Html.p { Html.a(href: inline_url) { Html.img(src: inline_url, alt: title) } }, Html.p { Html.a(href: attachment_url) { \"télécharger\" } }].compact)",
59
66
  "sections << Html.join(\n [\n Html.p { Html.b { \"{index + 1}. {title}\" } },\n Html.p { query } if query.presence,\n Html.p {\n Html.a(href: link or inline_url) { :source }\n } if (link or inline_url),\n Html.p {\n Html.a(href: inline_url) { Html.img(src: inline_url, alt: title) }\n },\n Html.p {\n Html.a(href: attachment_url) { \"télécharger\" }\n }\n ].compact\n)"
@@ -140,7 +147,7 @@ RSpec.describe Code::Format do
140
147
 
141
148
  formatted = described_class.format(Code.parse(input))
142
149
 
143
- expect(formatted.lines.map(&:chomp).map(&:length).max).to be <= 80
150
+ expect(formatted.lines.map { |line| line.chomp.length }.max).to be <= 80
144
151
  end
145
152
  end
146
153
  end
@@ -74,6 +74,8 @@ RSpec.describe Code::Object::Dictionary do
74
74
  ["{ a: 1, b: [2, 3] }.flatten(1)", "[:a, 1, :b, [2, 3]]"],
75
75
  ["{ a: 1, b: [2, 3] }.to_list", "[[:a, 1], [:b, [2, 3]]]"],
76
76
  ["{ a: nothing }.compact", "{}"],
77
+ ["{ a: nothing, b: 1, c: false, d: \"\" }.compact", '{ b: 1, c: false, d: "" }'],
78
+ ["{ a: nothing, b: 1, c: false, d: \"\" }.compact(&:blank?)", "{ b: 1 }"],
77
79
  ["{ age: 31 }.delete_unless { |key| key == :age }", "{ age: 31 }"],
78
80
  ["{ age: 31 }.delete_unless(Integer)", "{ age: 31 }"],
79
81
  ["{ age: 31 }.keep_unless { |key| key == :age }", "{}"],