burke-monkeysupport 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.4
data/lib/monkeysupport.rb CHANGED
@@ -1,2 +1,7 @@
1
+ module MonkeySupport
2
+ end
3
+
4
+ require 'monkeysupport/type_checks'
5
+ require 'monkeysupport/memoizable'
1
6
  require 'active_support_c'
2
7
  require 'monkeysupport/inflector'
@@ -1,50 +1,13 @@
1
1
  module ActiveSupport
2
2
  module Inflector
3
3
 
4
-
5
- def check_fixnum(obj)
6
- return (obj.class == Fixnum)
7
- end
8
-
9
- if '1.9'.respond_to?(:force_encoding)
10
- ASCII_ENCODING = Encoding.find("ASCII-8BIT")
11
- if Encoding.default_external == ASCII_ENCODING
12
- def check_ascii_string(obj)
13
- (obj.class == String) && obj.encoding == ASCII_ENCODING
14
- end
15
- else
16
- def check_ascii_string(obj)
17
- #TODO: Check ascii_only? and force_encoding here.
18
- (obj.class == String) && obj.encoding == ASCII_ENCODING
19
- end
20
- end
21
- else # <1.9
22
- def check_ascii_string(obj)
23
- obj.class == String
24
- end
25
- end
26
-
27
- @_pluralize = {}
28
- alias_method :__pluralize, :pluralize
29
- def pluralize(word)
30
- @_pluralize[word] ||= __pluralize(word)
31
- end
32
-
33
- @_singularize = {}
34
- alias_method :__singularize, :singularize
35
- def singularize(word)
36
- @_singularize[word] ||= __singularize(word)
37
- end
38
-
39
- @_humanize = {}
40
- alias_method :__humanize, :humanize
41
- def humanize(word)
42
- @_humanize[word] ||= __humanize(word)
43
- end
4
+ extend MonkeySupport::Memoizable
5
+ extend MonkeySupport::TypeChecks
6
+ monkey_memoize :pluralize, :singularize, :humanize
44
7
 
45
8
  alias_method :__camelize, :camelize
46
9
  def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
47
- if check_ascii_string(lower_case_and_underscored_word)
10
+ if is_ascii_string?(lower_case_and_underscored_word)
48
11
  ActiveSupport::ASC.inflector_camelize(lower_case_and_underscored_word.to_s, first_letter_in_uppercase)
49
12
  else
50
13
  __camelize(lower_case_and_underscored_word, first_letter_in_uppercase)
@@ -53,17 +16,16 @@ module ActiveSupport
53
16
 
54
17
  alias_method :__underscore, :underscore
55
18
  def underscore(camel_cased_word)
56
- if check_ascii_string(camel_cased_word)
19
+ if is_ascii_string?(camel_cased_word)
57
20
  ActiveSupport::ASC.inflector_underscore(camel_cased_word)
58
21
  else
59
22
  __underscore(camel_cased_word)
60
23
  end
61
24
  end
62
25
 
63
-
64
26
  alias_method :__dasherize, :dasherize
65
27
  def dasherize(underscored_word)
66
- if check_ascii_string(underscored_word)
28
+ if is_ascii_string?(underscored_word)
67
29
  ActiveSupport::ASC.inflector_dasherize(underscored_word)
68
30
  else
69
31
  __dasherize(underscored_word)
@@ -72,39 +34,36 @@ module ActiveSupport
72
34
 
73
35
  alias_method :__demodulize, :demodulize
74
36
  def demodulize(class_name_in_module)
75
- if check_ascii_string(class_name_in_module)
37
+ if is_ascii_string?(class_name_in_module)
76
38
  ActiveSupport::ASC.inflector_demodulize(class_name_in_module)
77
39
  else
78
40
  __demodulize(class_name_in_module)
79
41
  end
80
42
  end
81
43
 
82
-
83
44
  alias_method :__parameterize, :parameterize
84
45
  def parameterize(string, sep = '-')
85
46
  parameterized_string = transliterate(string)
86
- if check_ascii_string(parameterized_string) && check_ascii_string(sep)
47
+ if is_ascii_string?(parameterized_string) && is_ascii_string?(sep)
87
48
  ActiveSupport::ASC.inflector_parameterize(parameterized_string.to_s, sep)
88
49
  else
89
50
  __parameterize(string, sep)
90
51
  end
91
52
  end
92
53
 
