code-ruby 1.9.6 → 1.9.8
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.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/VERSION +1 -1
- data/lib/code/format.rb +13 -8
- data/lib/code/object/date.rb +15 -0
- data/lib/code/object/identifier_list.rb +88 -21
- data/lib/code/object/time.rb +19 -0
- data/spec/code/format_spec.rb +4 -0
- data/spec/code/object/identifier_list_spec.rb +34 -0
- data/spec/code_spec.rb +7 -0
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 56fcf3ffc17f9740302bc6bd52c6aeb204eb3799883920fa35d7140829070464
|
|
4
|
+
data.tar.gz: 6e8b2521a80535fbf094771f4c105c56f6ccaef4d492dfbec9b0d51d099ab875
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: fccf82958c319089713c3e00ec779f06adb23f315bff2fd4e2af5bac36b74cf91dbff74c7a5cd7d807cb4353e043e689b372fc4f3302d7e806e48a7c6d021c0b
|
|
7
|
+
data.tar.gz: 8424f8d97d597e00c3390ace61a8038fc151415f7a87573912be0cd43f342686a2d6dac70a775a86a7033f8cd40197376e1e1a6d7eb654f1c842180f2804904e
|
data/Gemfile.lock
CHANGED
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
1.9.
|
|
1
|
+
1.9.8
|
data/lib/code/format.rb
CHANGED
|
@@ -220,30 +220,35 @@ class Code
|
|
|
220
220
|
components.flat_map do |component|
|
|
221
221
|
value = component[:value].to_s
|
|
222
222
|
if component[:type] == :code
|
|
223
|
-
[value]
|
|
223
|
+
[{ value: value, splittable: false }]
|
|
224
224
|
else
|
|
225
|
-
value
|
|
225
|
+
value
|
|
226
|
+
.split(/(\s+)/)
|
|
227
|
+
.reject(&:empty?)
|
|
228
|
+
.map { |unit| { value: unit, splittable: true } }
|
|
226
229
|
end
|
|
227
|
-
end
|
|
230
|
+
end
|
|
228
231
|
|
|
229
232
|
chunks = [""]
|
|
230
233
|
units.each do |unit|
|
|
231
|
-
|
|
234
|
+
value = unit[:value]
|
|
235
|
+
|
|
236
|
+
if unit[:splittable] && value.length > limit
|
|
232
237
|
if chunks.last.empty?
|
|
233
|
-
segments =
|
|
238
|
+
segments = value.scan(/.{1,#{limit}}/m)
|
|
234
239
|
chunks[-1] = segments.shift.to_s
|
|
235
240
|
segments.each { |segment| chunks << segment }
|
|
236
241
|
else
|
|
237
|
-
chunks <<
|
|
242
|
+
chunks << value
|
|
238
243
|
end
|
|
239
244
|
next
|
|
240
245
|
end
|
|
241
246
|
|
|
242
247
|
current = chunks.last
|
|
243
|
-
candidate = "#{current}#{
|
|
248
|
+
candidate = "#{current}#{value}"
|
|
244
249
|
if !current.empty? && candidate.length > limit
|
|
245
250
|
chunks[-1] = current
|
|
246
|
-
chunks <<
|
|
251
|
+
chunks << value
|
|
247
252
|
else
|
|
248
253
|
chunks[-1] = candidate
|
|
249
254
|
end
|
data/lib/code/object/date.rb
CHANGED
|
@@ -7,6 +7,7 @@ class Code
|
|
|
7
7
|
|
|
8
8
|
class << self
|
|
9
9
|
delegate(
|
|
10
|
+
:code_zone,
|
|
10
11
|
:code_format,
|
|
11
12
|
:code_past?,
|
|
12
13
|
:code_future?,
|
|
@@ -84,6 +85,13 @@ class Code
|
|
|
84
85
|
code_second = code_arguments.code_second
|
|
85
86
|
|
|
86
87
|
case code_operator.to_s
|
|
88
|
+
when "zone="
|
|
89
|
+
sig(args) { String }
|
|
90
|
+
::Time.zone = code_value.raw
|
|
91
|
+
code_value
|
|
92
|
+
when "zone"
|
|
93
|
+
sig(args)
|
|
94
|
+
code_zone
|
|
87
95
|
when "after?"
|
|
88
96
|
sig(args) { (Date | Time).maybe }
|
|
89
97
|
code_after?(code_value)
|
|
@@ -334,6 +342,9 @@ class Code
|
|
|
334
342
|
code_second = code_arguments.code_second
|
|
335
343
|
|
|
336
344
|
case code_operator.to_s
|
|
345
|
+
when "zone"
|
|
346
|
+
sig(args)
|
|
347
|
+
code_zone
|
|
337
348
|
when "after?"
|
|
338
349
|
sig(args) { (Date | Time).maybe }
|
|
339
350
|
code_after?(code_value)
|
|
@@ -858,6 +869,10 @@ class Code
|
|
|
858
869
|
|
|
859
870
|
Date.new(dup)
|
|
860
871
|
end
|
|
872
|
+
|
|
873
|
+
def code_zone
|
|
874
|
+
String.new(::Time.zone.name)
|
|
875
|
+
end
|
|
861
876
|
end
|
|
862
877
|
end
|
|
863
878
|
end
|
|
@@ -12,32 +12,99 @@ class Code
|
|
|
12
12
|
case code_operator.to_s
|
|
13
13
|
when /=$/
|
|
14
14
|
sig(args) { Object }
|
|
15
|
+
assignment_operator = code_operator.to_s
|
|
16
|
+
context = find_context_with_identifier(code_context, raw.first)
|
|
15
17
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
code_value
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
operator: code_operator.to_s.chop,
|
|
31
|
-
arguments: [code_value]
|
|
32
|
-
)
|
|
33
|
-
end
|
|
34
|
-
)
|
|
35
|
-
|
|
36
|
-
code_context.code_fetch(raw.last)
|
|
18
|
+
if context
|
|
19
|
+
assign_in_context(
|
|
20
|
+
context: context,
|
|
21
|
+
assignment_operator: assignment_operator,
|
|
22
|
+
code_value: code_value,
|
|
23
|
+
**args
|
|
24
|
+
)
|
|
25
|
+
else
|
|
26
|
+
assign_with_setter(
|
|
27
|
+
assignment_operator: assignment_operator,
|
|
28
|
+
code_value: code_value,
|
|
29
|
+
**args
|
|
30
|
+
)
|
|
31
|
+
end
|
|
37
32
|
else
|
|
38
33
|
super
|
|
39
34
|
end
|
|
40
35
|
end
|
|
36
|
+
|
|
37
|
+
private
|
|
38
|
+
|
|
39
|
+
def find_context_with_identifier(context, identifier)
|
|
40
|
+
current = context
|
|
41
|
+
|
|
42
|
+
while current
|
|
43
|
+
return current if current.code_has_key?(identifier).truthy?
|
|
44
|
+
|
|
45
|
+
current = current.parent
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
nil
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def assign_in_context(context:, assignment_operator:, code_value:, **args)
|
|
52
|
+
receiver =
|
|
53
|
+
raw[..-2].reduce(context) do |value, code_identifier|
|
|
54
|
+
value.code_fetch(code_identifier)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
receiver.code_set(
|
|
58
|
+
raw.last,
|
|
59
|
+
if assignment_operator == "="
|
|
60
|
+
code_value
|
|
61
|
+
else
|
|
62
|
+
receiver.code_fetch(raw.last).call(
|
|
63
|
+
**args.merge(context: args.fetch(:context, context)),
|
|
64
|
+
operator: assignment_operator.chop,
|
|
65
|
+
arguments: [code_value]
|
|
66
|
+
)
|
|
67
|
+
end
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
receiver.code_fetch(raw.last)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def assign_with_setter(assignment_operator:, code_value:, **args)
|
|
74
|
+
code_object = args.fetch(:object)
|
|
75
|
+
receiver =
|
|
76
|
+
raw[...-1].reduce(code_object) do |value, code_identifier|
|
|
77
|
+
value.call(
|
|
78
|
+
**args,
|
|
79
|
+
operator: code_identifier,
|
|
80
|
+
arguments: Object::List.new
|
|
81
|
+
)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
if assignment_operator == "="
|
|
85
|
+
receiver.call(
|
|
86
|
+
**args,
|
|
87
|
+
operator: "#{raw.last}=",
|
|
88
|
+
arguments: Object::List.new([code_value])
|
|
89
|
+
)
|
|
90
|
+
else
|
|
91
|
+
next_value = receiver.call(
|
|
92
|
+
**args,
|
|
93
|
+
operator: raw.last,
|
|
94
|
+
arguments: Object::List.new
|
|
95
|
+
).call(
|
|
96
|
+
**args,
|
|
97
|
+
operator: assignment_operator.chop,
|
|
98
|
+
arguments: Object::List.new([code_value])
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
receiver.call(
|
|
102
|
+
**args,
|
|
103
|
+
operator: "#{raw.last}=",
|
|
104
|
+
arguments: Object::List.new([next_value])
|
|
105
|
+
)
|
|
106
|
+
end
|
|
107
|
+
end
|
|
41
108
|
end
|
|
42
109
|
end
|
|
43
110
|
end
|
data/lib/code/object/time.rb
CHANGED
|
@@ -97,6 +97,14 @@ class Code
|
|
|
97
97
|
code_second = code_arguments.code_second
|
|
98
98
|
|
|
99
99
|
case code_operator.to_s
|
|
100
|
+
when "zone="
|
|
101
|
+
sig(args) { String }
|
|
102
|
+
::Time.zone = code_value.raw
|
|
103
|
+
code_value
|
|
104
|
+
when "zone"
|
|
105
|
+
sig(args)
|
|
106
|
+
::Time.zone ||= DEFAULT_ZONE
|
|
107
|
+
code_zone
|
|
100
108
|
when "after?"
|
|
101
109
|
sig(args) { (Date | Time).maybe }
|
|
102
110
|
code_after?(code_value)
|
|
@@ -370,6 +378,10 @@ class Code
|
|
|
370
378
|
end
|
|
371
379
|
end
|
|
372
380
|
|
|
381
|
+
def self.code_zone
|
|
382
|
+
String.new(::Time.zone.name)
|
|
383
|
+
end
|
|
384
|
+
|
|
373
385
|
def call(**args)
|
|
374
386
|
code_operator = args.fetch(:operator, nil).to_code
|
|
375
387
|
code_arguments = args.fetch(:arguments, []).to_code
|
|
@@ -377,6 +389,9 @@ class Code
|
|
|
377
389
|
code_second = code_arguments.code_second
|
|
378
390
|
|
|
379
391
|
case code_operator.to_s
|
|
392
|
+
when "zone"
|
|
393
|
+
sig(args)
|
|
394
|
+
code_zone
|
|
380
395
|
when "after?"
|
|
381
396
|
sig(args) { (Date | Time).maybe }
|
|
382
397
|
code_after?(code_value)
|
|
@@ -997,6 +1012,10 @@ class Code
|
|
|
997
1012
|
|
|
998
1013
|
Time.new(dup)
|
|
999
1014
|
end
|
|
1015
|
+
|
|
1016
|
+
def code_zone
|
|
1017
|
+
String.new(raw.zone)
|
|
1018
|
+
end
|
|
1000
1019
|
end
|
|
1001
1020
|
end
|
|
1002
1021
|
end
|
data/spec/code/format_spec.rb
CHANGED
|
@@ -30,6 +30,10 @@ RSpec.describe Code::Format do
|
|
|
30
30
|
[
|
|
31
31
|
"Http.post(\"https://api.openai.com/v1/chat/completions\", headers: { authorization: \"Bearer {open_ai_api_key}\", \"content-type\": \"application/json\" }, body: { model: model, messages: [{ role: \"system\", content: \"hello\" }, { role: \"user\", content: \"world\" }] }.to_json)",
|
|
32
32
|
"Http.post(\n \"https://api.openai.com/v1/chat/completions\",\n headers: {\n authorization: \"Bearer {open_ai_api_key}\",\n \"content-type\": \"application/json\"\n },\n body: {\n model: model,\n messages: [\n { role: :system, content: :hello },\n { role: :user, content: :world }\n ]\n }.to_json\n)"
|
|
33
|
+
],
|
|
34
|
+
[
|
|
35
|
+
"proxy_url = (url) => { \"{proxy_base_url}?{{ url: url, disposition: proxy_inline_disposition }.to_query}\" }",
|
|
36
|
+
"proxy_url = (url) => {\n \"{proxy_base_url}?\"\n + \"{{\n url: url,\n disposition: proxy_inline_disposition\n }.to_query}\"\n}"
|
|
33
37
|
]
|
|
34
38
|
].each do |input, expected|
|
|
35
39
|
it "formats #{input.inspect}" do
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
|
|
5
|
+
RSpec.describe Code::Object::IdentifierList do
|
|
6
|
+
it "keeps context for ||= assignments in nested context receivers" do
|
|
7
|
+
code_context = Code::Object::Context.new
|
|
8
|
+
code_identifier = Code::Object::String.new("value")
|
|
9
|
+
code_key = Code::Object::String.new("key")
|
|
10
|
+
|
|
11
|
+
code_context.code_set(
|
|
12
|
+
code_identifier,
|
|
13
|
+
Code::Object::Dictionary.new(code_key => Code::Object::Dictionary.new(a: 1))
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
identifier_list = described_class.new([code_identifier, code_key])
|
|
17
|
+
|
|
18
|
+
expect do
|
|
19
|
+
identifier_list.send(
|
|
20
|
+
:assign_in_context,
|
|
21
|
+
context: code_context,
|
|
22
|
+
assignment_operator: "||=",
|
|
23
|
+
code_value: Code::Object::Dictionary.new(b: 2),
|
|
24
|
+
arguments: Code::Object::List.new([Code::Object::Dictionary.new(b: 2)]),
|
|
25
|
+
operator: "||=",
|
|
26
|
+
error: StringIO.new,
|
|
27
|
+
input: StringIO.new,
|
|
28
|
+
object: Code::Object::Global.new,
|
|
29
|
+
output: StringIO.new,
|
|
30
|
+
source: ""
|
|
31
|
+
)
|
|
32
|
+
end.not_to raise_error
|
|
33
|
+
end
|
|
34
|
+
end
|
data/spec/code_spec.rb
CHANGED
|
@@ -464,6 +464,7 @@ RSpec.describe Code do
|
|
|
464
464
|
["subject = 1 { subject }", "{ subject: 1 }"],
|
|
465
465
|
["subject = 1 { subject: }", "{ subject: 1 }"],
|
|
466
466
|
["'{1} {2}'", "'1 2'"],
|
|
467
|
+
['Time.zone = "Etc/UTC"', '"Etc/UTC"'],
|
|
467
468
|
%w[Json.parse("1") 1],
|
|
468
469
|
%w[{a:1}.to_query "a=1"],
|
|
469
470
|
["", ""]
|
|
@@ -532,4 +533,10 @@ RSpec.describe Code do
|
|
|
532
533
|
described_class.evaluate(input)
|
|
533
534
|
described_class.evaluate(format_input(input))
|
|
534
535
|
end
|
|
536
|
+
|
|
537
|
+
it "raises for undefined constant receiver assignment" do
|
|
538
|
+
expect do
|
|
539
|
+
described_class.evaluate("UnknownConstant.zone = 1")
|
|
540
|
+
end.to raise_error(Code::Error, /UnknownConstant is not defined/)
|
|
541
|
+
end
|
|
535
542
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: code-ruby
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.9.
|
|
4
|
+
version: 1.9.8
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Dorian Marié
|
|
@@ -346,6 +346,7 @@ files:
|
|
|
346
346
|
- spec/code/object/dictionary_spec.rb
|
|
347
347
|
- spec/code/object/function_spec.rb
|
|
348
348
|
- spec/code/object/http_spec.rb
|
|
349
|
+
- spec/code/object/identifier_list_spec.rb
|
|
349
350
|
- spec/code/object/integer_spec.rb
|
|
350
351
|
- spec/code/object/list_spec.rb
|
|
351
352
|
- spec/code/object/nothing_spec.rb
|