Class: Dorian::Eval
- Inherits:
-
Object
- Object
- Dorian::Eval
- Defined in:
- lib/dorian/eval.rb
Constant Summary collapse
- Return =
Data.define(:stdout, :stderr, :returned) do def initialize(stdout: "", stderr: "", returned: nil) super(stdout:, stderr:, returned:) end end
- COLORS =
{ red: "\e[31m", green: "\e[32m", reset: "\e[0m" }.freeze
Instance Attribute Summary collapse
-
#colorize ⇒ Object
readonly
Returns the value of attribute colorize.
-
#debug ⇒ Object
readonly
Returns the value of attribute debug.
-
#fast ⇒ Object
readonly
Returns the value of attribute fast.
-
#it ⇒ Object
readonly
Returns the value of attribute it.
-
#rails ⇒ Object
readonly
Returns the value of attribute rails.
-
#returns ⇒ Object
readonly
Returns the value of attribute returns.
-
#ruby ⇒ Object
readonly
Returns the value of attribute ruby.
-
#stderr ⇒ Object
readonly
Returns the value of attribute stderr.
-
#stdout ⇒ Object
readonly
Returns the value of attribute stdout.
Class Method Summary collapse
Instance Method Summary collapse
- #colorize? ⇒ Boolean
- #colorize_string(string, color) ⇒ Object
- #debug? ⇒ Boolean
- #eval ⇒ Object
- #eval_fast ⇒ Object
- #eval_slow ⇒ Object
- #fast? ⇒ Boolean
- #full_ruby ⇒ Object
- #gets(read, color: nil, print: true, method: :puts) ⇒ Object
-
#initialize(ruby: nil, it: nil, debug: false, stdout: true, stderr: true, colorize: false, rails: false, returns: false, fast: false) ⇒ Eval
constructor
A new instance of Eval.
- #prefix ⇒ Object
- #rails? ⇒ Boolean
- #returns? ⇒ Boolean
- #slow? ⇒ Boolean
- #spawn(write_out:, write_err:) ⇒ Object
- #stderr? ⇒ Boolean
- #stdout? ⇒ Boolean
- #to_ruby(ruby) ⇒ Object
Constructor Details
#initialize(ruby: nil, it: nil, debug: false, stdout: true, stderr: true, colorize: false, rails: false, returns: false, fast: false) ⇒ Eval
Returns a new instance of Eval.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/dorian/eval.rb', line 25 def initialize( ruby: nil, it: nil, debug: false, stdout: true, stderr: true, colorize: false, rails: false, returns: false, fast: false ) @ruby = ruby.to_s.empty? ? "nil" : ruby @it = it.to_s.empty? ? nil : it @debug = !!debug @stdout = !!stdout @stderr = !!stderr @colorize = !!colorize @rails = !!rails @returns = !!returns @fast = !!fast end |
Instance Attribute Details
#colorize ⇒ Object (readonly)
Returns the value of attribute colorize.
13 14 15 |
# File 'lib/dorian/eval.rb', line 13 def colorize @colorize end |
#debug ⇒ Object (readonly)
Returns the value of attribute debug.
13 14 15 |
# File 'lib/dorian/eval.rb', line 13 def debug @debug end |
#fast ⇒ Object (readonly)
Returns the value of attribute fast.
13 14 15 |
# File 'lib/dorian/eval.rb', line 13 def fast @fast end |
#it ⇒ Object (readonly)
Returns the value of attribute it.
13 14 15 |
# File 'lib/dorian/eval.rb', line 13 def it @it end |
#rails ⇒ Object (readonly)
Returns the value of attribute rails.
13 14 15 |
# File 'lib/dorian/eval.rb', line 13 def rails @rails end |
#returns ⇒ Object (readonly)
Returns the value of attribute returns.
13 14 15 |
# File 'lib/dorian/eval.rb', line 13 def returns @returns end |
#ruby ⇒ Object (readonly)
Returns the value of attribute ruby.
13 14 15 |
# File 'lib/dorian/eval.rb', line 13 def ruby @ruby end |
#stderr ⇒ Object (readonly)
Returns the value of attribute stderr.
13 14 15 |
# File 'lib/dorian/eval.rb', line 13 def stderr @stderr end |
#stdout ⇒ Object (readonly)
Returns the value of attribute stdout.
13 14 15 |
# File 'lib/dorian/eval.rb', line 13 def stdout @stdout end |
Class Method Details
.eval ⇒ Object
47 48 49 |
# File 'lib/dorian/eval.rb', line 47 def self.eval(...) new(...).eval end |
Instance Method Details
#colorize? ⇒ Boolean
99 100 101 |
# File 'lib/dorian/eval.rb', line 99 def colorize? !!colorize end |
#colorize_string(string, color) ⇒ Object
184 185 186 187 188 |
# File 'lib/dorian/eval.rb', line 184 def colorize_string(string, color) return string unless color [COLORS.fetch(color), string, COLORS.fetch(:reset)].join end |
#debug? ⇒ Boolean
87 88 89 |
# File 'lib/dorian/eval.rb', line 87 def debug? !!debug end |
#eval ⇒ Object
51 52 53 |
# File 'lib/dorian/eval.rb', line 51 def eval fast? ? eval_fast : eval_slow end |
#eval_fast ⇒ Object
55 56 57 |
# File 'lib/dorian/eval.rb', line 55 def eval_fast Return.new(returned: Kernel.eval(full_ruby)) end |
#eval_slow ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/dorian/eval.rb', line 59 def eval_slow read_out, write_out = IO.pipe read_err, write_err = IO.pipe spawn(write_out:, write_err:) write_out.close write_err.close out = "" err = "" while !read_out.eof? || !read_err.eof? out += gets(read_out, print: stdout?, method: :puts).to_s err += gets(read_err, color: :red, print: stderr?, method: :warn).to_s end if returns? Return.new(stdout: out, stderr: err, returned: YAML.safe_load(out)) else Return.new(stdout: out, stderr: err) end end |
#fast? ⇒ Boolean
83 84 85 |
# File 'lib/dorian/eval.rb', line 83 def fast? !!fast end |
#full_ruby ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/dorian/eval.rb', line 136 def full_ruby full_ruby = "it = #{to_ruby(it)}\n" full_ruby += if returns? && slow? <<~RUBY require "yaml" puts (#{ruby}).to_yaml RUBY else <<~RUBY #{ruby} RUBY end full_ruby = <<~RUBY if rails? require "#{Dir.pwd}/config/environment" #{full_ruby} RUBY full_ruby end |
#gets(read, color: nil, print: true, method: :puts) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/dorian/eval.rb', line 168 def gets(read, color: nil, print: true, method: :puts) original_string = read.gets return unless original_string string = original_string.rstrip string = colorize_string(string, color) if colorize? && color if method == :puts && print puts [prefix, string].join elsif method == :warn && print warn [prefix, string].join end original_string end |
#prefix ⇒ Object
111 112 113 |
# File 'lib/dorian/eval.rb', line 111 def prefix debug? && !returns? && it ? "[#{it}] " : "" end |
#rails? ⇒ Boolean
103 104 105 |
# File 'lib/dorian/eval.rb', line 103 def rails? !!rails end |
#returns? ⇒ Boolean
107 108 109 |
# File 'lib/dorian/eval.rb', line 107 def returns? !!returns end |
#slow? ⇒ Boolean
132 133 134 |
# File 'lib/dorian/eval.rb', line 132 def slow? !fast? end |
#spawn(write_out:, write_err:) ⇒ Object
158 159 160 161 162 163 164 165 166 |
# File 'lib/dorian/eval.rb', line 158 def spawn(write_out:, write_err:) Process.spawn( RbConfig.ruby, "-e", full_ruby, out: write_out, err: write_err ) end |
#stderr? ⇒ Boolean
95 96 97 |
# File 'lib/dorian/eval.rb', line 95 def stderr? !!stderr end |
#stdout? ⇒ Boolean
91 92 93 |
# File 'lib/dorian/eval.rb', line 91 def stdout? !!stdout end |
#to_ruby(ruby) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/dorian/eval.rb', line 115 def to_ruby(ruby) case ruby when Struct keys = ruby.to_h.keys.map { |key| to_ruby(key) } values = ruby.to_h.values.map { |value| to_ruby(value) } "Struct.new(#{keys.join(", ")}).new(#{values.join(", ")})" when String, Symbol, NilClass, TrueClass, FalseClass, Float, Integer ruby.inspect when Array "[#{ruby.map { |element| to_ruby(element) }.join(", ")}]" when Hash "{#{ruby.map { |key, value| "#{to_ruby(key)} => #{to_ruby(value)}" }}}" else raise "#{ruby.class} not supported" end end |