93
-
94
54
  alias_method :__foreign_key, :foreign_key
95
55
  def foreign_key(class_name, separate_class_name_and_id_with_underscore = true)
96
- if check_ascii_string(class_name)
56
+ if is_ascii_string?(class_name)
97
57
  ActiveSupport::ASC.inflector_foreign_key(class_name, separate_class_name_and_id_with_underscore)
98
58
  else
99
59
  __foreign_key(class_name, separate_class_name_and_id_with_underscore)
100
60
  end
101
61
  end
102
62
 
103
-
104
63
  alias_method :__ordinalize, :ordinalize
105
64
  def ordinalize(number)
106
65
  x = number.to_i
107
- if check_fixnum(x)
66
+ if is_fixnum?(x)
108
67
  ActiveSupport::ASC.inflector_ordinalize(number.to_i)
109
68
  else
110
69
  __ordinalize(number)
@@ -0,0 +1,31 @@
1
+ module MonkeySupport
2
+ module Memoizable
3
+ # This is faster than AS::Memoizeable
4
+ def monkey_memoize(*methods)
5
+ methods.each do |method|
6
+ class_eval( <<"END"
7
+
8
+ @__#{method} = {}
9
+ alias_method :__#{method}, :#{method}
10
+
11
+ if method(:#{method}).arity == 1
12
+
13
+ def #{method}(arg)
14
+ @__#{method}[arg] ||= __#{method}(arg)
15
+ end
16
+
17
+ else
18
+
19
+ def #{method}(*args)
20
+ @__#{method}[args] ||= __#{method}(*args)
21
+ end
22
+
23
+ end
24
+
25
+ END
26
+ )
27
+
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,27 @@
1
+ module MonkeySupport
2
+ module TypeChecks
3
+
4
+ def is_fixnum?(obj)
5
+ obj.class == Fixnum
6
+ end
7
+
8
+ if '1.9'.respond_to?(:force_encoding)
9
+ ASCII_ENCODING = Encoding.find("ASCII-8BIT")
10
+ if Encoding.default_external == ASCII_ENCODING
11
+ def is_ascii_string?(obj)
12
+ (obj.class == String) && obj.encoding == ASCII_ENCODING
13
+ end
14
+ else
15
+ def is_ascii_string?(obj)
16
+ #TODO: Check ascii_only? and force_encoding here.
17
+ (obj.class == String) && obj.encoding == ASCII_ENCODING
18
+ end
19
+ end
20
+ else # <1.9
21
+ def is_ascii_string?(obj)
22
+ obj.class == String
23
+ end
24
+ end
25
+
26
+ end
27
+ end
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{monkeysupport}
8
- s.version = "0.0.3"
8
+ s.version = "0.0.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Burke Libbey"]
@@ -50,6 +50,10 @@ Gem::Specification.new do |s|
50
50
  "lib/monkeysupport.rb",
51
51
  "lib/monkeysupport/inflector.rb",
52
52
  "lib/monkeysupport/inflector.rb",
53
+ "lib/monkeysupport/memoizable.rb",
54
+ "lib/monkeysupport/memoizable.rb",
55
+ "lib/monkeysupport/type_checks.rb",
56
+ "lib/monkeysupport/type_checks.rb",
53
57
  "monkeysupport.gemspec",
54
58
  "test/monkeysupport_test.rb",
55
59
  "test/monkeysupport_test.rb",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: burke-monkeysupport
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Burke Libbey
@@ -51,11 +51,14 @@ files:
51
51
  - ext/inflector.h
52
52
  - lib/monkeysupport.rb
53
53
  - lib/monkeysupport/inflector.rb
54
+ - lib/monkeysupport/memoizable.rb
55
+ - lib/monkeysupport/type_checks.rb
54
56
  - monkeysupport.gemspec
55
57
  - test/monkeysupport_test.rb
56
58
  - test/test_helper.rb
57
59
  has_rdoc: false
58
60
  homepage: http://github.com/burke/monkeysupport
61
+ licenses:
59
62
  post_install_message:
60
63
  rdoc_options:
61
64
  - --charset=UTF-8
@@ -76,7 +79,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
76
79
  requirements: []
77
80
 
78
81
  rubyforge_project:
79
- rubygems_version: 1.2.0
82
+ rubygems_version: 1.3.5
80
83
  signing_key:
81
84
  specification_version: 3
82
85
  summary: Monkeypatching Rails with C since 2009