Module: TwitterCldr::Shared::TerritoriesContainment

Defined in:
lib/twitter_cldr/shared/territories_containment.rb

Class Method Summary collapse

Class Method Details

.children(territory_code) ⇒ Object

Returns the immediate parent of the territory with the given code. Raises an ArgumentError exception if the territory code is invalid.


35
36
37
38
39
# File 'lib/twitter_cldr/shared/territories_containment.rb', line 35

def children(territory_code)
  validate_territory(territory_code)

  containment_map[territory_code]
end

.containment_mapObject


41
42
43
44
45
46
# File 'lib/twitter_cldr/shared/territories_containment.rb', line 41

def containment_map
  @containment_map ||= get_resource.inject(Hash.new { |h, k| h[k] = [] }) do |memo, (territory, children)|
    memo[territory.to_s] = children[:contains].map(&:to_s)
    memo
  end
end

.contains?(parent_code, child_code) ⇒ Boolean

Returns true if the first territory contains the second one. Returns false otherwise. Raises an ArgumentError exception if one of the territories is invalid.

Returns:

  • (Boolean)

15
16
17
18
19
20
21
22
23
# File 'lib/twitter_cldr/shared/territories_containment.rb', line 15

def contains?(parent_code, child_code)
  validate_territory(parent_code)
  validate_territory(child_code)

  immediate_children = children(parent_code)

  immediate_children.include?(child_code) ||
    immediate_children.any? { |immediate_child| contains?(immediate_child, child_code) }
end

.parents(territory_code) ⇒ Object

Returns the immediate parent of the territory with the given code. Raises an ArgumentError exception if the territory code is invalid.


27
28
29
30
31
# File 'lib/twitter_cldr/shared/territories_containment.rb', line 27

def parents(territory_code)
  validate_territory(territory_code)

  parents_map[territory_code]
end