lib/code.rb in code-ruby-0.5.6 vs lib/code.rb in code-ruby-0.6.0
- old
+ new
@@ -1,44 +1,43 @@
+# frozen_string_literal: true
+
class Code
EMPTY_STRING = ""
- GLOBALS = %i[io context object]
- DEFAULT_TIMEOUT = Template::DEFAULT_TIMEOUT
+ GLOBALS = %i[io context object].freeze
+ DEFAULT_TIMEOUT = 0
def initialize(input, io: StringIO.new, timeout: DEFAULT_TIMEOUT, ruby: {})
@input = input
@parsed = Timeout.timeout(timeout) { ::Code::Parser.parse(@input).to_raw }
@io = io
@timeout = timeout || DEFAULT_TIMEOUT
- @ruby = ::Code::Ruby.to_code(ruby || {})
+ @ruby = ::Code::Ruby.to_code(ruby || {}).code_to_context
end
def self.evaluate(
input,
context = "",
io: StringIO.new,
timeout: DEFAULT_TIMEOUT,
ruby: {}
)
- new(input, io: io, timeout: timeout, ruby: ruby).evaluate(context)
+ new(input, io:, timeout:, ruby:).evaluate(context)
end
def evaluate(context = "")
Timeout.timeout(timeout) do
- if context != EMPTY_STRING
- context = ::Code.evaluate(context, timeout: timeout, io: io, ruby: ruby)
- else
- context = ::Code::Object::Dictionnary.new
- end
+ context =
+ if context == EMPTY_STRING
+ Object::Context.new
+ else
+ Code.evaluate(context, timeout:, io:, ruby:).code_to_context
+ end
- if !context.is_a?(::Code::Object::Dictionnary)
- raise ::Code::Error::IncompatibleContext.new(
- "context must be a dictionnary"
- )
- end
+ raise(Error::IncompatibleContext) unless context.is_a?(Object::Context)
context = context.merge(ruby)
- ::Code::Node::Code.new(parsed).evaluate(context: context, io: io)
+ Node::Code.new(parsed).evaluate(context:, io:)
end
end
private