Class: Carmen::Region

Inherits:
Object
  • Object
show all
Defined in:
lib/carmen/region.rb

Direct Known Subclasses

Country, World

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}, parent = nil) ⇒ Region

Returns a new instance of Region.


13
14
15
16
17
# File 'lib/carmen/region.rb', line 13

def initialize(data={}, parent=nil)
  @type = data['type']
  @code = data['code']
  @parent = parent
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code


10
11
12
# File 'lib/carmen/region.rb', line 10

def code
  @code
end

#parentObject (readonly)

Returns the value of attribute parent


11
12
13
# File 'lib/carmen/region.rb', line 11

def parent
  @parent
end

#typeObject (readonly)

Returns the value of attribute type


9
10
11
# File 'lib/carmen/region.rb', line 9

def type
  @type
end

Instance Method Details

#<=>(other) ⇒ Object


66
67
68
# File 'lib/carmen/region.rb', line 66

def <=>(other)
  name <=> other.name
end

#inspectObject


49
50
51
# File 'lib/carmen/region.rb', line 49

def inspect
  "<##{self.class} name=\"#{name}\" type=\"#{type}\">"
end

#nameObject


19
20
21
# File 'lib/carmen/region.rb', line 19

def name
  Carmen.i18n_backend.translate(path('name'))
end

#path(suffix = nil) ⇒ Object

Return a path string for this region. Useful for use with I18n.

Returns a string in the format “world.$PARENT_CODE.$REGION_CODE”, such as “world.us.il”. The number of segments is the depth of the region plus one.


43
44
45
46
47
# File 'lib/carmen/region.rb', line 43

def path(suffix=nil)
  base = "#{parent.path}.#{subregion_directory}"
  base << ".#{suffix.to_s}" if suffix
  base
end

#reset!Object

Clears the subregion cache


62
63
64
# File 'lib/carmen/region.rb', line 62

def reset!
  @subregions = nil
end

#subregion_classObject


35
36
37
# File 'lib/carmen/region.rb', line 35

def subregion_class
  Region
end

#subregion_data_pathObject


31
32
33
# File 'lib/carmen/region.rb', line 31

def subregion_data_path
  @parent.subregion_data_path.sub('.yml', "/#{subregion_directory}.yml")
end

#subregionsObject


23
24
25
# File 'lib/carmen/region.rb', line 23

def subregions
  @subregions ||= load_subregions.freeze
end

#subregions?Boolean

Returns:

  • (Boolean)

27
28
29
# File 'lib/carmen/region.rb', line 27

def subregions?
  !subregions.empty?
end

#to_hashObject


57
58
59
# File 'lib/carmen/region.rb', line 57

def to_hash
  { type: type, code: code, name: name, subregions: subregions }
end

#to_sObject


53
54
55
# File 'lib/carmen/region.rb', line 53

def to_s
  name
end