Class: TwitterCldr::Shared::PropertiesDatabase

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/twitter_cldr/shared/properties_database.rb

Constant Summary collapse

DEFAULT_ROOT_PATH =
File.join(
  TwitterCldr::RESOURCES_DIR, 'unicode_data', 'properties'
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

compute_cache_key, deep_merge!, deep_merge_hash, deep_symbolize_keys, traverse_hash

Constructor Details

#initialize(root_path = DEFAULT_ROOT_PATH) ⇒ PropertiesDatabase

Returns a new instance of PropertiesDatabase.



20
21
22
23
# 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

Instance Attribute Details

#root_pathObject (readonly)

Returns the value of attribute root_path.



18
19
20
# File 'lib/twitter_cldr/shared/properties_database.rb', line 18

def root_path
  @root_path
end

#trieObject (readonly)

Returns the value of attribute trie.



18
19
20
# File 'lib/twitter_cldr/shared/properties_database.rb', line 18

def trie
  @trie
end

Instance Method Details

#code_points_for_property(property_name, property_value = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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_pairObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/twitter_cldr/shared/properties_database.rb', line 105

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) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
52
53
54
55
# 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) ⇒ Object



121
122
123
# File 'lib/twitter_cldr/shared/properties_database.rb', line 121

def normalize(property_name, property_value = nil)
  normalizer.normalize(property_name, property_value)
end

#properties_for_code_point(code_point) ⇒ Object



57
58
59
60
# 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_namesArray<String>

List of property names

Examples:

TwitterCldr::Shared::CodePoint.properties.property_names
# => ["ASCII_Hex_Digit", "Age", "Alphabetic", … ]

Returns:

  • (Array<String>)

    Array of property names string



67
68
69
70
71
72
# File 'lib/twitter_cldr/shared/properties_database.rb', line 67

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) ⇒ Array<String>|nil

Return possible values for a given property TwitterCldr::Shared::CodePoint.properties.property_values_for(‘Alphabetic’) # => nil

Examples:

TwitterCldr::Shared::CodePoint.properties.property_values_for('Script')
# => ["Adlam", "Ahom", "Anatolian_Hieroglyphs", … ]

Parameters:

  • property_name (String)

    Property name

Returns:

  • (Array<String>|nil)

    List of values Values



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/twitter_cldr/shared/properties_database.rb', line 81

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) ⇒ Object



25
26
27
# 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