burke-monkeysupport 0.0.7 → 0.1.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.
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.1.1
|
@@ -7,10 +7,11 @@
|
|
7
7
|
#include "activesupport_inflector.h"
|
8
8
|
#include "ruby.h"
|
9
9
|
|
10
|
+
//rb_raise(rb_eTypeError, "Non-ASCII String");
|
11
|
+
|
10
12
|
VALUE activesupport_inflector_underscore(VALUE self, VALUE rstr)
|
11
13
|
{
|
12
|
-
|
13
|
-
// Check_Type(rstr, T_STRING);
|
14
|
+
Check_Type(rstr, T_STRING);
|
14
15
|
|
15
16
|
VALUE ret = rb_str_new("", 0);
|
16
17
|
char * ip = StringValuePtr(rstr);
|
@@ -44,18 +45,24 @@ VALUE activesupport_inflector_underscore(VALUE self, VALUE rstr)
|
|
44
45
|
|
45
46
|
VALUE activesupport_inflector_parameterize(VALUE self, VALUE str, VALUE sep)
|
46
47
|
{
|
47
|
-
|
48
|
-
|
49
|
-
|
48
|
+
Check_Type(str, T_STRING);
|
49
|
+
Check_Type(sep, T_STRING);
|
50
|
+
|
51
|
+
VALUE mActiveSupport = rb_define_module("ActiveSupport");
|
52
|
+
VALUE mInflector = rb_define_module_under(mActiveSupport, "Inflector");
|
53
|
+
VALUE transliterated = rb_funcall(mInflector, rb_intern("transliterate"), 1, str);
|
54
|
+
|
55
|
+
Check_Type(transliterated, T_STRING); // You never know...
|
50
56
|
|
51
57
|
VALUE ret = rb_str_new("", 0);
|
52
58
|
int sep_len = RSTRING_LEN(sep);
|
53
|
-
int ilen = RSTRING_LEN(
|
54
|
-
char * ip = RSTRING_PTR(
|
59
|
+
int ilen = RSTRING_LEN(transliterated);
|
60
|
+
char * ip = RSTRING_PTR(transliterated);
|
55
61
|
bool separated = true;
|
56
62
|
int i;
|
57
63
|
char tmp;
|
58
|
-
|
64
|
+
|
65
|
+
|
59
66
|
for (i = 0; i < ilen; i++, ip++) {
|
60
67
|
if (isalnum(*ip) || *ip == '-' || *ip == '_' || *ip == '+') { // normal char
|
61
68
|
separated = false;
|
@@ -83,8 +90,7 @@ VALUE activesupport_inflector_parameterize(VALUE self, VALUE str, VALUE sep)
|
|
83
90
|
|
84
91
|
VALUE activesupport_inflector_dasherize(VALUE self, VALUE str)
|
85
92
|
{
|
86
|
-
|
87
|
-
// Check_Type(str, T_STRING);
|
93
|
+
Check_Type(str, T_STRING);
|
88
94
|
|
89
95
|
char * out = ALLOC_N(char, RSTRING_LEN(str) + 1);
|
90
96
|
char * ip = RSTRING_PTR(str);
|
@@ -105,8 +111,7 @@ VALUE activesupport_inflector_dasherize(VALUE self, VALUE str)
|
|
105
111
|
|
106
112
|
VALUE activesupport_inflector_demodulize(VALUE self, VALUE rstr)
|
107
113
|
{
|
108
|
-
|
109
|
-
// Check_Type(rstr, T_STRING);
|
114
|
+
Check_Type(rstr, T_STRING);
|
110
115
|
|
111
116
|
char * str = RSTRING_PTR(rstr);
|
112
117
|
char * ip = str;
|
@@ -129,8 +134,7 @@ VALUE activesupport_inflector_demodulize(VALUE self, VALUE rstr)
|
|
129
134
|
|
130
135
|
VALUE activesupport_inflector_camelize(VALUE self, VALUE str, VALUE first_letter_uppercase)
|
131
136
|
{
|
132
|
-
|
133
|
-
// Check_Type(str, T_STRING);
|
137
|
+
Check_Type(str, T_STRING);
|
134
138
|
|
135
139
|
VALUE ret = rb_str_new("", 0);
|
136
140
|
bool cap_next = RTEST(first_letter_uppercase);
|
@@ -161,8 +165,7 @@ VALUE activesupport_inflector_camelize(VALUE self, VALUE str, VALUE first_letter
|
|
161
165
|
|
162
166
|
VALUE activesupport_inflector_foreign_key(VALUE self, VALUE str, VALUE use_underscore)
|
163
167
|
{
|
164
|
-
|
165
|
-
// Check_Type(str, T_STRING);
|
168
|
+
Check_Type(str, T_STRING);
|
166
169
|
|
167
170
|
VALUE ret = activesupport_inflector_underscore(self, activesupport_inflector_demodulize(self, str));
|
168
171
|
|
@@ -203,8 +206,7 @@ static char * itoa(int n)
|
|
203
206
|
|
204
207
|
VALUE activesupport_inflector_ordinalize(VALUE self, VALUE rn)
|
205
208
|
{
|
206
|
-
|
207
|
-
// Check_Type(rn, T_FIXNUM);
|
209
|
+
Check_Type(rn, T_FIXNUM);
|
208
210
|
|
209
211
|
int n = FIX2INT(rn);
|
210
212
|
VALUE ret = rb_str_new2(itoa(n));
|
@@ -1,4 +1,3 @@
|
|
1
|
-
|
2
1
|
module ActiveSupport
|
3
2
|
module Inflector
|
4
3
|
|
@@ -29,87 +28,16 @@ module ActiveSupport
|
|
29
28
|
:activesupport_inflector_demodulize,
|
30
29
|
[:string]) # class_name_in_module
|
31
30
|
|
32
|
-
|
31
|
+
monkey_c_proxy(:ordinalize,
|
32
|
+
:activesupport_inflector_ordinalize,
|
33
|
+
[:fixnum]) # number
|
34
|
+
|
33
35
|
# TODO: Transliterate
|
34
36
|
monkey_c_proxy(:parameterize,
|
35
37
|
:activesupport_inflector_parameterize,
|
36
38
|
[:string, # string
|
37
39
|
[:string, '-']]) # separator
|
38
40
|
|
39
|
-
# TODO: .to_i
|
40
|
-
monkey_c_proxy(:ordinalize,
|
41
|
-
:activesupport_inflector_ordinalize,
|
42
|
-
[:fixnum]) # number
|
43
|
-
|
44
|
-
alias_method :__camelize, :camelize
|
45
|
-
def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
|
46
|
-
if MonkeySupport::TypeChecks.is_ascii_string?(lower_case_and_underscored_word)
|
47
|
-
MonkeySupport::C.activesupport_inflector_camelize(lower_case_and_underscored_word.to_s, first_letter_in_uppercase)
|
48
|
-
else
|
49
|
-
__camelize(lower_case_and_underscored_word, first_letter_in_uppercase)
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
alias_method :__underscore, :underscore
|
54
|
-
def underscore(camel_cased_word)
|
55
|
-
if MonkeySupport::TypeChecks.is_ascii_string?(camel_cased_word)
|
56
|
-
MonkeySupport::C.activesupport_inflector_underscore(camel_cased_word)
|
57
|
-
else
|
58
|
-
__underscore(camel_cased_word)
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
alias_method :__dasherize, :dasherize
|
63
|
-
def dasherize(underscored_word)
|
64
|
-
if MonkeySupport::TypeChecks.is_ascii_string?(underscored_word)
|
65
|
-
MonkeySupport::C.activesupport_inflector_dasherize(underscored_word)
|
66
|
-
else
|
67
|
-
__dasherize(underscored_word)
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
alias_method :__demodulize, :demodulize
|
72
|
-
def demodulize(class_name_in_module)
|
73
|
-
if MonkeySupport::TypeChecks.is_ascii_string?(class_name_in_module)
|
74
|
-
MonkeySupport::C.activesupport_inflector_demodulize(class_name_in_module)
|
75
|
-
else
|
76
|
-
__demodulize(class_name_in_module)
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
alias_method :__foreign_key, :foreign_key
|
81
|
-
def foreign_key(class_name, separate_class_name_and_id_with_underscore = true)
|
82
|
-
if MonkeySupport::TypeChecks.is_ascii_string?(class_name)
|
83
|
-
MonkeySupport::C.activesupport_inflector_foreign_key(class_name, separate_class_name_and_id_with_underscore)
|
84
|
-
else
|
85
|
-
__foreign_key(class_name, separate_class_name_and_id_with_underscore)
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
=end
|
90
|
-
|
91
|
-
alias_method :__parameterize, :parameterize
|
92
|
-
def parameterize(string, sep = '-')
|
93
|
-
parameterized_string = transliterate(string)
|
94
|
-
if (MonkeySupport::TypeChecks.is_ascii_string?(parameterized_string) \
|
95
|
-
&& MonkeySupport::TypeChecks.is_ascii_string?(sep))
|
96
|
-
|
97
|
-
MonkeySupport::C.activesupport_inflector_parameterize(parameterized_string.to_s, sep)
|
98
|
-
else
|
99
|
-
__parameterize(string, sep)
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
alias_method :__ordinalize, :ordinalize
|
104
|
-
def ordinalize(number)
|
105
|
-
x = number.to_i
|
106
|
-
if (x.class == Fixnum)
|
107
|
-
MonkeySupport::C.activesupport_inflector_ordinalize(x)
|
108
|
-
else
|
109
|
-
__ordinalize(number)
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
41
|
end
|
114
42
|
end
|
115
43
|
|
@@ -16,35 +16,24 @@ module MonkeySupport
|
|
16
16
|
#
|
17
17
|
# alias_method :__demodulize, :demodulize
|
18
18
|
# def demodulize(arg0)
|
19
|
-
#
|
19
|
+
# begin
|
20
20
|
# MonkeySupport::C.activesupport_inflector_demodulize(arg0)
|
21
|
-
#
|
21
|
+
# rescue TypeError
|
22
22
|
# __demodulize(arg0)
|
23
23
|
# end
|
24
24
|
# end
|
25
25
|
def monkey_c_proxy(ruby_name, c_name, args)
|
26
|
-
checklist = Util::checklist(args)
|
27
26
|
arglist_with_defaults = Util::arglist(args, true)
|
28
27
|
arglist_without_defaults = Util::arglist(args, false)
|
29
28
|
|
30
|
-
if checklist
|
31
|
-
body = <<-EOS
|
32
|
-
if #{checklist}
|
33
|
-
MonkeySupport::C.#{c_name}(#{arglist_without_defaults})
|
34
|
-
else
|
35
|
-
__#{ruby_name}(#{arglist_without_defaults})
|
36
|
-
end
|
37
|
-
EOS
|
38
|
-
else
|
39
|
-
body = <<-EOS
|
40
|
-
MonkeySupport::C.#{c_name}(#{arglist_without_defaults})
|
41
|
-
EOS
|
42
|
-
end
|
43
|
-
|
44
29
|
function = <<-EOS
|
45
30
|
alias_method :__#{ruby_name}, :#{ruby_name}
|
46
31
|
def #{ruby_name}(#{arglist_with_defaults})
|
47
|
-
|
32
|
+
begin
|
33
|
+
MonkeySupport::C.#{c_name}(#{arglist_without_defaults})
|
34
|
+
rescue TypeError
|
35
|
+
__#{ruby_name}(#{arglist_without_defaults})
|
36
|
+
end
|
48
37
|
end
|
49
38
|
EOS
|
50
39
|
|
@@ -52,22 +41,11 @@ module MonkeySupport
|
|
52
41
|
end
|
53
42
|
|
54
43
|
module Util
|
55
|
-
def self.checklist(args)
|
56
|
-
checklist = []
|
57
|
-
args.each_with_index do |x, i|
|
58
|
-
arg = ((x.class == Array) ? x[0] : x) # support [:bool, true] notation.
|
59
|
-
if [:string, :fixnum].include?(arg)
|
60
|
-
checklist << "(MonkeySupport::TypeChecks.valid_#{arg}?(arg#{i}))"
|
61
|
-
end
|
62
|
-
end
|
63
|
-
return (checklist.empty? ? nil : "(#{checklist.join(' && ')})")
|
64
|
-
end
|
65
|
-
|
66
44
|
def self.arglist(args, include_defaults)
|
67
45
|
arglist = []
|
68
46
|
args.each_with_index do |arg, i|
|
69
47
|
if (arg.class == Array && include_defaults)
|
70
|
-
arglist << "arg#{i} = #{arg[1]}"
|
48
|
+
arglist << "arg#{i} = #{arg[1].inspect}"
|
71
49
|
else
|
72
50
|
arglist << "arg#{i}"
|
73
51
|
end
|
@@ -1,10 +1,12 @@
|
|
1
1
|
module MonkeySupport
|
2
2
|
module TypeChecks
|
3
3
|
|
4
|
+
# Disclaimer: I may have been writing too much lisp lately.
|
5
|
+
|
4
6
|
# NOTE: This exists, but it'll still be a lot faster to just call
|
5
7
|
# obj.class == Fixnum in the calling code, rather than resolving
|
6
8
|
# MonkeySupport::TypeChecks.is_fixnum? each time.
|
7
|
-
def self.valid_fixnum?(obj)
|
9
|
+
def self.valid_fixnum?(obj, string=false)
|
8
10
|
obj.class == Fixnum
|
9
11
|
end
|
10
12
|
|
@@ -19,17 +21,17 @@ module MonkeySupport
|
|
19
21
|
if '1.9'.respond_to?(:force_encoding)
|
20
22
|
ASCII_ENCODING = Encoding.find("ASCII-8BIT")
|
21
23
|
if Encoding.default_external == ASCII_ENCODING
|
22
|
-
def self.valid_string?(obj)
|
24
|
+
def self.valid_string?(obj, string=false)
|
23
25
|
(obj.class == String) && obj.encoding == ASCII_ENCODING
|
24
26
|
end
|
25
27
|
else
|
26
|
-
def self.valid_string?(obj)
|
28
|
+
def self.valid_string?(obj, string=false)
|
27
29
|
#TODO: Check ascii_only? and force_encoding here.
|
28
30
|
(obj.class == String) && obj.encoding == ASCII_ENCODING
|
29
31
|
end
|
30
32
|
end
|
31
33
|
else # <1.9
|
32
|
-
def self.valid_string?(obj)
|
34
|
+
def self.valid_string?(obj, string=false)
|
33
35
|
obj.class == String
|
34
36
|
end
|
35
37
|
end
|
data/monkeysupport.gemspec
CHANGED