code-ruby 1.9.6 → 1.9.7

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a95a925416d72995a702f1ee826b2acaed3783f8b5fb969ded2acce57a3b463c
4
- data.tar.gz: 83dc27edc922a93efee66d355b670ce6689e99573c6c7b26c3e2ffd84c3cbb6a
3
+ metadata.gz: 6333e864ab7337464252aefef70ff9d3bd5adbfa56a6824f26800a19e5edc776
4
+ data.tar.gz: 6556133a0bf11f1651f693d9af6edce9781f2c5b90a842fb46dbd0a45061034d
5
5
  SHA512:
6
- metadata.gz: 4085a7a81985da654973b626b01e03bc54f8348004652afb17eab037556c608fd3591b98fcbd0a944a1e1ec9206795b62f2c297f62e989eb19744f8d16fe5540
7
- data.tar.gz: 67c3d94c118ecc9f3d3f5fcce44c6295214c3b0af2309ccb933093f2002bd0a59fd9c9a6cb2fdfd4b4ffa3364c91dd862498d5411ebabe6d8cd4898e1e7e9b62
6
+ metadata.gz: a4e8152a05e0f67801282b08d1ebc4a2ed8ebbb346d15cdb976f4e201a0358658922d7f457faf6b7bfe5d9e65f7d135a3b97741dcdc8ce10706c302e0e19fa8d
7
+ data.tar.gz: f24a4f62f8eac88f50c6627f07969deeb92a3a0e95d5f4b77769aa78b28f27136c13b573db5fb46a1737064af2be2eeedc62f3a0a16ebcf28ec24bce3674dcff
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- code-ruby (1.9.6)
4
+ code-ruby (1.9.7)
5
5
  activesupport
6
6
  base64
7
7
  bigdecimal
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.9.6
1
+ 1.9.7
@@ -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
- code_context = code_context.code_lookup!(raw.first)
17
-
18
- code_context =
19
- raw[..-2].reduce(code_context) do |code_context, code_identifier|
20
- code_context.code_fetch(code_identifier)
21
- end
22
-
23
- code_context.code_set(
24
- raw.last,
25
- if code_operator.to_s == "="
26
- code_value
27
- else
28
- code_context.code_fetch(raw.last).call(
29
- **args,
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,
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
@@ -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_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.6
4
+ version: 1.9.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dorian Marié