dorian-eval 1.3.0 → 1.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/VERSION +1 -1
  3. data/bin/eval +5 -1
  4. data/lib/dorian/eval.rb +62 -16
  5. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0e3397d795e44365e644442920fa42a4e0059b9ae6c973c6229789022b717ea0
4
- data.tar.gz: 1e273dea96c18ea6b2e29f8bd8be8acbaa534d9c9c062caa617ba80943e86ddb
3
+ metadata.gz: 6703e36e324b4d2bfb01ea10df40e58cc6e9d71361a360e96046ecf7cd63b1ec
4
+ data.tar.gz: 87724996f3b92701a0804331ba24d539ec2ebba7400221582214394228c8aa6f
5
5
  SHA512:
6
- metadata.gz: 4c74e41390380d59c9a9d3689a319662e688e109aa85b226e18c4dfd37bbbc2dc04e976ce68f09512a3f711345768ab1261f9fe9fe1d0a294cdd12d34546e7a4
7
- data.tar.gz: 3432b27087c160ebb42d5640c65489705e5a99891ed7d09340cb3fdc208dc21b2fb59d57b923a8c5f1de15a6c8864a0589b9aea9534284d4c132f73c29de6484
6
+ metadata.gz: 48749012f9b6a5c9312a6390588565d519abab6efd462de2d0bbc8879efe8d0e69e6d48d8b7378ac5dc43b642c352edbb3397a9c580dc92395a4b9b70d1f7949
7
+ data.tar.gz: 9af9ad5cd930bf83101b7960fc6dbd18686002bc6109377733403fe28897bd29c1c83eb5fe9a552d73003592f09deeda338d0cc8b57dcb8a792d7d46685f1b14
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.3.0
1
+ 1.4.1
data/bin/eval CHANGED
@@ -28,6 +28,8 @@ parsed =
28
28
  rails: {
29
29
  alias: :r
30
30
  },
31
+ returns: { alias: :return },
32
+ fast: { alias: :f },
31
33
  version: {
32
34
  alias: :v
33
35
  },
@@ -49,5 +51,7 @@ Dorian::Eval.eval(
49
51
  stdout: parsed.options.stdout,
50
52
  stderr: parsed.options.stderr,
51
53
  colorize: parsed.options.colorize,
52
- rails: parsed.options.rails
54
+ rails: parsed.options.rails,
55
+ returns: parsed.options.returns,
56
+ fast: parsed.options.fast
53
57
  )
data/lib/dorian/eval.rb CHANGED
@@ -4,9 +4,21 @@ require "yaml"
4
4
 
5
5
  class Dorian
6
6
  class Eval
7
- Return = Data.define(:stdout, :stderr, :returned)
7
+ Return = Data.define(:stdout, :stderr, :returned) do
8
+ def initialize(stdout: "", stderr: "", returned: nil)
9
+ super(stdout:, stderr:, returned:)
10
+ end
11
+ end
8
12
 
9
- attr_reader :ruby, :it, :debug, :stdout, :stderr, :colorize, :rails, :returns
13
+ attr_reader :ruby,
14
+ :it,
15
+ :debug,
16
+ :stdout,
17
+ :stderr,
18
+ :colorize,
19
+ :rails,
20
+ :returns,
21
+ :fast
10
22
 
11
23
  COLORS = { red: "\e[31m", green: "\e[32m", reset: "\e[0m" }.freeze
12
24
 
@@ -18,7 +30,8 @@ class Dorian
18
30
  stderr: true,
19
31
  colorize: false,
20
32
  rails: false,
21
- returns: false
33
+ returns: false,
34
+ fast: false
22
35
  )
23
36
  @ruby = ruby.to_s.empty? ? "nil" : ruby
24
37
  @it = it.to_s.empty? ? nil : it
@@ -28,6 +41,7 @@ class Dorian
28
41
  @colorize = !!colorize
29
42
  @rails = !!rails
30
43
  @returns = !!returns
44
+ @fast = !!fast
31
45
  end
32
46
 
33
47
  def self.eval(...)
@@ -35,6 +49,14 @@ class Dorian
35
49
  end
36
50
 
37
51
  def eval
52
+ fast? ? eval_fast : eval_slow
53
+ end
54
+
55
+ def eval_fast
56
+ Return.new(returned: Kernel.eval(full_ruby))
57
+ end
58
+
59
+ def eval_slow
38
60
  read_out, write_out = IO.pipe
39
61
  read_err, write_err = IO.pipe
40
62
 
@@ -54,10 +76,14 @@ class Dorian
54
76
  if returns?
55
77
  Return.new(stdout: out, stderr: err, returned: YAML.safe_load(out))
56
78
  else
57
- Return.new(stdout: out, stderr: err, returned: nil)
79
+ Return.new(stdout: out, stderr: err)
58
80
  end
59
81
  end
60
82
 
83
+ def fast?
84
+ !!fast
85
+ end
86
+
61
87
  def debug?
62
88
  !!debug
63
89
  end
@@ -86,20 +112,40 @@ class Dorian
86
112
  debug? && !returns? && it ? "[#{it}] " : ""
87
113
  end
88
114
 
89
- def full_ruby
90
- if returns?
91
- full_ruby = <<~RUBY
92
- require "yaml"
93
-
94
- it = #{it.inspect}
95
- puts (#{ruby}).to_yaml
96
- RUBY
115
+ def to_ruby(ruby)
116
+ case ruby
117
+ when Struct
118
+ keys = ruby.to_h.keys.map { |key| to_ruby(key) }
119
+ values = ruby.to_h.values.map { |value| to_ruby(value) }
120
+ "Struct.new(#{keys.join(", ")}).new(#{values.join(", ")})"
121
+ when String, Symbol, NilClass, TrueClass, FalseClass, Float, Integer
122
+ ruby.inspect
123
+ when Array
124
+ "[#{ruby.map { |element| to_ruby(element) }.join(", ")}]"
125
+ when Hash
126
+ "{#{ruby.map { |key, value| "#{to_ruby(key)} => #{to_ruby(value)}" }}}"
97
127
  else
98
- full_ruby = <<~RUBY
99
- it = #{it.inspect}
100
- #{ruby}
101
- RUBY
128
+ raise "#{ruby.class} not supported"
102
129
  end
130
+ end
131
+
132
+ def slow?
133
+ !fast?
134
+ end
135
+
136
+ def full_ruby
137
+ full_ruby = "it = #{to_ruby(it)}\n"
138
+ full_ruby +=
139
+ if returns? && slow?
140
+ <<~RUBY
141
+ require "yaml"
142
+ puts (#{ruby}).to_yaml
143
+ RUBY
144
+ else
145
+ <<~RUBY
146
+ #{ruby}
147
+ RUBY
148
+ end
103
149
 
104
150
  full_ruby = <<~RUBY if rails?
105
151
  require "#{Dir.pwd}/config/environment"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dorian-eval
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dorian Marié