code-ruby 0.7.5 → 0.7.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 463ae3dc79fc4e22a6822e556ffbc299483203343ede2978409e9cfff74ba406
4
- data.tar.gz: c1cf9ecfff4ea81e9e523f7c5951d8e51ded3595dae6b51c729f1d89c6d55e46
3
+ metadata.gz: d84bcfcb9ae6cbd546334c89b2aefd5a0b70bf4da537d0fe646c5f2a8030c4a3
4
+ data.tar.gz: d9894a0ee1a9c80fa1744b4b29c3b8065c6dedd310558d0f4210f252f5f330a2
5
5
  SHA512:
6
- metadata.gz: fab95b9f2f6c0e46c3ae1e7fc77b6b55198bad7354c89b35947a4c326a84b68853d9b817a5779d409a35ea79496ada6d4ad3b344ec8201cdb720d3a5b320ed86
7
- data.tar.gz: 8c4133efb5de0813fea888156341a35edf3daf458730a19080cfae9681d6c8dfa2e36d13ec87552327744f19e46775e4bbc1c73019e17860a2d08dec62ce0693
6
+ metadata.gz: f18605e6c94eccb892d45d09dddb06cf4f5fba5f2f1f30df077687812424ea97c0fde6c3c53f80bc075dfc362864307d17ef354ed5f45997c9ccc42faeb44658
7
+ data.tar.gz: 1333842f223896c4bff55a2359034420631680bfe6d2563458b1dc6546e95e7c1d50b42b577396866f3770592bc574b616aaf48d32acda843d80997a4179bb09
data/Gemfile.lock CHANGED
@@ -1,7 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- code-ruby (0.7.3)
4
+ code-ruby (0.7.6)
5
+ activesupport (~> 7)
5
6
  bigdecimal (~> 3)
6
7
  language-ruby (~> 0)
7
8
  zeitwerk (~> 2)
@@ -9,10 +10,29 @@ PATH
9
10
  GEM
10
11
  remote: https://rubygems.org/
11
12
  specs:
13
+ activesupport (7.1.3)
14
+ base64
15
+ bigdecimal
16
+ concurrent-ruby (~> 1.0, >= 1.0.2)
17
+ connection_pool (>= 2.2.5)
18
+ drb
19
+ i18n (>= 1.6, < 2)
20
+ minitest (>= 5.1)
21
+ mutex_m
22
+ tzinfo (~> 2.0)
23
+ base64 (0.2.0)
12
24
  bigdecimal (3.1.6)
25
+ concurrent-ruby (1.2.3)
26
+ connection_pool (2.4.1)
13
27
  diff-lcs (1.5.1)
28
+ drb (2.2.0)
29
+ ruby2_keywords
30
+ i18n (1.14.1)
31
+ concurrent-ruby (~> 1.0)
14
32
  language-ruby (0.6.2)
15
33
  zeitwerk (~> 2)
34
+ minitest (5.22.2)
35
+ mutex_m (0.2.0)
16
36
  rspec (3.13.0)
17
37
  rspec-core (~> 3.13.0)
18
38
  rspec-expectations (~> 3.13.0)
@@ -27,6 +47,9 @@ GEM
27
47
  rspec-support (~> 3.13.0)
28
48
  rspec-support (3.13.0)
29
49
  ruby-prof (1.7.0)
50
+ ruby2_keywords (0.0.5)
51
+ tzinfo (2.0.6)
52
+ concurrent-ruby (~> 1.0)
30
53
  zeitwerk (2.6.13)
31
54
 
32
55
  PLATFORMS
data/code-ruby.gemspec CHANGED
@@ -18,5 +18,6 @@ Gem::Specification.new do |s|
18
18
 
19
19
  s.add_dependency "bigdecimal", "~> 3"
20
20
  s.add_dependency "language-ruby", "~> 0"
21
+ s.add_dependency "activesupport", "~> 7"
21
22
  s.add_dependency "zeitwerk", "~> 2"
22
23
  end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/numeric/time"
4
+
5
+ class Code
6
+ class Object
7
+ class Duration < Object
8
+ attr_reader :raw
9
+
10
+ def initialize(duration)
11
+ @raw = duration
12
+ end
13
+
14
+ def self.name
15
+ "Duration"
16
+ end
17
+
18
+ def call(**args)
19
+ operator = args.fetch(:operator, nil)
20
+
21
+ case operator.to_s
22
+ when "from_now"
23
+ sig(args)
24
+ code_from_now
25
+ else
26
+ super
27
+ end
28
+ end
29
+
30
+ def code_from_now
31
+ Time.new(raw.from_now)
32
+ end
33
+
34
+ def inspect
35
+ to_s
36
+ end
37
+
38
+ def to_s
39
+ raw.to_s
40
+ end
41
+ end
42
+ end
43
+ end
@@ -12,7 +12,7 @@ class Code
12
12
  arguments = args.fetch(:arguments, [])
