dorian-eval 1.2.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 +59 -13
  4. metadata +17 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b21f0fd649da60ad757aee35c4cd37882238256ad3cce477d4ef091251958ce0
4
- data.tar.gz: '08c67d23fabfcb66678e1527ec0743ce3e920162f1b947126384e2623bb3e928'
3
+ metadata.gz: 10646b3b875591f349e5b45416f4a96705d23b948fe9b2ee065d7ceed5bb5619
4
+ data.tar.gz: 542b45a942b3fc281385163ae9f158d8f3227a73547d04c6686b76622b768273
5
5
  SHA512:
6
- metadata.gz: 28a5aa59bcb7da623d70f44094ab1c85fd49bee49aebaf84bed690227b044a040f20a30fbcfaea4a03a31ae2a5b460268a4834a21b52fd4889f5b80d98874ae4
7
- data.tar.gz: 2cbb210c32dc52ef8dd9f46002f1e0b1d21f25293322174b0b634b71d27a076af9686d030e7d35088e2f8daf64be645998114f20ec2ed3ac7ef282beaf53a2ab
6
+ metadata.gz: 3879cb847be21d2d6aaaebc2c92a998be47fa61fec48b573609c03ac680202487e39631447e88f712716c902dae69d19bce4e71c39ac51397c193fa67abdd3b2
7
+ data.tar.gz: ab6dd6ba4f37f0106c8f0a6a8ddad40eef87bbccaa6d6e813538cc99f04e652436e3d6b45004171db00917e97f80782d42a9c3ade3828499c9f1d043021f3bd2
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.2.0
1
+ 1.4.0
data/lib/dorian/eval.rb CHANGED
@@ -1,28 +1,40 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "yaml"
4
+
3
5
  class Dorian
4
6
  class Eval
5
- attr_reader :ruby, :it, :debug, :stdout, :stderr, :colorize, :rails
7
+ Return = Data.define(:stdout, :stderr, :returned)
8
+
9
+ attr_reader :ruby,
10
+ :it,
11
+ :debug,
12
+ :stdout,
13
+ :stderr,
14
+ :colorize,
15
+ :rails,
16
+ :returns
6
17
 
7
18
  COLORS = { red: "\e[31m", green: "\e[32m", reset: "\e[0m" }.freeze
8
19
 
9
20
  def initialize(
10
- *args,
11
- ruby: "",
12
- it: "",
21
+ ruby: nil,
22
+ it: nil,
13
23
  debug: false,
14
24
  stdout: true,
15
25
  stderr: true,
16
26
  colorize: false,
17
- rails: false
27
+ rails: false,
28
+ returns: false
18
29
  )
19
- @ruby = ruby.empty? ? args.join(" ") : ruby
20
- @it = it
30
+ @ruby = ruby.to_s.empty? ? "nil" : ruby
31
+ @it = it.to_s.empty? ? nil : it
21
32
  @debug = !!debug
22
33
  @stdout = !!stdout
23
34
  @stderr = !!stderr
24
35
  @colorize = !!colorize
25
36
  @rails = !!rails
37
+ @returns = !!returns
26
38
  end
27
39
 
28
40
  def self.eval(...)
@@ -46,7 +58,11 @@ class Dorian
46
58
  err += gets(read_err, color: :red, print: stderr?, method: :warn).to_s
47
59
  end
48
60
 
49
- [out, err]
61
+ if returns?
62
+ Return.new(stdout: out, stderr: err, returned: YAML.safe_load(out))
63
+ else
64
+ Return.new(stdout: out, stderr: err, returned: nil)
65
+ end
50
66
  end
51
67
 
52
68
  def debug?
@@ -69,15 +85,45 @@ class Dorian
69
85
  !!rails
70
86
  end
71
87
 
88
+ def returns?
89
+ !!returns
90
+ end
91
+
72
92
  def prefix
73
- debug? ? "[#{it}] " : ""
93
+ debug? && !returns? && it ? "[#{it}] " : ""
94
+ end
95
+
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)}" }}}"
109
+ else
110
+ raise "#{ruby.class} not supported"
111
+ end
74
112
  end
75
113
 
76
114
  def full_ruby
77
- full_ruby = <<~RUBY
78
- it = #{it.inspect}
79
- #{ruby}
80
- 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
81
127
 
82
128
  full_ruby = <<~RUBY if rails?
83
129
  require "#{Dir.pwd}/config/environment"
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dorian-eval
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.4.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-08-29 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2024-08-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: yaml
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  description: evaluates ruby
14
28
  email: dorian@dorianmarie.com
15
29
  executables: