code-ruby 0.6.6 → 0.7.0

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: 51e07a3736fa390271534aa21eb24fb5bdd5a149a06ae2b3aa3865fe3ed411d3
4
- data.tar.gz: 5b2be326d1fb68f97a6f4729aab8e639241e45496afcff0f503db104b505e111
3
+ metadata.gz: 6c1825b3354247a5986c1eb78223a6b16bff33119f38b3f8a55ee1e7b7fb354d
4
+ data.tar.gz: 125688c0383d89e4801b847665952af382326764c3bda9e8e8f52942b65ced73
5
5
  SHA512:
6
- metadata.gz: 0024632b4b4f724c1c2726cf25896cb7a09c231d689bf22359c5cf2206dbddb1c9a298ee0e9b4127c7da7a3eb860952b618477def968446048639b6e9ee36ff0
7
- data.tar.gz: 442576080a62326683fec644a9c6b5a62d0163a6d6488c7e9b85c85fd60d0b30a45fc1be99595f0f065e8dd8899bfeb853593a5e1b36fa838277f790ecff76d7
6
+ metadata.gz: 8bd85f1991b52e9848e19ac1a019716148788d348f3fbc9d98dbd625f973a6f1a46ea43a853b2b0bdf30d1c611fe12c36c0e6572f29dbac0d4339574409eb03e
7
+ data.tar.gz: f126c93f039f0c8ef0f9229d744dd63e4a996b10847756dc5c67db3ea7cf340a01aea5062e2543df56b50c316205f407bcfe68e318d69e6db0a9c595d33dc621
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- code-ruby (0.6.4)
4
+ code-ruby (0.6.7)
5
5
  bigdecimal (~> 3)
6
6
  language-ruby (~> 0)
7
7
  zeitwerk (~> 2)
data/code-ruby.gemspec CHANGED
@@ -12,7 +12,7 @@ Gem::Specification.new do |s|
12
12
  s.email = "dorian@dorianmarie.fr"
13
13
  s.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
14
14
  s.require_paths = ["lib"]
15
- s.homepage = "https://github.com/dorianmariefr/code-ruby"
15
+ s.homepage = "https://github.com/dorianmariecom/code-ruby"
16
16
  s.license = "MIT"
17
17
  s.executables = "code"
18
18
 
@@ -10,7 +10,7 @@ class Code
10
10
  def call(**args)
11
11
  operator = args.fetch(:operator, nil)
12
12
  arguments = args.fetch(:arguments, [])
13
- io = args.fetch(:io)
13
+ output = args.fetch(:output)
14
14
  context = args.fetch(:context)
15
15
  multi_fetch(args, *GLOBALS)
16
16
  value = arguments.first&.value
@@ -60,13 +60,16 @@ class Code
60
60
  Code.evaluate(value.to_s)
61
61
  when "p"
62
62
  sig(args) { Object.repeat }
63
- io.puts(*arguments.map(&:value).map(&:inspect)) || Nothing.new
63
+ output.puts(*arguments.map(&:value).map(&:inspect))
64
+ Nothing.new
64
65
  when "print"
65
66
  sig(args) { Object.repeat }
66
- io.print(*arguments.map(&:value)) || Nothing.new
67
+ output.print(*arguments.map(&:value))
68
+ Nothing.new
67
69
  when "puts"
68
70
  sig(args) { Object.repeat }
69
- io.puts(*arguments.map(&:value)) || Nothing.new
71
+ output.puts(*arguments.map(&:value))
72
+ Nothing.new
70
73
  else
71
74
  context = context.lookup!(operator)
72
75
  result = context.code_fetch(operator)
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.6.6")
5
+ Code::Version = Gem::Version.new("0.7.0")
data/lib/code.rb CHANGED
@@ -2,45 +2,51 @@
2
2
 
3
3
  class Code
4
4
  EMPTY_STRING = ""
5
- GLOBALS = %i[io context object].freeze
5
+ GLOBALS = %i[output error context object].freeze
6
6
  DEFAULT_TIMEOUT = 0
7
7
 
8
- def initialize(input, io: StringIO.new, timeout: DEFAULT_TIMEOUT, ruby: {})
8
+ def initialize(
9
+ input,
10
+ output: StringIO.new,
11
+ error: StringIO.new,
12
+ timeout: DEFAULT_TIMEOUT,
13
+ ruby: {}
14
+ )
9
15
  @input = input
10
- @parsed = Timeout.timeout(timeout) { ::Code::Parser.parse(@input).to_raw }
11
- @io = io
16
+ @parsed = Timeout.timeout(timeout) { ::Code::Parser.parse(input).to_raw }
17
+ @output = output
18
+ @error = error
12
19
  @timeout = timeout || DEFAULT_TIMEOUT
13
20
  @ruby = ::Code::Ruby.to_code(ruby || {}).code_to_context
14
21
  end
15
22
 
16
23
  def self.evaluate(
17
24
  input,
18
- context = "",
19
- io: StringIO.new,
25
+ context = EMPTY_STRING,
26
+ output: StringIO.new,
27
+ error: StringIO.new,
20
28
  timeout: DEFAULT_TIMEOUT,
21
29
  ruby: {}
22
30
  )
23
- new(input, io:, timeout:, ruby:).evaluate(context)
31
+ new(input, output:, error:, timeout:, ruby:).evaluate(context)
24
32
  end
25
33
 
26
- def evaluate(context = "")
34
+ def evaluate(context = EMPTY_STRING)
27
35
  Timeout.timeout(timeout) do
28
36
  context =
29
37
  if context == EMPTY_STRING
30
38
  Object::Context.new
31
39
  else
32
- Code.evaluate(context, timeout:, io:, ruby:).code_to_context
40
+ Code.evaluate(context, timeout:, output:, error:, ruby:).code_to_context
33
41
  end
34
42
 
35
- raise(Error::IncompatibleContext) unless context.is_a?(Object::Context)
36
-
37
- context = context.merge(ruby)
43
+ context = ruby.merge(context)
38
44
 
39
- Node::Code.new(parsed).evaluate(context:, io:)
45
+ Node::Code.new(parsed).evaluate(context:, output:, error:)
40
46
  end
41
47
  end
42
48
 
43
49
  private
44
50
 
45
- attr_reader :input, :parsed, :timeout, :io, :ruby
51
+ attr_reader :input, :parsed, :timeout, :output, :error, :ruby
46
52
  end
data/spec/code_spec.rb CHANGED
@@ -134,9 +134,9 @@ RSpec.describe Code do
134
134
 
135
135
  [["puts(true)", "true\n"], %w[print(false) false]].each do |input, expected|
136
136
  it "#{input} prints #{expected}" do
137
- io = StringIO.new
138
- Code.evaluate(input, io:)
139
- expect(io.string).to eq(expected)
137
+ output = StringIO.new
138
+ Code.evaluate(input, output:)
139
+ expect(output.string).to eq(expected)
140
140
  end
141
141
  end
142
142
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: code-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.6
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dorian Marié
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-02-07 00:00:00.000000000 Z
11
+ date: 2024-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bigdecimal
@@ -187,7 +187,7 @@ files:
187
187
  - spec/code/type_spec.rb
188
188
  - spec/code_spec.rb
189
189
  - spec/spec_helper.rb
190
- homepage: https://github.com/dorianmariefr/code-ruby
190
+ homepage: https://github.com/dorianmariecom/code-ruby
191
191
  licenses:
192
192
  - MIT
193
193
  metadata: {}