Class: ActiveSupport::Multibyte::Unicode::UnicodeDatabase

Inherits:
Object
  • Object
show all
Defined in:
lib/active_support/multibyte/unicode.rb

Overview

Holds static data from the Unicode database.

Constant Summary collapse

ATTRIBUTES =
:codepoints, :composition_exclusion, :composition_map, :boundary, :cp1252

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUnicodeDatabase

Returns a new instance of UnicodeDatabase.


313
314
315
316
317
318
319
# File 'lib/active_support/multibyte/unicode.rb', line 313

def initialize
  @codepoints = Hash.new(Codepoint.new)
  @composition_exclusion = []
  @composition_map = {}
  @boundary = {}
  @cp1252 = {}
end

Class Method Details

.dirnameObject

Returns the directory in which the data files are stored.


356
357
358
# File 'lib/active_support/multibyte/unicode.rb', line 356

def self.dirname
  File.dirname(__FILE__) + '/../values/'
end

.filenameObject

Returns the filename for the data file for this version.


361
362
363
# File 'lib/active_support/multibyte/unicode.rb', line 361

def self.filename
  File.expand_path File.join(dirname, "unicode_tables.dat")
end

Instance Method Details

#loadObject

Loads the Unicode database and returns all the internal objects of UnicodeDatabase.


333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/active_support/multibyte/unicode.rb', line 333

def load
  begin
    @codepoints, @composition_exclusion, @composition_map, @boundary, @cp1252 = File.open(self.class.filename, 'rb') { |f| Marshal.load f.read }
  rescue => e
      raise IOError.new("Couldn't load the Unicode tables for UTF8Handler (#{e.message}), ActiveSupport::Multibyte is unusable")
  end

  # Redefine the === method so we can write shorter rules for grapheme cluster breaks
  @boundary.each do |k,_|
    @boundary[k].instance_eval do
      def ===(other)
        detect { |i| i === other } ? true : false
      end
    end if @boundary[k].kind_of?(Array)
  end

  # define attr_reader methods for the instance variables
  class << self
    attr_reader(*ATTRIBUTES)
  end
end