Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 5476

Show
Ignore:
Timestamp:
11/09/06 19:40:32 (2 years ago)
Author:
rick
Message:

Lazily load the Unicode Database in the UTF-8 Handler [Rick Olson]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/CHANGELOG

    r5464 r5476  
    11*SVN* 
     2 
     3* Lazily load the Unicode Database in the UTF-8 Handler [Rick Olson] 
    24 
    35* Update dependencies to delete partially loaded constants. [Nicholas Seckar] 
  • trunk/activesupport/lib/active_support/multibyte/handlers/utf8_handler.rb

    r5316 r5476  
    88   
    99  class UnicodeDatabase #:nodoc: 
    10     attr_accessor :codepoints, :composition_exclusion, :composition_map, :boundary, :cp1252 
    11      
    12     # Creates a new UnicodeDatabase instance and loads the database. 
    13     def initialize 
     10    attr_writer :codepoints, :composition_exclusion, :composition_map, :boundary, :cp1252 
     11     
     12    # self-expiring methods that lazily load the Unicode database and then return the value. 
     13    [:codepoints, :composition_exclusion, :composition_map, :boundary, :cp1252].each do |attr_name| 
     14      class_eval(<<-EOS, __FILE__, __LINE__) 
     15        def #{attr_name} 
     16          load 
     17          @#{attr_name} 
     18        end 
     19      EOS 
     20    end 
     21     
     22    # Shortcut to ucd.codepoints[] 
     23    def [](index); codepoints[index]; end 
     24     
     25    # Returns the directory in which the data files are stored 
     26    def self.dirname 
     27      File.dirname(__FILE__) + '/../../values/' 
     28    end 
     29     
     30    # Returns the filename for the data file for this version 
     31    def self.filename 
     32      File.expand_path File.join(dirname, "unicode_tables.dat") 
     33    end 
     34     
     35    # Loads the unicode database and returns all the internal objects of UnicodeDatabase 
     36    # Once the values have been loaded, define attr_reader methods for the instance variables. 
     37    def load 
    1438      begin 
    15         @codepoints, @composition_exclusion, @composition_map, @boundary, @cp1252 = self.class.load 
     39        @codepoints, @composition_exclusion, @composition_map, @boundary, @cp1252 = File.open(self.class.filename, 'rb') { |f| Marshal.load f.read } 
    1640      rescue Exception => e 
    1741          raise IOError.new("Couldn't load the unicode tables for UTF8Handler (#{e.message}), handler is unusable") 
     
    3155        end if @boundary[k].kind_of?(Array) 
    3256      end 
    33     end 
    34      
    35     # Shortcut to ucd.codepoints[] 
    36     def [](index); @codepoints[index]; end 
    37      
    38     # Returns the directory in which the data files are stored 
    39     def self.dirname 
    40       File.dirname(__FILE__) + '/../../values/' 
    41     end 
    42      
    43     # Returns the filename for the data file for this version 
    44     def self.filename 
    45       File.expand_path File.join(dirname, "unicode_tables.dat") 
    46     end 
    47      
    48     # Loads the unicode database and returns all the internal objects of UnicodeDatabase 
    49     def self.load 
    50       File.open(self.filename, 'rb') { |f| Marshal.load f.read } 
     57 
     58      # define attr_reader methods for the instance variables 
     59      class << self 
     60        attr_reader :codepoints, :composition_exclusion, :composition_map, :boundary, :cp1252 
     61      end 
    5162    end 
    5263  end