Skip to content

Commit 77b154c

Browse files
committed
it works
1 parent 24f72d0 commit 77b154c

File tree

2 files changed

+82
-64
lines changed

2 files changed

+82
-64
lines changed

ext/case_transform/src/lib.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,43 @@ extern crate inflector;
1212
// use inflector::cases::classcase::to_class_case;
1313
use inflector::Inflector;
1414

15-
use ruru::{Class, Object, RString};
15+
use ruru::{Class, Object, RString, Hash};
1616

1717
methods! (
1818
RString,
1919
itself,
2020

21-
fn toSnakeCase() -> String {
22-
itself.to_string().to_snake_case()
21+
fn toSnakeCase() -> RString {
22+
RString::new(&itself.to_string().to_snake_case())
2323
}
2424

25-
fn toCamelCase() -> String {
26-
itself.to_string().to_camel_case()
25+
fn toCamelCase() -> RString {
26+
RString::new(&itself.to_string().to_camel_case())
2727
}
2828

29-
fn toClassCase() -> String {
30-
itself.to_string().to_class_case()
29+
fn toClassCase() -> RString {
30+
RString::new(&itself.to_string().to_class_case())
3131
}
3232

33-
fn toKebabCase() -> String {
34-
itself.to_string().to_kebab_case()
33+
fn toKebabCase() -> RString {
34+
RString::new(&itself.to_string().to_kebab_case())
3535
}
3636
);
3737

38+
// methods! (
39+
// Hash,
40+
// itself,
41+
//
42+
// fn deepTransformKeys() -> Hash {
43+
// let result = Hash::new()
44+
// itself.each(|key, value| {
45+
// result.store(key, value)
46+
// });
47+
// }
48+
// );
49+
3850
#[no_mangle]
39-
pub extern fn initialize_string() {
51+
pub extern fn initialize_case_transform() {
4052
Class::from_existing("String").define(|itself| {
4153
itself.def("to_snake_case", toSnakeCase);
4254
itself.def("to_camel_case", toCamelCase);

lib/case_transform.rb

Lines changed: 60 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,76 +2,82 @@
22

33
require 'case_transform/version'
44
require 'thermite/config'
5+
require 'fiddle'
56

67
ruby_dir = File.dirname(File.dirname(__FILE__))
78
ext_dir = ruby_dir + '/ext/case_transform'
89
config = Thermite::Config.new(cargo_project_path: ext_dir, ruby_project_path: ruby_dir)
910
# Do I have to use fiddle? :-\
10-
11+
library = Fiddle.dlopen(config.ruby_extension_path)
12+
func = Fiddle::Function.new(
13+
library['initialize_case_transform'],
14+
[], Fiddle::TYPE_VOIDP
15+
)
16+
func.call
1117

1218
module CaseTransform
1319
module_function
1420

15-
# Transforms values to UpperCamelCase or PascalCase.
16-
#
17-
# @example:
18-
# "some_key" => "SomeKey",
19-
def camel(value)
20-
case value
21-
when Array then value.map { |item| camel(item) }
22-
when Hash then value.deep_transform_keys! { |key| camel(key) }
23-
when Symbol then camel(value.to_s).to_sym
24-
when String then value.to_snake_case.to_class_case
25-
else value
26-
end
21+
def deep_transform_keys(hash, &block)
22+
result = {}
23+
hash.each do |key, value|
24+
result[yield(key)] = value.is_a?(Hash) ? value.deep_transform_keys(&block) : value
2725
end
26+
result
27+
end
2828

29-
# Transforms values to camelCase.
30-
#
31-
# @example:
32-
# "some_key" => "someKey",
33-
def camel_lower(value)
34-
case value
35-
when Array then value.map { |item| camel_lower(item) }
36-
when Hash then value.deep_transform_keys! { |key| camel_lower(key) }
37-
when Symbol then camel_lower(value.to_s).to_sym
38-
when String then value.to_snake_case.to_camel_case
39-
else value
40-
end
29+
def transform(value, via)
30+
case value
31+
when Array then value.map { |item| send(via, item) }
32+
when Hash then deep_transform_keys(value) { |key| send(via, key) }
33+
when Symbol then send(via, value.to_s).to_sym
34+
when String then yield(value)
35+
else value
4136
end
37+
end
4238

43-
# Transforms values to dashed-case.
44-
# This is the default case for the JsonApi adapter.
45-
#
46-
# @example:
47-
# "some_key" => "some-key",
48-
def dash(value)
49-
case value
50-
when Array then value.map { |item| dash(item) }
51-
when Hash then value.deep_transform_keys! { |key| dash(key) }
52-
when Symbol then dash(value.to_s).to_sym
53-
when String then value.to_snake_case.to_kebab_case
54-
else value
55-
end
39+
# Transforms values to UpperCamelCase or PascalCase.
40+
#
41+
# @example:
42+
# "some_key" => "SomeKey",
43+
def camel(value)
44+
transform(value, :camel) do |v|
45+
v.to_snake_case.to_class_case
5646
end
47+
end
5748

58-
# Transforms values to underscore_case.
59-
# This is the default case for deserialization in the JsonApi adapter.
60-
#
61-
# @example:
62-
# "some-key" => "some_key",
63-
def underscore(value)
64-
case value
65-
when Array then value.map { |item| underscore(item) }
66-
when Hash then value.deep_transform_keys! { |key| underscore(key) }
67-
when Symbol then underscore(value.to_s).to_sym
68-
when String then value.to_snake_case
69-
else value
70-
end
49+
# Transforms values to camelCase.
50+
#
51+
# @example:
52+
# "some_key" => "someKey",
53+
def camel_lower(value)
54+
transform(value, :camel_lower) do |v|
55+
v.to_snake_case.to_camel_case
7156
end
57+
end
7258

73-
# Returns the value unaltered
74-
def unaltered(value)
75-
value
59+
# Transforms values to dashed-case.
60+
# This is the default case for the JsonApi adapter.
61+
#
62+
# @example:
63+
# "some_key" => "some-key",
64+
def dash(value)
65+
transform(value, :dash) do |v|
66+
v.to_snake_case.to_kebab_case
7667
end
68+
end
69+
70+
# Transforms values to underscore_case.
71+
# This is the default case for deserialization in the JsonApi adapter.
72+
#
73+
# @example:
74+
# "some-key" => "some_key",
75+
def underscore(value)
76+
transform(value, :underscore, &:to_snake_case)
77+
end
78+
79+
# Returns the value unaltered
80+
def unaltered(value)
81+
value
82+
end
7783
end

0 commit comments

Comments
 (0)