class TwitterCldr::Shared::PropertiesDatabase

Constants

DEFAULT_ROOT_PATH

Attributes

root_path[R]
trie[R]

Public Class Methods

new(root_path = DEFAULT_ROOT_PATH) click to toggle source
# File lib/twitter_cldr/shared/properties_database.rb, line 20
def initialize(root_path = DEFAULT_ROOT_PATH)
  @root_path = root_path
  @trie = FileSystemTrie.new(root_path)
end

Public Instance Methods

code_points_for_property(property_name, property_value = nil) click to toggle source
# File lib/twitter_cldr/shared/properties_database.rb, line 29
def code_points_for_property(property_name, property_value = nil)
  key = key_for(property_name, property_value)
  node = trie.get_node(key)

  if node
    if node.value
      node.value
    elsif name_indicates_value_prefix?(property_name)
      concat_children(key)
    else
      RangeSet.new([])
    end
  else
    RangeSet.new([])
  end
end
each_property_pair() { |property_name, property_value| ... } click to toggle source
# File lib/twitter_cldr/shared/properties_database.rb, line 93
def each_property_pair
  if block_given?
    property_names.each do |property_name|
      if property_values = property_values_for(property_name)
        property_values.each do |property_value|
          yield property_name, property_value
        end
      else
        yield property_name, nil
      end
    end
  else
    to_enum(__method__)
  end
end
include?(property_name, property_value = nil) click to toggle source
# File lib/twitter_cldr/shared/properties_database.rb, line 46
def include?(property_name, property_value = nil)
  values = property_values_for(property_name)

  if property_value
    property_names.include?(property_name) &&
      values && values.include?(property_value)
  else
    property_names.include?(property_name)
  end
end
normalize(property_name, property_value = nil) click to toggle source
# File lib/twitter_cldr/shared/properties_database.rb, line 109
def normalize(property_name, property_value = nil)
  normalizer.normalize(property_name, property_value)
end
properties_for_code_point(code_point) click to toggle source
# File lib/twitter_cldr/shared/properties_database.rb, line 57
def properties_for_code_point(code_point)
  code_point_cache[code_point] ||=
    PropertySet.new(lookup_code_point(code_point))
end
property_names() click to toggle source
# File lib/twitter_cldr/shared/properties_database.rb, line 62
def property_names
  glob = File.join(root_path, '*')
  @property_names ||= Dir.glob(glob).map do |path|
    File.basename(path)
  end
end
property_values_for(property_name) click to toggle source
# File lib/twitter_cldr/shared/properties_database.rb, line 69
def property_values_for(property_name)
  if property_values.include?(property_name)
    return property_values[property_name]
  end

  prefix = File.join(root_path, property_name)
  glob = File.join(prefix, "**/**/#{FileSystemTrie::VALUE_FILE}")

  values = Dir.glob(glob).map do |path|
    path = File.dirname(path)[(prefix.length + 1)..-1]
    path.split(File::SEPARATOR).join if path
  end.compact

  if name_indicates_value_prefix?(property_name)
    values += values.map { |v| v[0] }
  end

  property_values[property_name] = if values.length == 0
    nil
  else
    values.uniq
  end
end
store(property_name, property_value, data) click to toggle source
# File lib/twitter_cldr/shared/properties_database.rb, line 25
def store(property_name, property_value, data)
  trie.add(key_for(property_name, property_value), data)
end

Private Instance Methods

code_point_cache() click to toggle source
# File lib/twitter_cldr/shared/properties_database.rb, line 142
def code_point_cache
  @code_point_cache ||= {}
end
concat_children(key) click to toggle source
# File lib/twitter_cldr/shared/properties_database.rb, line 146
def concat_children(key)
  glob = File.join(root_path, *key, "**/**/#{FileSystemTrie::VALUE_FILE}")

  Dir.glob(glob).inject(RangeSet.new([])) do |ret, entry|
    key = File.dirname(entry)
    key = key[(root_path.length + 1)..-1].split(File::SEPARATOR)

    if value = trie.get(key)
      ret.union(value)
    else
      ret
    end
  end
end
key_for(property_name, property_value) click to toggle source
# File lib/twitter_cldr/shared/properties_database.rb, line 161
def key_for(property_name, property_value)
  if property_value
    if name_indicates_value_prefix?(property_name)
      [property_name] + property_value.chars.to_a
    else
      [property_name, property_value]
    end
  else
    [property_name]
  end
end
lookup_code_point(code_point) click to toggle source
# File lib/twitter_cldr/shared/properties_database.rb, line 119
def lookup_code_point(code_point)
  {}.tap do |properties|
    each_property_pair do |property_name, property_value|
      code_points = code_points_for_property(
        property_name, property_value
      )

      if code_points.include?(code_point)
        if property_value
          properties[property_name] ||= Set.new
          properties[property_name] << property_value
        else
          properties[property_name] = nil
        end
      end
    end
  end
end
name_indicates_value_prefix?(property_name) click to toggle source
# File lib/twitter_cldr/shared/properties_database.rb, line 173
def name_indicates_value_prefix?(property_name)
  property_name == 'General_Category' ||
    property_name == 'gc'
end
normalizer() click to toggle source
# File lib/twitter_cldr/shared/properties_database.rb, line 115
def normalizer
  @normalizer ||= PropertyNormalizer.new(self)
end
property_values() click to toggle source
# File lib/twitter_cldr/shared/properties_database.rb, line 138
def property_values
  @property_values ||= {}
end