spikard 0.5.0 → 0.6.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.
- checksums.yaml +4 -4
- data/LICENSE +1 -1
- data/README.md +674 -674
- data/ext/spikard_rb/Cargo.toml +17 -17
- data/ext/spikard_rb/extconf.rb +13 -10
- data/ext/spikard_rb/src/lib.rs +6 -6
- data/lib/spikard/app.rb +405 -405
- data/lib/spikard/background.rb +27 -27
- data/lib/spikard/config.rb +396 -396
- data/lib/spikard/converters.rb +13 -13
- data/lib/spikard/handler_wrapper.rb +113 -113
- data/lib/spikard/provide.rb +214 -214
- data/lib/spikard/response.rb +173 -173
- data/lib/spikard/schema.rb +243 -243
- data/lib/spikard/sse.rb +111 -111
- data/lib/spikard/streaming_response.rb +44 -44
- data/lib/spikard/testing.rb +256 -256
- data/lib/spikard/upload_file.rb +131 -131
- data/lib/spikard/version.rb +5 -5
- data/lib/spikard/websocket.rb +59 -59
- data/lib/spikard.rb +43 -43
- data/sig/spikard.rbs +366 -366
- data/vendor/crates/spikard-bindings-shared/Cargo.toml +63 -63
- data/vendor/crates/spikard-bindings-shared/examples/config_extraction.rs +132 -132
- data/vendor/crates/spikard-bindings-shared/src/config_extractor.rs +752 -752
- data/vendor/crates/spikard-bindings-shared/src/conversion_traits.rs +194 -194
- data/vendor/crates/spikard-bindings-shared/src/di_traits.rs +246 -246
- data/vendor/crates/spikard-bindings-shared/src/error_response.rs +401 -401
- data/vendor/crates/spikard-bindings-shared/src/handler_base.rs +238 -238
- data/vendor/crates/spikard-bindings-shared/src/lib.rs +24 -24
- data/vendor/crates/spikard-bindings-shared/src/lifecycle_base.rs +292 -292
- data/vendor/crates/spikard-bindings-shared/src/lifecycle_executor.rs +616 -616
- data/vendor/crates/spikard-bindings-shared/src/response_builder.rs +305 -305
- data/vendor/crates/spikard-bindings-shared/src/test_client_base.rs +248 -248
- data/vendor/crates/spikard-bindings-shared/src/validation_helpers.rs +351 -351
- data/vendor/crates/spikard-bindings-shared/tests/comprehensive_coverage.rs +454 -454
- data/vendor/crates/spikard-bindings-shared/tests/error_response_edge_cases.rs +383 -383
- data/vendor/crates/spikard-bindings-shared/tests/handler_base_integration.rs +280 -280
- data/vendor/crates/spikard-core/Cargo.toml +40 -40
- data/vendor/crates/spikard-core/src/bindings/mod.rs +3 -3
- data/vendor/crates/spikard-core/src/bindings/response.rs +133 -133
- data/vendor/crates/spikard-core/src/debug.rs +127 -127
- data/vendor/crates/spikard-core/src/di/container.rs +702 -702
- data/vendor/crates/spikard-core/src/di/dependency.rs +273 -273
- data/vendor/crates/spikard-core/src/di/error.rs +118 -118
- data/vendor/crates/spikard-core/src/di/factory.rs +534 -534
- data/vendor/crates/spikard-core/src/di/graph.rs +506 -506
- data/vendor/crates/spikard-core/src/di/mod.rs +192 -192
- data/vendor/crates/spikard-core/src/di/resolved.rs +405 -405
- data/vendor/crates/spikard-core/src/di/value.rs +281 -281
- data/vendor/crates/spikard-core/src/errors.rs +69 -69
- data/vendor/crates/spikard-core/src/http.rs +415 -415
- data/vendor/crates/spikard-core/src/lib.rs +29 -29
- data/vendor/crates/spikard-core/src/lifecycle.rs +1186 -1186
- data/vendor/crates/spikard-core/src/metadata.rs +389 -389
- data/vendor/crates/spikard-core/src/parameters.rs +2525 -2525
- data/vendor/crates/spikard-core/src/problem.rs +344 -344
- data/vendor/crates/spikard-core/src/request_data.rs +1154 -1154
- data/vendor/crates/spikard-core/src/router.rs +510 -510
- data/vendor/crates/spikard-core/src/schema_registry.rs +183 -183
- data/vendor/crates/spikard-core/src/type_hints.rs +304 -304
- data/vendor/crates/spikard-core/src/validation/error_mapper.rs +696 -688
- data/vendor/crates/spikard-core/src/validation/mod.rs +457 -457
- data/vendor/crates/spikard-http/Cargo.toml +62 -64
- data/vendor/crates/spikard-http/examples/sse-notifications.rs +148 -148
- data/vendor/crates/spikard-http/examples/websocket-chat.rs +92 -92
- data/vendor/crates/spikard-http/src/auth.rs +296 -296
- data/vendor/crates/spikard-http/src/background.rs +1860 -1860
- data/vendor/crates/spikard-http/src/bindings/mod.rs +3 -3
- data/vendor/crates/spikard-http/src/bindings/response.rs +1 -1
- data/vendor/crates/spikard-http/src/body_metadata.rs +8 -8
- data/vendor/crates/spikard-http/src/cors.rs +1005 -1005
- data/vendor/crates/spikard-http/src/debug.rs +128 -128
- data/vendor/crates/spikard-http/src/di_handler.rs +1668 -1668
- data/vendor/crates/spikard-http/src/handler_response.rs +901 -901
- data/vendor/crates/spikard-http/src/handler_trait.rs +838 -830
- data/vendor/crates/spikard-http/src/handler_trait_tests.rs +290 -290
- data/vendor/crates/spikard-http/src/lib.rs +534 -534
- data/vendor/crates/spikard-http/src/lifecycle/adapter.rs +230 -230
- data/vendor/crates/spikard-http/src/lifecycle.rs +1193 -1193
- data/vendor/crates/spikard-http/src/middleware/mod.rs +560 -540
- data/vendor/crates/spikard-http/src/middleware/multipart.rs +912 -912
- data/vendor/crates/spikard-http/src/middleware/urlencoded.rs +513 -513
- data/vendor/crates/spikard-http/src/middleware/validation.rs +768 -735
- data/vendor/crates/spikard-http/src/openapi/mod.rs +309 -309
- data/vendor/crates/spikard-http/src/openapi/parameter_extraction.rs +535 -535
- data/vendor/crates/spikard-http/src/openapi/schema_conversion.rs +1363 -1363
- data/vendor/crates/spikard-http/src/openapi/spec_generation.rs +665 -665
- data/vendor/crates/spikard-http/src/query_parser.rs +793 -793
- data/vendor/crates/spikard-http/src/response.rs +720 -720
- data/vendor/crates/spikard-http/src/server/handler.rs +1650 -1650
- data/vendor/crates/spikard-http/src/server/lifecycle_execution.rs +234 -234
- data/vendor/crates/spikard-http/src/server/mod.rs +1593 -1502
- data/vendor/crates/spikard-http/src/server/request_extraction.rs +789 -770
- data/vendor/crates/spikard-http/src/server/routing_factory.rs +629 -599
- data/vendor/crates/spikard-http/src/sse.rs +1409 -1409
- data/vendor/crates/spikard-http/src/testing/form.rs +52 -52
- data/vendor/crates/spikard-http/src/testing/multipart.rs +64 -60
- data/vendor/crates/spikard-http/src/testing/test_client.rs +311 -283
- data/vendor/crates/spikard-http/src/testing.rs +406 -377
- data/vendor/crates/spikard-http/src/websocket.rs +1404 -1375
- data/vendor/crates/spikard-http/tests/background_behavior.rs +832 -832
- data/vendor/crates/spikard-http/tests/common/handlers.rs +309 -309
- data/vendor/crates/spikard-http/tests/common/mod.rs +26 -26
- data/vendor/crates/spikard-http/tests/di_integration.rs +192 -192
- data/vendor/crates/spikard-http/tests/doc_snippets.rs +5 -5
- data/vendor/crates/spikard-http/tests/lifecycle_execution.rs +1093 -1093
- data/vendor/crates/spikard-http/tests/multipart_behavior.rs +656 -656
- data/vendor/crates/spikard-http/tests/server_config_builder.rs +314 -314
- data/vendor/crates/spikard-http/tests/sse_behavior.rs +620 -620
- data/vendor/crates/spikard-http/tests/websocket_behavior.rs +663 -663
- data/vendor/crates/spikard-rb/Cargo.toml +48 -48
- data/vendor/crates/spikard-rb/build.rs +199 -199
- data/vendor/crates/spikard-rb/src/background.rs +63 -63
- data/vendor/crates/spikard-rb/src/config/mod.rs +5 -5
- data/vendor/crates/spikard-rb/src/config/server_config.rs +285 -285
- data/vendor/crates/spikard-rb/src/conversion.rs +554 -554
- data/vendor/crates/spikard-rb/src/di/builder.rs +100 -100
- data/vendor/crates/spikard-rb/src/di/mod.rs +375 -375
- data/vendor/crates/spikard-rb/src/handler.rs +618 -618
- data/vendor/crates/spikard-rb/src/integration/mod.rs +3 -3
- data/vendor/crates/spikard-rb/src/lib.rs +1806 -1810
- data/vendor/crates/spikard-rb/src/lifecycle.rs +275 -275
- data/vendor/crates/spikard-rb/src/metadata/mod.rs +5 -5
- data/vendor/crates/spikard-rb/src/metadata/route_extraction.rs +442 -447
- data/vendor/crates/spikard-rb/src/runtime/mod.rs +5 -5
- data/vendor/crates/spikard-rb/src/runtime/server_runner.rs +324 -324
- data/vendor/crates/spikard-rb/src/server.rs +305 -308
- data/vendor/crates/spikard-rb/src/sse.rs +231 -231
- data/vendor/crates/spikard-rb/src/testing/client.rs +538 -551
- data/vendor/crates/spikard-rb/src/testing/mod.rs +7 -7
- data/vendor/crates/spikard-rb/src/testing/sse.rs +143 -143
- data/vendor/crates/spikard-rb/src/testing/websocket.rs +608 -635
- data/vendor/crates/spikard-rb/src/websocket.rs +377 -374
- metadata +15 -1
|
@@ -1,375 +1,375 @@
|
|
|
1
|
-
//! Ruby dependency injection implementations
|
|
2
|
-
//!
|
|
3
|
-
//! This module provides Ruby-specific implementations of the Dependency trait,
|
|
4
|
-
//! bridging Ruby values and Procs to the Rust DI system.
|
|
5
|
-
|
|
6
|
-
#![allow(dead_code)]
|
|
7
|
-
|
|
8
|
-
pub mod builder;
|
|
9
|
-
|
|
10
|
-
pub use builder::build_dependency_container;
|
|
11
|
-
|
|
12
|
-
use http::Request;
|
|
13
|
-
use magnus::prelude::*;
|
|
14
|
-
use magnus::value::{InnerValue, Opaque};
|
|
15
|
-
use magnus::{Error, RHash, Ruby, TryConvert, Value};
|
|
16
|
-
use serde_json::Value as JsonValue;
|
|
17
|
-
use spikard_core::di::{Dependency, DependencyError, ResolvedDependencies};
|
|
18
|
-
use spikard_core::request_data::RequestData;
|
|
19
|
-
use std::any::Any;
|
|
20
|
-
use std::pin::Pin;
|
|
21
|
-
use std::sync::Arc;
|
|
22
|
-
|
|
23
|
-
/// Ruby value dependency
|
|
24
|
-
///
|
|
25
|
-
/// Wraps a Ruby object as a static dependency value
|
|
26
|
-
pub struct RubyValueDependency {
|
|
27
|
-
key: String,
|
|
28
|
-
value: Opaque<Value>,
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
impl RubyValueDependency {
|
|
32
|
-
pub fn new(key: String, value: Value) -> Self {
|
|
33
|
-
Self {
|
|
34
|
-
key,
|
|
35
|
-
value: Opaque::from(value),
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
impl Dependency for RubyValueDependency {
|
|
41
|
-
fn key(&self) -> &str {
|
|
42
|
-
&self.key
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
fn depends_on(&self) -> Vec<String> {
|
|
46
|
-
Vec::new()
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
fn resolve(
|
|
50
|
-
&self,
|
|
51
|
-
_request: &Request<()>,
|
|
52
|
-
_request_data: &RequestData,
|
|
53
|
-
_resolved: &ResolvedDependencies,
|
|
54
|
-
) -> Pin<Box<dyn std::future::Future<Output = Result<Arc<dyn Any + Send + Sync>, DependencyError>> + Send + '_>>
|
|
55
|
-
{
|
|
56
|
-
Box::pin(async move {
|
|
57
|
-
let ruby = Ruby::get().map_err(|e| DependencyError::ResolutionFailed { message: e.to_string() })?;
|
|
58
|
-
|
|
59
|
-
let value = self.value.get_inner_with(&ruby);
|
|
60
|
-
|
|
61
|
-
let json_value = ruby_value_to_json(&ruby, value)
|
|
62
|
-
.map_err(|e| DependencyError::ResolutionFailed { message: e.to_string() })?;
|
|
63
|
-
|
|
64
|
-
Ok(Arc::new(json_value) as Arc<dyn Any + Send + Sync>)
|
|
65
|
-
})
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
fn singleton(&self) -> bool {
|
|
69
|
-
true
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
fn cacheable(&self) -> bool {
|
|
73
|
-
true
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/// Ruby factory dependency
|
|
78
|
-
///
|
|
79
|
-
/// Wraps a Ruby Proc as a factory dependency
|
|
80
|
-
pub struct RubyFactoryDependency {
|
|
81
|
-
key: String,
|
|
82
|
-
factory: Opaque<Value>,
|
|
83
|
-
depends_on: Vec<String>,
|
|
84
|
-
singleton: bool,
|
|
85
|
-
cacheable: bool,
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
impl RubyFactoryDependency {
|
|
89
|
-
pub fn new(key: String, factory: Value, depends_on: Vec<String>, singleton: bool, cacheable: bool) -> Self {
|
|
90
|
-
Self {
|
|
91
|
-
key,
|
|
92
|
-
factory: Opaque::from(factory),
|
|
93
|
-
depends_on,
|
|
94
|
-
singleton,
|
|
95
|
-
cacheable,
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
impl Dependency for RubyFactoryDependency {
|
|
101
|
-
fn key(&self) -> &str {
|
|
102
|
-
&self.key
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
fn depends_on(&self) -> Vec<String> {
|
|
106
|
-
self.depends_on.clone()
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
fn resolve(
|
|
110
|
-
&self,
|
|
111
|
-
_request: &Request<()>,
|
|
112
|
-
_request_data: &RequestData,
|
|
113
|
-
resolved: &ResolvedDependencies,
|
|
114
|
-
) -> Pin<Box<dyn std::future::Future<Output = Result<Arc<dyn Any + Send + Sync>, DependencyError>> + Send + '_>>
|
|
115
|
-
{
|
|
116
|
-
let factory = self.factory;
|
|
117
|
-
let depends_on = self.depends_on.clone();
|
|
118
|
-
let key = self.key.clone();
|
|
119
|
-
let is_singleton = self.singleton;
|
|
120
|
-
let resolved_clone = resolved.clone();
|
|
121
|
-
|
|
122
|
-
let resolved_deps: Vec<(String, JsonValue)> = depends_on
|
|
123
|
-
.iter()
|
|
124
|
-
.filter_map(|dep_key| {
|
|
125
|
-
if let Some(json_value) = resolved.get::<JsonValue>(dep_key) {
|
|
126
|
-
return Some((dep_key.clone(), (*json_value).clone()));
|
|
127
|
-
}
|
|
128
|
-
if let Some(wrapper) = resolved.get::<RubyValueWrapper>(dep_key)
|
|
129
|
-
&& let Ok(ruby) = Ruby::get()
|
|
130
|
-
&& let Ok(json) = wrapper.to_json(&ruby)
|
|
131
|
-
{
|
|
132
|
-
return Some((dep_key.clone(), json));
|
|
133
|
-
}
|
|
134
|
-
None
|
|
135
|
-
})
|
|
136
|
-
.collect();
|
|
137
|
-
|
|
138
|
-
Box::pin(async move {
|
|
139
|
-
let ruby = Ruby::get().map_err(|e| DependencyError::ResolutionFailed { message: e.to_string() })?;
|
|
140
|
-
|
|
141
|
-
let args: Result<Vec<Value>, DependencyError> = depends_on
|
|
142
|
-
.iter()
|
|
143
|
-
.filter_map(|dep_key| resolved_deps.iter().find(|(k, _)| k == dep_key).map(|(_, v)| v))
|
|
144
|
-
.map(|dep_value| {
|
|
145
|
-
json_to_ruby(&ruby, dep_value).map_err(|e| DependencyError::ResolutionFailed {
|
|
146
|
-
message: format!("Failed to convert dependency value: {}", e),
|
|
147
|
-
})
|
|
148
|
-
})
|
|
149
|
-
.collect();
|
|
150
|
-
let args = args?;
|
|
151
|
-
|
|
152
|
-
let factory_value = factory.get_inner_with(&ruby);
|
|
153
|
-
|
|
154
|
-
if !factory_value
|
|
155
|
-
.respond_to("call", false)
|
|
156
|
-
.map_err(|e| DependencyError::ResolutionFailed { message: e.to_string() })?
|
|
157
|
-
{
|
|
158
|
-
return Err(DependencyError::ResolutionFailed {
|
|
159
|
-
message: format!("Dependency factory for '{}' is not callable", key),
|
|
160
|
-
});
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
let result: Value = if !args.is_empty() {
|
|
164
|
-
let args_array = ruby.ary_new();
|
|
165
|
-
for arg in &args {
|
|
166
|
-
args_array.push(*arg).map_err(|e| DependencyError::ResolutionFailed {
|
|
167
|
-
message: format!("Failed to push arg to array: {}", e),
|
|
168
|
-
})?;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
let splat_lambda = ruby
|
|
172
|
-
.eval::<Value>("lambda { |proc, args| proc.call(*args) }")
|
|
173
|
-
.map_err(|e| DependencyError::ResolutionFailed {
|
|
174
|
-
message: format!("Failed to create splat lambda: {}", e),
|
|
175
|
-
})?;
|
|
176
|
-
|
|
177
|
-
splat_lambda.funcall("call", (factory_value, args_array))
|
|
178
|
-
} else {
|
|
179
|
-
factory_value.funcall("call", ())
|
|
180
|
-
}
|
|
181
|
-
.map_err(|e| DependencyError::ResolutionFailed {
|
|
182
|
-
message: format!("Failed to call factory for '{}': {}", key, e),
|
|
183
|
-
})?;
|
|
184
|
-
|
|
185
|
-
let (value_to_convert, cleanup_callback) = if result.is_kind_of(ruby.class_array()) {
|
|
186
|
-
let array = magnus::RArray::from_value(result).ok_or_else(|| DependencyError::ResolutionFailed {
|
|
187
|
-
message: format!("Failed to convert result to array for '{}'", key),
|
|
188
|
-
})?;
|
|
189
|
-
|
|
190
|
-
let len = array.len();
|
|
191
|
-
if len == 2 {
|
|
192
|
-
let resource: Value = array.entry(0).map_err(|e| DependencyError::ResolutionFailed {
|
|
193
|
-
message: format!("Failed to extract resource from array for '{}': {}", key, e),
|
|
194
|
-
})?;
|
|
195
|
-
|
|
196
|
-
let cleanup: Value = array.entry(1).map_err(|e| DependencyError::ResolutionFailed {
|
|
197
|
-
message: format!("Failed to extract cleanup callback for '{}': {}", key, e),
|
|
198
|
-
})?;
|
|
199
|
-
|
|
200
|
-
(resource, Some(cleanup))
|
|
201
|
-
} else {
|
|
202
|
-
(result, None)
|
|
203
|
-
}
|
|
204
|
-
} else {
|
|
205
|
-
(result, None)
|
|
206
|
-
};
|
|
207
|
-
|
|
208
|
-
if let Some(cleanup_proc) = cleanup_callback {
|
|
209
|
-
let cleanup_opaque = Opaque::from(cleanup_proc);
|
|
210
|
-
|
|
211
|
-
resolved_clone.add_cleanup_task(Box::new(move || {
|
|
212
|
-
Box::pin(async move {
|
|
213
|
-
if let Ok(ruby) = Ruby::get() {
|
|
214
|
-
let proc = cleanup_opaque.get_inner_with(&ruby);
|
|
215
|
-
let _ = proc.funcall::<_, _, Value>("call", ());
|
|
216
|
-
}
|
|
217
|
-
})
|
|
218
|
-
}));
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
if is_singleton {
|
|
222
|
-
let wrapper = RubyValueWrapper::new(value_to_convert);
|
|
223
|
-
return Ok(Arc::new(wrapper) as Arc<dyn Any + Send + Sync>);
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
let json_value = ruby_value_to_json(&ruby, value_to_convert)
|
|
227
|
-
.map_err(|e| DependencyError::ResolutionFailed { message: e.to_string() })?;
|
|
228
|
-
|
|
229
|
-
Ok(Arc::new(json_value) as Arc<dyn Any + Send + Sync>)
|
|
230
|
-
})
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
fn singleton(&self) -> bool {
|
|
234
|
-
self.singleton
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
fn cacheable(&self) -> bool {
|
|
238
|
-
self.cacheable
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
/// Wrapper around a Ruby Value that preserves object identity for singleton mutations
|
|
243
|
-
///
|
|
244
|
-
/// This stores the Ruby object itself rather than a JSON snapshot, allowing
|
|
245
|
-
/// singleton dependencies to maintain mutable state across requests.
|
|
246
|
-
#[derive(Clone)]
|
|
247
|
-
pub struct RubyValueWrapper {
|
|
248
|
-
/// Thread-safe wrapper around Ruby Value
|
|
249
|
-
/// Opaque<Value> is Send + Sync per magnus design
|
|
250
|
-
value: Opaque<Value>,
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
impl RubyValueWrapper {
|
|
254
|
-
/// Create a new wrapper around a Ruby value
|
|
255
|
-
pub fn new(value: Value) -> Self {
|
|
256
|
-
Self {
|
|
257
|
-
value: Opaque::from(value),
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
/// Get the raw Ruby value directly
|
|
262
|
-
///
|
|
263
|
-
/// This preserves object identity for singletons, allowing mutations
|
|
264
|
-
/// to persist across requests.
|
|
265
|
-
pub fn get_value(&self, ruby: &Ruby) -> Value {
|
|
266
|
-
self.value.get_inner_with(ruby)
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
/// Convert the wrapped Ruby value to JSON
|
|
270
|
-
///
|
|
271
|
-
/// This is called fresh each time to capture any mutations to the object.
|
|
272
|
-
/// For singletons, this means we see updated counter values, etc.
|
|
273
|
-
pub fn to_json(&self, ruby: &Ruby) -> Result<JsonValue, Error> {
|
|
274
|
-
let value = self.value.get_inner_with(ruby);
|
|
275
|
-
ruby_value_to_json(ruby, value)
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
unsafe impl Send for RubyValueWrapper {}
|
|
280
|
-
unsafe impl Sync for RubyValueWrapper {}
|
|
281
|
-
|
|
282
|
-
/// Convert Ruby Value to serde_json::Value
|
|
283
|
-
fn ruby_value_to_json(ruby: &Ruby, value: Value) -> Result<JsonValue, Error> {
|
|
284
|
-
if value.is_nil() {
|
|
285
|
-
return Ok(JsonValue::Null);
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
let json_module: Value = ruby
|
|
289
|
-
.class_object()
|
|
290
|
-
.const_get("JSON")
|
|
291
|
-
.map_err(|_| Error::new(ruby.exception_runtime_error(), "JSON module not available"))?;
|
|
292
|
-
|
|
293
|
-
let json_string: String = json_module.funcall("generate", (value,))?;
|
|
294
|
-
serde_json::from_str(&json_string).map_err(|err| {
|
|
295
|
-
Error::new(
|
|
296
|
-
ruby.exception_runtime_error(),
|
|
297
|
-
format!("Failed to convert Ruby value to JSON: {err}"),
|
|
298
|
-
)
|
|
299
|
-
})
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
/// Convert serde_json::Value to Ruby Value
|
|
303
|
-
pub fn json_to_ruby(ruby: &Ruby, value: &JsonValue) -> Result<Value, Error> {
|
|
304
|
-
match value {
|
|
305
|
-
JsonValue::Null => Ok(ruby.qnil().as_value()),
|
|
306
|
-
JsonValue::Bool(b) => Ok(if *b {
|
|
307
|
-
ruby.qtrue().as_value()
|
|
308
|
-
} else {
|
|
309
|
-
ruby.qfalse().as_value()
|
|
310
|
-
}),
|
|
311
|
-
JsonValue::Number(num) => {
|
|
312
|
-
if let Some(i) = num.as_i64() {
|
|
313
|
-
Ok(ruby.integer_from_i64(i).as_value())
|
|
314
|
-
} else if let Some(f) = num.as_f64() {
|
|
315
|
-
Ok(ruby.float_from_f64(f).as_value())
|
|
316
|
-
} else {
|
|
317
|
-
Ok(ruby.qnil().as_value())
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
JsonValue::String(str_val) => Ok(ruby.str_new(str_val).as_value()),
|
|
321
|
-
JsonValue::Array(items) => {
|
|
322
|
-
let array = ruby.ary_new();
|
|
323
|
-
for item in items {
|
|
324
|
-
array.push(json_to_ruby(ruby, item)?)?;
|
|
325
|
-
}
|
|
326
|
-
Ok(array.as_value())
|
|
327
|
-
}
|
|
328
|
-
JsonValue::Object(map) => {
|
|
329
|
-
let hash = ruby.hash_new();
|
|
330
|
-
for (key, item) in map {
|
|
331
|
-
hash.aset(ruby.str_new(key), json_to_ruby(ruby, item)?)?;
|
|
332
|
-
}
|
|
333
|
-
Ok(hash.as_value())
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
/// Helper to extract keyword arguments from Ruby options hash
|
|
339
|
-
pub fn extract_di_options(ruby: &Ruby, options: Value) -> Result<(Vec<String>, bool, bool), Error> {
|
|
340
|
-
if options.is_nil() {
|
|
341
|
-
return Ok((Vec::new(), false, true));
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
let hash = RHash::try_convert(options)?;
|
|
345
|
-
|
|
346
|
-
let depends_on = if let Some(deps_value) = get_kw(ruby, hash, "depends_on") {
|
|
347
|
-
if deps_value.is_nil() {
|
|
348
|
-
Vec::new()
|
|
349
|
-
} else {
|
|
350
|
-
Vec::<String>::try_convert(deps_value)?
|
|
351
|
-
}
|
|
352
|
-
} else {
|
|
353
|
-
Vec::new()
|
|
354
|
-
};
|
|
355
|
-
|
|
356
|
-
let singleton = if let Some(singleton_value) = get_kw(ruby, hash, "singleton") {
|
|
357
|
-
bool::try_convert(singleton_value).unwrap_or(false)
|
|
358
|
-
} else {
|
|
359
|
-
false
|
|
360
|
-
};
|
|
361
|
-
|
|
362
|
-
let cacheable = if let Some(cacheable_value) = get_kw(ruby, hash, "cacheable") {
|
|
363
|
-
bool::try_convert(cacheable_value).unwrap_or(true)
|
|
364
|
-
} else {
|
|
365
|
-
true
|
|
366
|
-
};
|
|
367
|
-
|
|
368
|
-
Ok((depends_on, singleton, cacheable))
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
/// Get keyword argument from Ruby hash (tries both symbol and string keys)
|
|
372
|
-
fn get_kw(ruby: &Ruby, hash: RHash, name: &str) -> Option<Value> {
|
|
373
|
-
let sym = ruby.intern(name);
|
|
374
|
-
hash.get(sym).or_else(|| hash.get(name))
|
|
375
|
-
}
|
|
1
|
+
//! Ruby dependency injection implementations
|
|
2
|
+
//!
|
|
3
|
+
//! This module provides Ruby-specific implementations of the Dependency trait,
|
|
4
|
+
//! bridging Ruby values and Procs to the Rust DI system.
|
|
5
|
+
|
|
6
|
+
#![allow(dead_code)]
|
|
7
|
+
|
|
8
|
+
pub mod builder;
|
|
9
|
+
|
|
10
|
+
pub use builder::build_dependency_container;
|
|
11
|
+
|
|
12
|
+
use http::Request;
|
|
13
|
+
use magnus::prelude::*;
|
|
14
|
+
use magnus::value::{InnerValue, Opaque};
|
|
15
|
+
use magnus::{Error, RHash, Ruby, TryConvert, Value};
|
|
16
|
+
use serde_json::Value as JsonValue;
|
|
17
|
+
use spikard_core::di::{Dependency, DependencyError, ResolvedDependencies};
|
|
18
|
+
use spikard_core::request_data::RequestData;
|
|
19
|
+
use std::any::Any;
|
|
20
|
+
use std::pin::Pin;
|
|
21
|
+
use std::sync::Arc;
|
|
22
|
+
|
|
23
|
+
/// Ruby value dependency
|
|
24
|
+
///
|
|
25
|
+
/// Wraps a Ruby object as a static dependency value
|
|
26
|
+
pub struct RubyValueDependency {
|
|
27
|
+
key: String,
|
|
28
|
+
value: Opaque<Value>,
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
impl RubyValueDependency {
|
|
32
|
+
pub fn new(key: String, value: Value) -> Self {
|
|
33
|
+
Self {
|
|
34
|
+
key,
|
|
35
|
+
value: Opaque::from(value),
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
impl Dependency for RubyValueDependency {
|
|
41
|
+
fn key(&self) -> &str {
|
|
42
|
+
&self.key
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
fn depends_on(&self) -> Vec<String> {
|
|
46
|
+
Vec::new()
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
fn resolve(
|
|
50
|
+
&self,
|
|
51
|
+
_request: &Request<()>,
|
|
52
|
+
_request_data: &RequestData,
|
|
53
|
+
_resolved: &ResolvedDependencies,
|
|
54
|
+
) -> Pin<Box<dyn std::future::Future<Output = Result<Arc<dyn Any + Send + Sync>, DependencyError>> + Send + '_>>
|
|
55
|
+
{
|
|
56
|
+
Box::pin(async move {
|
|
57
|
+
let ruby = Ruby::get().map_err(|e| DependencyError::ResolutionFailed { message: e.to_string() })?;
|
|
58
|
+
|
|
59
|
+
let value = self.value.get_inner_with(&ruby);
|
|
60
|
+
|
|
61
|
+
let json_value = ruby_value_to_json(&ruby, value)
|
|
62
|
+
.map_err(|e| DependencyError::ResolutionFailed { message: e.to_string() })?;
|
|
63
|
+
|
|
64
|
+
Ok(Arc::new(json_value) as Arc<dyn Any + Send + Sync>)
|
|
65
|
+
})
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
fn singleton(&self) -> bool {
|
|
69
|
+
true
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
fn cacheable(&self) -> bool {
|
|
73
|
+
true
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/// Ruby factory dependency
|
|
78
|
+
///
|
|
79
|
+
/// Wraps a Ruby Proc as a factory dependency
|
|
80
|
+
pub struct RubyFactoryDependency {
|
|
81
|
+
key: String,
|
|
82
|
+
factory: Opaque<Value>,
|
|
83
|
+
depends_on: Vec<String>,
|
|
84
|
+
singleton: bool,
|
|
85
|
+
cacheable: bool,
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
impl RubyFactoryDependency {
|
|
89
|
+
pub fn new(key: String, factory: Value, depends_on: Vec<String>, singleton: bool, cacheable: bool) -> Self {
|
|
90
|
+
Self {
|
|
91
|
+
key,
|
|
92
|
+
factory: Opaque::from(factory),
|
|
93
|
+
depends_on,
|
|
94
|
+
singleton,
|
|
95
|
+
cacheable,
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
impl Dependency for RubyFactoryDependency {
|
|
101
|
+
fn key(&self) -> &str {
|
|
102
|
+
&self.key
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
fn depends_on(&self) -> Vec<String> {
|
|
106
|
+
self.depends_on.clone()
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
fn resolve(
|
|
110
|
+
&self,
|
|
111
|
+
_request: &Request<()>,
|
|
112
|
+
_request_data: &RequestData,
|
|
113
|
+
resolved: &ResolvedDependencies,
|
|
114
|
+
) -> Pin<Box<dyn std::future::Future<Output = Result<Arc<dyn Any + Send + Sync>, DependencyError>> + Send + '_>>
|
|
115
|
+
{
|
|
116
|
+
let factory = self.factory;
|
|
117
|
+
let depends_on = self.depends_on.clone();
|
|
118
|
+
let key = self.key.clone();
|
|
119
|
+
let is_singleton = self.singleton;
|
|
120
|
+
let resolved_clone = resolved.clone();
|
|
121
|
+
|
|
122
|
+
let resolved_deps: Vec<(String, JsonValue)> = depends_on
|
|
123
|
+
.iter()
|
|
124
|
+
.filter_map(|dep_key| {
|
|
125
|
+
if let Some(json_value) = resolved.get::<JsonValue>(dep_key) {
|
|
126
|
+
return Some((dep_key.clone(), (*json_value).clone()));
|
|
127
|
+
}
|
|
128
|
+
if let Some(wrapper) = resolved.get::<RubyValueWrapper>(dep_key)
|
|
129
|
+
&& let Ok(ruby) = Ruby::get()
|
|
130
|
+
&& let Ok(json) = wrapper.to_json(&ruby)
|
|
131
|
+
{
|
|
132
|
+
return Some((dep_key.clone(), json));
|
|
133
|
+
}
|
|
134
|
+
None
|
|
135
|
+
})
|
|
136
|
+
.collect();
|
|
137
|
+
|
|
138
|
+
Box::pin(async move {
|
|
139
|
+
let ruby = Ruby::get().map_err(|e| DependencyError::ResolutionFailed { message: e.to_string() })?;
|
|
140
|
+
|
|
141
|
+
let args: Result<Vec<Value>, DependencyError> = depends_on
|
|
142
|
+
.iter()
|
|
143
|
+
.filter_map(|dep_key| resolved_deps.iter().find(|(k, _)| k == dep_key).map(|(_, v)| v))
|
|
144
|
+
.map(|dep_value| {
|
|
145
|
+
json_to_ruby(&ruby, dep_value).map_err(|e| DependencyError::ResolutionFailed {
|
|
146
|
+
message: format!("Failed to convert dependency value: {}", e),
|
|
147
|
+
})
|
|
148
|
+
})
|
|
149
|
+
.collect();
|
|
150
|
+
let args = args?;
|
|
151
|
+
|
|
152
|
+
let factory_value = factory.get_inner_with(&ruby);
|
|
153
|
+
|
|
154
|
+
if !factory_value
|
|
155
|
+
.respond_to("call", false)
|
|
156
|
+
.map_err(|e| DependencyError::ResolutionFailed { message: e.to_string() })?
|
|
157
|
+
{
|
|
158
|
+
return Err(DependencyError::ResolutionFailed {
|
|
159
|
+
message: format!("Dependency factory for '{}' is not callable", key),
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
let result: Value = if !args.is_empty() {
|
|
164
|
+
let args_array = ruby.ary_new();
|
|
165
|
+
for arg in &args {
|
|
166
|
+
args_array.push(*arg).map_err(|e| DependencyError::ResolutionFailed {
|
|
167
|
+
message: format!("Failed to push arg to array: {}", e),
|
|
168
|
+
})?;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
let splat_lambda = ruby
|
|
172
|
+
.eval::<Value>("lambda { |proc, args| proc.call(*args) }")
|
|
173
|
+
.map_err(|e| DependencyError::ResolutionFailed {
|
|
174
|
+
message: format!("Failed to create splat lambda: {}", e),
|
|
175
|
+
})?;
|
|
176
|
+
|
|
177
|
+
splat_lambda.funcall("call", (factory_value, args_array))
|
|
178
|
+
} else {
|
|
179
|
+
factory_value.funcall("call", ())
|
|
180
|
+
}
|
|
181
|
+
.map_err(|e| DependencyError::ResolutionFailed {
|
|
182
|
+
message: format!("Failed to call factory for '{}': {}", key, e),
|
|
183
|
+
})?;
|
|
184
|
+
|
|
185
|
+
let (value_to_convert, cleanup_callback) = if result.is_kind_of(ruby.class_array()) {
|
|
186
|
+
let array = magnus::RArray::from_value(result).ok_or_else(|| DependencyError::ResolutionFailed {
|
|
187
|
+
message: format!("Failed to convert result to array for '{}'", key),
|
|
188
|
+
})?;
|
|
189
|
+
|
|
190
|
+
let len = array.len();
|
|
191
|
+
if len == 2 {
|
|
192
|
+
let resource: Value = array.entry(0).map_err(|e| DependencyError::ResolutionFailed {
|
|
193
|
+
message: format!("Failed to extract resource from array for '{}': {}", key, e),
|
|
194
|
+
})?;
|
|
195
|
+
|
|
196
|
+
let cleanup: Value = array.entry(1).map_err(|e| DependencyError::ResolutionFailed {
|
|
197
|
+
message: format!("Failed to extract cleanup callback for '{}': {}", key, e),
|
|
198
|
+
})?;
|
|
199
|
+
|
|
200
|
+
(resource, Some(cleanup))
|
|
201
|
+
} else {
|
|
202
|
+
(result, None)
|
|
203
|
+
}
|
|
204
|
+
} else {
|
|
205
|
+
(result, None)
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
if let Some(cleanup_proc) = cleanup_callback {
|
|
209
|
+
let cleanup_opaque = Opaque::from(cleanup_proc);
|
|
210
|
+
|
|
211
|
+
resolved_clone.add_cleanup_task(Box::new(move || {
|
|
212
|
+
Box::pin(async move {
|
|
213
|
+
if let Ok(ruby) = Ruby::get() {
|
|
214
|
+
let proc = cleanup_opaque.get_inner_with(&ruby);
|
|
215
|
+
let _ = proc.funcall::<_, _, Value>("call", ());
|
|
216
|
+
}
|
|
217
|
+
})
|
|
218
|
+
}));
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if is_singleton {
|
|
222
|
+
let wrapper = RubyValueWrapper::new(value_to_convert);
|
|
223
|
+
return Ok(Arc::new(wrapper) as Arc<dyn Any + Send + Sync>);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
let json_value = ruby_value_to_json(&ruby, value_to_convert)
|
|
227
|
+
.map_err(|e| DependencyError::ResolutionFailed { message: e.to_string() })?;
|
|
228
|
+
|
|
229
|
+
Ok(Arc::new(json_value) as Arc<dyn Any + Send + Sync>)
|
|
230
|
+
})
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
fn singleton(&self) -> bool {
|
|
234
|
+
self.singleton
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
fn cacheable(&self) -> bool {
|
|
238
|
+
self.cacheable
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/// Wrapper around a Ruby Value that preserves object identity for singleton mutations
|
|
243
|
+
///
|
|
244
|
+
/// This stores the Ruby object itself rather than a JSON snapshot, allowing
|
|
245
|
+
/// singleton dependencies to maintain mutable state across requests.
|
|
246
|
+
#[derive(Clone)]
|
|
247
|
+
pub struct RubyValueWrapper {
|
|
248
|
+
/// Thread-safe wrapper around Ruby Value
|
|
249
|
+
/// Opaque<Value> is Send + Sync per magnus design
|
|
250
|
+
value: Opaque<Value>,
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
impl RubyValueWrapper {
|
|
254
|
+
/// Create a new wrapper around a Ruby value
|
|
255
|
+
pub fn new(value: Value) -> Self {
|
|
256
|
+
Self {
|
|
257
|
+
value: Opaque::from(value),
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/// Get the raw Ruby value directly
|
|
262
|
+
///
|
|
263
|
+
/// This preserves object identity for singletons, allowing mutations
|
|
264
|
+
/// to persist across requests.
|
|
265
|
+
pub fn get_value(&self, ruby: &Ruby) -> Value {
|
|
266
|
+
self.value.get_inner_with(ruby)
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/// Convert the wrapped Ruby value to JSON
|
|
270
|
+
///
|
|
271
|
+
/// This is called fresh each time to capture any mutations to the object.
|
|
272
|
+
/// For singletons, this means we see updated counter values, etc.
|
|
273
|
+
pub fn to_json(&self, ruby: &Ruby) -> Result<JsonValue, Error> {
|
|
274
|
+
let value = self.value.get_inner_with(ruby);
|
|
275
|
+
ruby_value_to_json(ruby, value)
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
unsafe impl Send for RubyValueWrapper {}
|
|
280
|
+
unsafe impl Sync for RubyValueWrapper {}
|
|
281
|
+
|
|
282
|
+
/// Convert Ruby Value to serde_json::Value
|
|
283
|
+
fn ruby_value_to_json(ruby: &Ruby, value: Value) -> Result<JsonValue, Error> {
|
|
284
|
+
if value.is_nil() {
|
|
285
|
+
return Ok(JsonValue::Null);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
let json_module: Value = ruby
|
|
289
|
+
.class_object()
|
|
290
|
+
.const_get("JSON")
|
|
291
|
+
.map_err(|_| Error::new(ruby.exception_runtime_error(), "JSON module not available"))?;
|
|
292
|
+
|
|
293
|
+
let json_string: String = json_module.funcall("generate", (value,))?;
|
|
294
|
+
serde_json::from_str(&json_string).map_err(|err| {
|
|
295
|
+
Error::new(
|
|
296
|
+
ruby.exception_runtime_error(),
|
|
297
|
+
format!("Failed to convert Ruby value to JSON: {err}"),
|
|
298
|
+
)
|
|
299
|
+
})
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/// Convert serde_json::Value to Ruby Value
|
|
303
|
+
pub fn json_to_ruby(ruby: &Ruby, value: &JsonValue) -> Result<Value, Error> {
|
|
304
|
+
match value {
|
|
305
|
+
JsonValue::Null => Ok(ruby.qnil().as_value()),
|
|
306
|
+
JsonValue::Bool(b) => Ok(if *b {
|
|
307
|
+
ruby.qtrue().as_value()
|
|
308
|
+
} else {
|
|
309
|
+
ruby.qfalse().as_value()
|
|
310
|
+
}),
|
|
311
|
+
JsonValue::Number(num) => {
|
|
312
|
+
if let Some(i) = num.as_i64() {
|
|
313
|
+
Ok(ruby.integer_from_i64(i).as_value())
|
|
314
|
+
} else if let Some(f) = num.as_f64() {
|
|
315
|
+
Ok(ruby.float_from_f64(f).as_value())
|
|
316
|
+
} else {
|
|
317
|
+
Ok(ruby.qnil().as_value())
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
JsonValue::String(str_val) => Ok(ruby.str_new(str_val).as_value()),
|
|
321
|
+
JsonValue::Array(items) => {
|
|
322
|
+
let array = ruby.ary_new();
|
|
323
|
+
for item in items {
|
|
324
|
+
array.push(json_to_ruby(ruby, item)?)?;
|
|
325
|
+
}
|
|
326
|
+
Ok(array.as_value())
|
|
327
|
+
}
|
|
328
|
+
JsonValue::Object(map) => {
|
|
329
|
+
let hash = ruby.hash_new();
|
|
330
|
+
for (key, item) in map {
|
|
331
|
+
hash.aset(ruby.str_new(key), json_to_ruby(ruby, item)?)?;
|
|
332
|
+
}
|
|
333
|
+
Ok(hash.as_value())
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/// Helper to extract keyword arguments from Ruby options hash
|
|
339
|
+
pub fn extract_di_options(ruby: &Ruby, options: Value) -> Result<(Vec<String>, bool, bool), Error> {
|
|
340
|
+
if options.is_nil() {
|
|
341
|
+
return Ok((Vec::new(), false, true));
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
let hash = RHash::try_convert(options)?;
|
|
345
|
+
|
|
346
|
+
let depends_on = if let Some(deps_value) = get_kw(ruby, hash, "depends_on") {
|
|
347
|
+
if deps_value.is_nil() {
|
|
348
|
+
Vec::new()
|
|
349
|
+
} else {
|
|
350
|
+
Vec::<String>::try_convert(deps_value)?
|
|
351
|
+
}
|
|
352
|
+
} else {
|
|
353
|
+
Vec::new()
|
|
354
|
+
};
|
|
355
|
+
|
|
356
|
+
let singleton = if let Some(singleton_value) = get_kw(ruby, hash, "singleton") {
|
|
357
|
+
bool::try_convert(singleton_value).unwrap_or(false)
|
|
358
|
+
} else {
|
|
359
|
+
false
|
|
360
|
+
};
|
|
361
|
+
|
|
362
|
+
let cacheable = if let Some(cacheable_value) = get_kw(ruby, hash, "cacheable") {
|
|
363
|
+
bool::try_convert(cacheable_value).unwrap_or(true)
|
|
364
|
+
} else {
|
|
365
|
+
true
|
|
366
|
+
};
|
|
367
|
+
|
|
368
|
+
Ok((depends_on, singleton, cacheable))
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/// Get keyword argument from Ruby hash (tries both symbol and string keys)
|
|
372
|
+
fn get_kw(ruby: &Ruby, hash: RHash, name: &str) -> Option<Value> {
|
|
373
|
+
let sym = ruby.intern(name);
|
|
374
|
+
hash.get(sym).or_else(|| hash.get(name))
|
|
375
|
+
}
|