Permalink
Browse files

created TypeChecks module

  • Loading branch information...
1 parent 61fba70 commit 0bf1dbba4e5ba1600f157cd9cfdc28cf3af28269 Burke Libbey committed Sep 4, 2009
View
2 lib/monkeysupport.rb
@@ -1,7 +1,7 @@
module MonkeySupport
end
-require 'monkeysupport/util'
+require 'monkeysupport/type_checks'
require 'monkeysupport/memoizable'
require 'active_support_c'
require 'monkeysupport/inflector'
View
18 lib/monkeysupport/inflector.rb
@@ -2,11 +2,12 @@ module ActiveSupport
module Inflector
extend MonkeySupport::Memoizable
+ extend MonkeySupport::TypeChecks
monkey_memoize :pluralize, :singularize, :humanize
alias_method :__camelize, :camelize
def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
- if MonkeySupport::Util::is_ascii_string?(lower_case_and_underscored_word)
+ if is_ascii_string?(lower_case_and_underscored_word)
ActiveSupport::ASC.inflector_camelize(lower_case_and_underscored_word.to_s, first_letter_in_uppercase)
else
__camelize(lower_case_and_underscored_word, first_letter_in_uppercase)
@@ -15,7 +16,7 @@ def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
alias_method :__underscore, :underscore
def underscore(camel_cased_word)
- if MonkeySupport::Util::is_ascii_string?(camel_cased_word)
+ if is_ascii_string?(camel_cased_word)
ActiveSupport::ASC.inflector_underscore(camel_cased_word)
else
__underscore(camel_cased_word)
@@ -24,7 +25,7 @@ def underscore(camel_cased_word)
alias_method :__dasherize, :dasherize
def dasherize(underscored_word)
- if MonkeySupport::Util::is_ascii_string?(underscored_word)
+ if is_ascii_string?(underscored_word)
ActiveSupport::ASC.inflector_dasherize(underscored_word)
else
__dasherize(underscored_word)
@@ -33,39 +34,36 @@ def dasherize(underscored_word)
alias_method :__demodulize, :demodulize
def demodulize(class_name_in_module)
- if MonkeySupport::Util::is_ascii_string?(class_name_in_module)
+ if is_ascii_string?(class_name_in_module)
ActiveSupport::ASC.inflector_demodulize(class_name_in_module)
else
__demodulize(class_name_in_module)
end
end
-
alias_method :__parameterize, :parameterize
def parameterize(string, sep = '-')
parameterized_string = transliterate(string)
- if MonkeySupport::Util::is_ascii_string?(parameterized_string) && MonkeySupport::Util::is_ascii_string?(sep)
+ if is_ascii_string?(parameterized_string) && is_ascii_string?(sep)
ActiveSupport::ASC.inflector_parameterize(parameterized_string.to_s, sep)
else
__parameterize(string, sep)
end
end
-
alias_method :__foreign_key, :foreign_key
def foreign_key(class_name, separate_class_name_and_id_with_underscore = true)
- if MonkeySupport::Util::is_ascii_string?(class_name)
+ if is_ascii_string?(class_name)
ActiveSupport::ASC.inflector_foreign_key(class_name, separate_class_name_and_id_with_underscore)
else
__foreign_key(class_name, separate_class_name_and_id_with_underscore)
end
end
-
alias_method :__ordinalize, :ordinalize
def ordinalize(number)
x = number.to_i
- if MonkeySupport::Util::is_fixnum?(x)
+ if is_fixnum?(x)
ActiveSupport::ASC.inflector_ordinalize(number.to_i)
else
__ordinalize(number)
View
2 lib/monkeysupport/memoizable.rb
@@ -1,9 +1,7 @@
module MonkeySupport
module Memoizable
# This is faster than AS::Memoizeable
- # TODO: Move this into a util class of some sort. Same with check methods above.
def monkey_memoize(*methods)
- methods = [methods] if methods.class == Symbol
methods.each do |method|
class_eval( <<"END"
View
27 lib/monkeysupport/type_checks.rb
@@ -0,0 +1,27 @@
+module MonkeySupport
+ module TypeChecks
+
+ def is_fixnum?(obj)
+ obj.class == Fixnum
+ end
+
+ if '1.9'.respond_to?(:force_encoding)
+ ASCII_ENCODING = Encoding.find("ASCII-8BIT")
+ if Encoding.default_external == ASCII_ENCODING
+ def is_ascii_string?(obj)
+ (obj.class == String) && obj.encoding == ASCII_ENCODING
+ end
+ else
+ def is_ascii_string?(obj)
+ #TODO: Check ascii_only? and force_encoding here.
+ (obj.class == String) && obj.encoding == ASCII_ENCODING
+ end
+ end
+ else # <1.9
+ def is_ascii_string?(obj)
+ obj.class == String
+ end
+ end
+
+ end
+end
View
40 lib/monkeysupport/util.rb
@@ -1,40 +0,0 @@
-module MonkeySupport
- module Util
-
- def self.is_fixnum?(obj)
- obj.class == Fixnum
- end
-
- if '1.9'.respond_to?(:force_encoding)
- ASCII_ENCODING = Encoding.find("ASCII-8BIT")
- if Encoding.default_external == ASCII_ENCODING
- def self.is_ascii_string?(obj)
- (obj.class == String) && obj.encoding == ASCII_ENCODING
- end
- else
- def self.is_ascii_string?(obj)
- #TODO: Check ascii_only? and force_encoding here.
- (obj.class == String) && obj.encoding == ASCII_ENCODING
- end
- end
- else # <1.9
- def self.is_ascii_string?(obj)
- obj.class == String
- end
- end
-
- # This is faster than AS::Memoizeable
- # TODO: Move this into a util class of some sort. Same with check methods above.
- def self.memoize(*methods)
- methods = [methods] if methods.class == Symbol
- methods.each do |method|
- instance_eval("@__#{method} = {}\n" +
- "alias_method :__#{method}, :#{method}\n" +
- "def #{method}(*args)\n" +
- " @__#{method}[args] ||= __#{method}(args)\n" +
- "end")
- end
- end
-
- end
-end
View
3 monkeysupport.gemspec
@@ -51,7 +51,8 @@ Gem::Specification.new do |s|
"lib/monkeysupport/inflector.rb",
"lib/monkeysupport/inflector.rb",
"lib/monkeysupport/memoizable.rb",
- "lib/monkeysupport/util.rb",
+ "lib/monkeysupport/memoizable.rb",
+ "lib/monkeysupport/type_checks.rb",
"monkeysupport.gemspec",
"test/monkeysupport_test.rb",
"test/monkeysupport_test.rb",

0 comments on commit 0bf1dbb

Please sign in to comment.