dorian-eval 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/VERSION +1 -1
  3. data/lib/dorian/eval.rb +37 -13
  4. 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: 10646b3b875591f349e5b45416f4a96705d23b948fe9b2ee065d7ceed5bb5619
4
+ data.tar.gz: 542b45a942b3fc281385163ae9f158d8f3227a73547d04c6686b76622b768273
5
5
  SHA512:
6
- metadata.gz: 4c74e41390380d59c9a9d3689a319662e688e109aa85b226e18c4dfd37bbbc2dc04e976ce68f09512a3f711345768ab1261f9fe9fe1d0a294cdd12d34546e7a4
7
- data.tar.gz: 3432b27087c160ebb42d5640c65489705e5a99891ed7d09340cb3fdc208dc21b2fb59d57b923a8c5f1de15a6c8864a0589b9aea9534284d4c132f73c29de6484
6
+ metadata.gz: 3879cb847be21d2d6aaaebc2c92a998be47fa61fec48b573609c03ac680202487e39631447e88f712716c902dae69d19bce4e71c39ac51397c193fa67abdd3b2
7
+ data.tar.gz: ab6dd6ba4f37f0106c8f0a6a8ddad40eef87bbccaa6d6e813538cc99f04e652436e3d6b45004171db00917e97f80782d42a9c3ade3828499c9f1d043021f3bd2
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.3.0
1
+ 1.4.0
data/lib/dorian/eval.rb CHANGED
@@ -6,7 +6,14 @@ class Dorian
6
6
  class Eval
7
7
  Return = Data.define(:stdout, :stderr, :returned)
8
8
 
9
- attr_reader :ruby, :it, :debug, :stdout, :stderr, :colorize, :rails, :returns
9
+ attr_reader :ruby,
10
+ :it,
11
+ :debug,
12
+ :stdout,
13
+ :stderr,
14
+ :colorize,
15
+ :rails,
16
+ :returns
10
17
 
11
18
  COLORS = { red: "\e[31m", green: "\e[32m", reset: "\e[0m" }.freeze
12
19
 
@@ -86,20 +93,37 @@ class Dorian
86
93
  debug? && !returns? && it ? "[#{it}] " : ""
87
94
  end
88
95
 
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
96
+ def to_ruby(ruby)
97
+ if ruby.is_a?(Struct)
98
+ keys = ruby.to_h.keys.map { |key| to_ruby(key) }
99
+ values = ruby.to_h.values.map { |value| to_ruby(value) }
100
+ "Struct.new(#{keys.join(", ")}).new(#{values.join(", ")})"
101
+ elsif ruby.is_a?(String) || ruby.is_a?(Symbol) || ruby.is_a?(NilClass) ||
102
+ ruby.is_a?(TrueClass) || ruby.is_a?(FalseClass) || ruby.is_a?(Float) ||
103
+ ruby.is_a?(Integer)
104
+ ruby.inspect
105
+ elsif ruby.is_a?(Array)
106
+ "[#{ruby.map { |element| to_ruby(element) }.join(", ")}]"
107
+ elsif ruby.is_a?(Hash)
108
+ "{#{ruby.map { |key, value| "#{to_ruby(key)} => #{to_ruby(value)}" }}}"
97
109
  else
98
- full_ruby = <<~RUBY
99
- it = #{it.inspect}
100
- #{ruby}
101
- RUBY
110
+ raise "#{ruby.class} not supported"
102
111
  end
112
+ end
113
+
114
+ def full_ruby
115
+ full_ruby = "it = #{to_ruby(it)}\n"
116
+ full_ruby +=
117
+ if returns?
118
+ <<~RUBY
119
+ require "yaml"
120
+ puts (#{ruby}).to_yaml
121
+ RUBY
122
+ else
123
+ <<~RUBY
124
+ #{ruby}
125
+ RUBY
126
+ end
103
127
 
104
128
  full_ruby = <<~RUBY if rails?
105
129
  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.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dorian Marié