aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRick Olson <technoweenie@gmail.com>2006-11-09 19:40:32 +0000
committerRick Olson <technoweenie@gmail.com>2006-11-09 19:40:32 +0000
commit510092c681a2cd9cc218d01b97b81525cba310c3 (patch)
treea98d9e683a7aa34606a98ab7525afeb46982b2c5
parent9d2da04680db202ba006ff66199b345a3f29b2cb (diff)
downloadrails-510092c681a2cd9cc218d01b97b81525cba310c3.tar.gz
rails-510092c681a2cd9cc218d01b97b81525cba310c3.tar.bz2
rails-510092c681a2cd9cc218d01b97b81525cba310c3.zip
Lazily load the Unicode Database in the UTF-8 Handler [Rick Olson]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5476 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r--activesupport/CHANGELOG2
-rw-r--r--activesupport/lib/active_support/multibyte/handlers/utf8_handler.rb55
2 files changed, 35 insertions, 22 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG
index 8c5443de74..33cce48323 100644
--- a/activesupport/CHANGELOG
+++ b/activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Lazily load the Unicode Database in the UTF-8 Handler [Rick Olson]
+
* Update dependencies to delete partially loaded constants. [Nicholas Seckar]
* Fix unicode JSON regexp for Onigurama compatibility. #6494 [whitley]
diff --git a/activesupport/lib/active_support/multibyte/handlers/utf8_handler.rb b/activesupport/lib/active_support/multibyte/handlers/utf8_handler.rb
index 5b64734297..a34701b1af 100644
--- a/activesupport/lib/active_support/multibyte/handlers/utf8_handler.rb
+++ b/activesupport/lib/active_support/multibyte/handlers/utf8_handler.rb
@@ -7,12 +7,36 @@ module ActiveSupport::Multibyte::Handlers
end
class UnicodeDatabase #:nodoc:
- attr_accessor :codepoints, :composition_exclusion, :composition_map, :boundary, :cp1252
+ attr_writer :codepoints, :composition_exclusion, :composition_map, :boundary, :cp1252
- # Creates a new UnicodeDatabase instance and loads the database.
- def initialize
+ # self-expiring methods that lazily load the Unicode database and then return the value.
+ [:codepoints, :composition_exclusion, :composition_map, :boundary, :cp1252].each do |attr_name|
+ class_eval(<<-EOS, __FILE__, __LINE__)
+ def #{attr_name}
+ load
+ @#{attr_name}
+ end
+ EOS
+ end
+
+ # Shortcut to ucd.codepoints[]
+ def [](index); codepoints[index]; end
+
+ # Returns the directory in which the data files are stored
+ def self.dirname
+ File.dirname(__FILE__) + '/../../values/'
+ end
+
+ # Returns the filename for the data file for this version
+ def self.filename
+ File.expand_path File.join(dirname, "unicode_tables.dat")
+ end
+
+ # Loads the unicode database and returns all the internal objects of UnicodeDatabase
+ # Once the values have been loaded, define attr_reader methods for the instance variables.
+ def load
begin
- @codepoints, @composition_exclusion, @composition_map, @boundary, @cp1252 = self.class.load
+ @codepoints, @composition_exclusion, @composition_map, @boundary, @cp1252 = File.open(self.class.filename, 'rb') { |f| Marshal.load f.read }
rescue Exception => e
raise IOError.new("Couldn't load the unicode tables for UTF8Handler (#{e.message}), handler is unusable")
end
@@ -30,24 +54,11 @@ module ActiveSupport::Multibyte::Handlers
end
end if @boundary[k].kind_of?(Array)
end
- end
-
- # Shortcut to ucd.codepoints[]
- def [](index); @codepoints[index]; end
-
- # Returns the directory in which the data files are stored
- def self.dirname
- File.dirname(__FILE__) + '/../../values/'
- end
-
- # Returns the filename for the data file for this version
- def self.filename
- File.expand_path File.join(dirname, "unicode_tables.dat")
- end
-
- # Loads the unicode database and returns all the internal objects of UnicodeDatabase
- def self.load
- File.open(self.filename, 'rb') { |f| Marshal.load f.read }
+
+ # define attr_reader methods for the instance variables
+ class << self
+ attr_reader :codepoints, :composition_exclusion, :composition_map, :boundary, :cp1252
+ end
end
end