13
13
  output = args.fetch(:output)
14
14
  context = args.fetch(:context)
15
- multi_fetch(args, *GLOBALS)
15
+ globals = multi_fetch(args, *GLOBALS)
16
16
  value = arguments.first&.value
17
17
 
18
18
  case operator.to_s
@@ -28,6 +28,9 @@ class Code
28
28
  when "Dictionary"
29
29
  sig(args) { Object.maybe }
30
30
  value ? value.code_to_dictionnary : Class.new(Dictionary)
31
+ when "fetch"
32
+ sig(args) { [Object, Function.maybe] }
33
+ context.code_fetch(*arguments.map(&:value), **globals)
31
34
  when "Function"
32
35
  sig(args) { Object.maybe }
33
36
  value ? value.code_to_function : Class.new(Function)
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "active_support/core_ext/numeric/time"
4
+
3
5
  class Code
4
6
  class Object
5
7
  class Integer < Number
@@ -103,6 +105,9 @@ class Code
103
105
  when "four?"
104
106
  sig(args)
105
107
  code_four?
108
+ when "hour", "hours"
109
+ sig(args)
110
+ code_hours
106
111
  when "increment!"
107
112
  sig(args) { Integer.maybe }
108
113
  code_increment!(value)
@@ -382,6 +387,10 @@ class Code
382
387
  Boolean.new(raw.zero?)
383
388
  end
384
389
 
390
+ def code_hours
391
+ Duration.new(raw.hours)
392
+ end
393
+
385
394
  def inspect
386
395
  to_s
387
396
  end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Code
4
+ class Object
5
+ class Time < Object
6
+ attr_reader :raw
7
+
8
+ def initialize(time)
9
+ @raw = time
10
+ end
11
+
12
+ def self.name
13
+ "Time"
14
+ end
15
+
16
+ def inspect
17
+ to_s
18
+ end
19
+
20
+ def to_s
21
+ raw.to_s
22
+ end
23
+ end
24
+ end
25
+ end
data/lib/code/version.rb CHANGED
@@ -2,4 +2,4 @@
2
2
 
3
3
  require_relative "../code"
4
4
 
5
- Code::Version = Gem::Version.new("0.7.5")
5
+ Code::Version = Gem::Version.new("0.7.7")
data/lib/code-ruby.rb CHANGED
@@ -5,6 +5,7 @@ require "stringio"
5
5
  require "timeout"
6
6
  require "zeitwerk"
7
7
  require "language-ruby"
8
+ require "active_support"
8
9
 
9
10
  loader = Zeitwerk::Loader.for_gem(warn_on_extra_files: false)
10
11
  loader.ignore("#{__dir__}/code-ruby.rb")
data/spec/code_spec.rb CHANGED
@@ -159,6 +159,20 @@ RSpec.describe Code do
159
159
  )
160
160
  end
161
161
 
162
+ describe "#fetch" do
163
+ it "returns the value when present" do
164
+ expect(Code.evaluate("fetch(:downcase)", "{ downcase: 1 }")).to eq(
165
+ Code.evaluate("1")
166
+ )
167
+ end
168
+
169
+ it "returns the default value when not present" do
170
+ expect(Code.evaluate("fetch(:downcase) { 2 }")).to eq(
171
+ Code.evaluate("2")
172
+ )
173
+ end
174
+ end
175
+
162
176
  it "works with nested objects" do
163
177
  expect(
164
178
  Code.evaluate("items.first.title", ruby: { items: [{ title: "Hello" }] })
@@ -175,4 +189,8 @@ RSpec.describe Code do
175
189
  )
176
190
  ).to eq(Code.evaluate("[:Hello]"))
177
191
  end
192
+
193
+ it "works with time" do
194
+ Code.evaluate("1.hour.from_now")
195
+ end
178
196
  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: 0.7.5
4
+ version: 0.7.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dorian Marié
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '7'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '7'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: zeitwerk
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -106,6 +120,7 @@ files:
106
120
  - lib/code/object/context.rb
107
121
  - lib/code/object/decimal.rb
108
122
  - lib/code/object/dictionary.rb
123
+ - lib/code/object/duration.rb
109
124
  - lib/code/object/function.rb
110
125
  - lib/code/object/global.rb
111
126
  - lib/code/object/identifier_list.rb
@@ -116,6 +131,7 @@ files:
116
131
  - lib/code/object/range.rb
117
132
  - lib/code/object/ruby_function.rb
118
133
  - lib/code/object/string.rb
134
+ - lib/code/object/time.rb
119
135
  - lib/code/parser.rb
120
136
  - lib/code/parser/addition.rb
121
137
  - lib/code/parser/and_operator.rb