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



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# 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) ⇒ 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



109
110
111
# 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) ⇒ 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_namesObject



62
63
64
65
66
67
# 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) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# 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) ⇒ 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