Module: PortableExpressions

Defined in:
lib/portable_expressions.rb,
lib/portable_expressions/scalar.rb,
lib/portable_expressions/operand.rb,
lib/portable_expressions/version.rb,
lib/portable_expressions/variable.rb,
lib/portable_expressions/expression.rb,
lib/portable_expressions/environment.rb,
lib/portable_expressions/modules/serializable.rb

Overview

See the README for details.

Defined Under Namespace

Modules: Serializable Classes: Environment, Expression, Operand, Scalar, Variable

Constant Summary collapse

Error =
Class.new(StandardError)
DeserializationError =
Class.new(Error)
InvalidOperandError =
Class.new(Error)
InvalidOperatorError =
Class.new(Error)
MissingVariableError =
Class.new(Error)
VERSION =
"0.1.1"

Class Method Summary collapse

Class Method Details

.from_json(json) ⇒ Expression, ...

Parameters:

  • json (String, Hash)

Returns:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/portable_expressions.rb', line 26

def self.from_json(json) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength
  json = JSON.parse(json) if json.is_a?(String)

  case json["object"]
  when Environment.name
    Environment.new(**json["variables"])
  when Expression.name
    operator = json["operator"].to_sym
    operands = json["operands"].map { |operand_json| from_json(operand_json) }

    Expression.new(operator, *operands)
  when Variable.name
    Variable.new(json["name"])
  when Scalar.name
    Scalar.new(json["value"])
  else
    raise DeserializationError, "Object type #{json["object"]} not supported for deserialization."
  end
rescue JSON::ParserError => e
  raise DeserializationError, "Unable to parse JSON: #{e.message}."
end