case_transform-rust-extensions 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1510b6b38544e9201d85f5fa7ca9916fa03af4d3
4
- data.tar.gz: 3ccaf38b9862b444a67f2fe99757aea221245566
3
+ metadata.gz: c23bbb4a1a09435f6e23a4dc9dd7e0f00ae3338c
4
+ data.tar.gz: 3783d4c8dfe4c3570f175891ecdc61a61bd21189
5
5
  SHA512:
6
- metadata.gz: ae7576a7b437c5d212da5536eea4ba3ccc9ec9ab1235a25888a50f619d7f159d61fdb6b3c620c35170706ed79fb48e6274554fd1bf34e6d538ec4c301298b656
7
- data.tar.gz: a37849d85d6f505ccab397f7032a5290fb1af285cc0c1d820f08842d05c101f7a0340a837b76bd09481520296a70aff566e2a1abfe770eb5a9ea42a636afafe3
6
+ metadata.gz: 6a141766c0ce9d6c2f81cf8d137591d43f21f8e9a54549452daff7afecfc44338ad58a030a2728b3a9fdb04f018e80982b3bbb77a4f986397af20b2fef19304d
7
+ data.tar.gz: ec0051db880560bf5a899056f6ccfd8c0109129112b383215a08f50c906c7baff8a06e392b24c1222fed11b1636fd6aa24516c7e90d244394d5bfc9b28025d66
data/README.md CHANGED
@@ -7,16 +7,39 @@ Extraction of the key_transform abilities of ActiveModelSerializers
7
7
  [![Test Coverage](https://codeclimate.com/repos/57dafbcc628330006c001312/badges/5f190457aba7c5d5d78c/coverage.svg)](https://codeclimate.com/repos/57dafbcc628330006c001312/coverage)
8
8
  [![Dependency Status](https://gemnasium.com/NullVoxPopuli/case_transform.svg)](https://gemnasium.com/NullVoxPopuli/case_transform)
9
9
 
10
+
11
+ ## Performance
12
+
13
+ ```
14
+ $ ruby benchmark.rb
15
+
16
+ Rust: camel: 238774.9 i/s
17
+ Ruby: camel: 13107.3 i/s - 18.22x slower
18
+
19
+ Rust: camel_lower: 230641.5 i/s
20
+ Ruby: camel_lower: 8244.1 i/s - 27.98x slower
21
+
22
+ Rust: dash: 243197.8 i/s
23
+ Ruby: dash: 20350.9 i/s - 11.95x slower
24
+
25
+ Ruby: unaltered: 6030498.8 i/s
26
+ Rust: unaltered: 1960989.6 i/s - 3.08x slower
27
+
28
+ Rust: underscore: 291052.3 i/s
29
+ Ruby: underscore: 169458.2 i/s - 1.72x slower
30
+
31
+ ```
32
+
10
33
  ## Install
11
34
 
12
35
  ```ruby
13
- gem 'case_transform'
36
+ gem 'case_transform-rust-extensions', require: 'case_transform'
14
37
  ```
15
38
 
16
39
  or
17
40
 
18
41
  ```bash
19
- gem install case_transform
42
+ gem install case_transform-rust-extensions
20
43
  ```
21
44
 
22
45
  ## Usage
@@ -0,0 +1,18 @@
1
+ [package]
2
+ name = "case_transform"
3
+ version = "0.1.0"
4
+ authors = ["L. Preston Sego III <LPSego3+dev@gmail.com>"]
5
+ repository = "https://github.com/NullVoxPopuli/case_transform"
6
+
7
+ [package.metadata.thermite]
8
+ github_releases = true
9
+ github_release_type = "latest"
10
+
11
+ [lib]
12
+ name = "case_transform"
13
+ crate-type = ["dylib"]
14
+
15
+ [dependencies]
16
+ # Inflector = "0.3.1"
17
+ inflections = { git = "https://github.com/calebmer/inflections" }
18
+ ruru = "0.8.0"
@@ -0,0 +1,132 @@
1
+ #[macro_use]
2
+ extern crate ruru;
3
+ extern crate inflections;
4
+
5
+ // use inflector::cases::{camelcase, classcase, kebabcase, snakecase};
6
+ use inflections::Inflect;
7
+
8
+ use ruru::{Class, Object, VerifiedObject, RString, Hash, Array, Symbol, AnyObject};
9
+ use ruru::types::ValueType;
10
+ use ruru::result::Error as RuruError;
11
+
12
+ trait Transform: Object {
13
+ fn transform(&self, transform_function: &Fn(String) -> String) -> AnyObject;
14
+ }
15
+
16
+ impl Transform for AnyObject {
17
+ fn transform(&self, _transform_function: &Fn(String) -> String) -> AnyObject {
18
+ self.clone()
19
+ }
20
+ }
21
+
22
+ impl Transform for RString {
23
+ fn transform(&self, transform_function: &Fn(String) -> String) -> AnyObject {
24
+ let result = transform_function(self.to_string());
25
+
26
+ RString::new(&result).to_any_object()
27
+ }
28
+ }
29
+
30
+ impl Transform for Symbol {
31
+ fn transform(&self, transform_function: &Fn(String) -> String) -> AnyObject {
32
+ let result = transform_function(self.to_string());
33
+
34
+ Symbol::new(&result).to_any_object()
35
+ }
36
+ }
37
+
38
+ impl Transform for Hash {
39
+ fn transform(&self, transform_function: &Fn(String) -> String) -> AnyObject {
40
+ let mut result = Hash::new();
41
+
42
+ self.each(|key, value| {
43
+ let new_key = transform(key, transform_function);
44
+ let new_value = match value.ty() {
45
+ ValueType::Hash => transform(value, transform_function),
46
+ _ => value,
47
+ };
48
+
49
+ result.store(new_key, new_value);
50
+ });
51
+
52
+ result.to_any_object()
53
+ }
54
+ }
55
+
56
+ impl Transform for Array {
57
+ fn transform(&self, transform_function: &Fn(String) -> String) -> AnyObject {
58
+ // Temp hack to consume &self for iterator
59
+ let result = unsafe { self.to_any_object().to::<Array>() };
60
+
61
+ result.into_iter()
62
+ .map(|item| transform(item, transform_function))
63
+ .collect::<Array>()
64
+ .to_any_object()
65
+ }
66
+ }
67
+
68
+ trait TryTransform: Object {
69
+ fn try_transform<T>(&self,
70
+ transform_function: &Fn(String) -> String)
71
+ -> Result<AnyObject, RuruError>
72
+ where T: VerifiedObject + Transform
73
+ {
74
+ self.try_convert_to::<T>().map(|object| object.transform(transform_function))
75
+ }
76
+ }
77
+
78
+ impl TryTransform for AnyObject {}
79
+
80
+ fn transform(object: AnyObject, key_transform: &Fn(String) -> String) -> AnyObject {
81
+ let result = object.try_transform::<RString>(key_transform)
82
+ .or_else(|_| object.try_transform::<Symbol>(key_transform))
83
+ .or_else(|_| object.try_transform::<Array>(key_transform))
84
+ .or_else(|_| object.try_transform::<Hash>(key_transform))
85
+ .or_else(|_| object.try_transform::<AnyObject>(key_transform));
86
+
87
+ result.unwrap()
88
+ }
89
+
90
+ fn to_pascal_case(key: String) -> String {
91
+ // classcase::to_class_case(key)
92
+ key.to_pascal_case()
93
+ }
94
+
95
+ fn to_camel_case(key: String) -> String {
96
+ // camelcase::to_camel_case(key)
97
+ key.to_camel_case()
98
+ }
99
+
100
+ fn to_dashed_case(key: String) -> String {
101
+ // kebabcase::to_kebab_case(key)
102
+ key.to_kebab_case()
103
+ }
104
+
105
+ fn to_snake_case(key: String) -> String {
106
+ // snakecase::to_snake_case(key)
107
+ key.to_snake_case()
108
+ }
109
+
110
+ class!(CaseTransform);
111
+
112
+ methods! (
113
+ CaseTransform,
114
+ _itself,
115
+
116
+ fn camel(object: AnyObject) -> AnyObject { transform(object.unwrap(), &to_pascal_case) }
117
+ fn camel_lower(object: AnyObject) -> AnyObject { transform(object.unwrap(), &to_camel_case) }
118
+ fn dash(object: AnyObject) -> AnyObject { transform(object.unwrap(), &to_dashed_case) }
119
+ fn underscore(object: AnyObject) -> AnyObject { transform(object.unwrap(), &to_snake_case) }
120
+ fn unaltered(object: AnyObject) -> AnyObject { object.unwrap() }
121
+ );
122
+
123
+ #[no_mangle]
124
+ pub extern "C" fn initialize_case_transform() {
125
+ Class::from_existing("CaseTransform").define(|itself| {
126
+ itself.def_self("camel", camel);
127
+ itself.def_self("camel_lower", camel_lower);
128
+ itself.def_self("dash", dash);
129
+ itself.def_self("underscore", underscore);
130
+ itself.def_self("unaltered", unaltered);
131
+ });
132
+ }
data/ext/extconf.rb ADDED
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+ # NOTE: Thi file must be tab-delimited
3
+ require 'mkmf'
4
+
5
+ create_makefile 'rutgem'
6
+
7
+ mkf = %{
8
+ .ONESHELL:
9
+ all:
10
+ pwd
11
+ ls -la
12
+ cd ./case_transform
13
+ pwd
14
+ cargo build
15
+
16
+ clean:
17
+ rm -rf ./case_transform/target
18
+
19
+ install: ;
20
+ }
21
+
22
+ File.write('Makefile', mkf)
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'case_transform/version'
3
+ require_relative './case_transform/version'
4
4
  require 'thermite/config'
5
5
  require 'fiddle'
6
6
 
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
  module CaseTransform
3
- VERSION = '0.1'.freeze
3
+ VERSION = '0.2'.freeze
4
+ RUST = true
4
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: case_transform-rust-extensions
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: '0.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - L. Preston Sego III
@@ -141,13 +141,16 @@ dependencies:
141
141
  description: Extraction of the key_transform abilities of ActiveModelSerializers
142
142
  email: LPSego3+dev@gmail.com
143
143
  executables: []
144
- extensions: []
144
+ extensions:
145
+ - ext/extconf.rb
145
146
  extra_rdoc_files: []
146
147
  files:
147
148
  - LICENSE
148
149
  - README.md
150
+ - ext/case_transform/Cargo.toml
151
+ - ext/case_transform/src/lib.rs
152
+ - ext/extconf.rb
149
153
  - lib/case_transform.rb
150
- - lib/case_transform/native.bundle
151
154
  - lib/case_transform/version.rb
152
155
  - lib/libcase_transform.so
153
156
  homepage: https://github.com/NullVoxPopuli/case_transform-rust-extensions
@@ -173,5 +176,5 @@ rubyforge_project:
173
176
  rubygems_version: 2.5.1
174
177
  signing_key:
175
178
  specification_version: 4
176
- summary: CaseTransform-0.1
179
+ summary: CaseTransform-0.2
177
180
  test_files: []
Binary file