jsonata 2.2.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,113 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jsonata
4
+ # Ruby equivalent of the error objects thrown by jsonata-js, e.g.
5
+ # throw { code: "D1001", value: n, stack: ... }
6
+ #
7
+ # The message is populated from the error code catalog (see Evaluator::ERROR_CODES)
8
+ class Error < StandardError
9
+ FIELDS = %i[code position token value value2 index type exp function_name error remaining offset].freeze
10
+
11
+ attr_accessor(*FIELDS)
12
+ attr_writer :message
13
+
14
+ def initialize(code = nil, message: nil, **fields)
15
+ @code = code
16
+ @message = message
17
+ fields.each do |key, val|
18
+ raise ArgumentError.new("unknown field #{key}") unless FIELDS.include?(key)
19
+
20
+ instance_variable_set("@#{key}", val)
21
+ end
22
+ super(message || code)
23
+ end
24
+
25
+ def message
26
+ @message || code.to_s
27
+ end
28
+
29
+ def to_s
30
+ message
31
+ end
32
+
33
+ # Returns the error fields keyed using the jsonata-js property names
34
+ def to_h
35
+ result = {'code' => code}
36
+ FIELDS.each do |field|
37
+ next if field == :code
38
+
39
+ val = public_send(field)
40
+ next if val.nil?
41
+
42
+ key = field == :function_name ? 'functionName' : field.to_s
43
+ result[key] = val
44
+ end
45
+ result['message'] = message
46
+ result
47
+ end
48
+ end
49
+
50
+ # A lambda function defined in a JSONata expression
51
+ # (jsonata-js: object with _jsonata_lambda: true)
52
+ class Lambda
53
+ attr_accessor :input, :environment, :arguments, :signature, :body, :thunk, :token, :position, :apply_proc
54
+
55
+ def initialize(input: nil, environment: nil, arguments: [], signature: nil, body: nil, thunk: false)
56
+ @input = input
57
+ @environment = environment
58
+ @arguments = arguments
59
+ @signature = signature
60
+ @body = body
61
+ @thunk = thunk
62
+ end
63
+
64
+ # (jsonata-js: procedure.apply(self, args))
65
+ def apply(this, args)
66
+ raise Jsonata::Error.new('T1006') unless apply_proc
67
+
68
+ apply_proc.call(this, args)
69
+ end
70
+
71
+ def inspect
72
+ '#<Jsonata::Lambda>'
73
+ end
74
+ end
75
+
76
+ # A native (Ruby) function definition with an optional signature
77
+ # (jsonata-js: object with _jsonata_function: true)
78
+ class NativeFunction
79
+ attr_accessor :implementation, :signature, :token, :position
80
+
81
+ def initialize(implementation, signature = nil)
82
+ @implementation = implementation
83
+ @signature = signature
84
+ end
85
+
86
+ def inspect
87
+ '#<Jsonata::NativeFunction>'
88
+ end
89
+ end
90
+
91
+ # A Ruby callable with an explicit arity
92
+ # (jsonata-js: a plain JS function with an `arity` property)
93
+ class Closure
94
+ attr_reader :arity
95
+
96
+ def initialize(arity, &block)
97
+ @arity = arity
98
+ @block = block
99
+ end
100
+
101
+ def call(*args)
102
+ @block.call(*args)
103
+ end
104
+
105
+ def parameters
106
+ [%i[rest args]]
107
+ end
108
+
109
+ def to_proc
110
+ @block
111
+ end
112
+ end
113
+ end