Class: AreaCN::Code

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/area_cn/code.rb

Constant Summary collapse

ROOT =
"000000"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code) ⇒ Code

Returns a new instance of Code.



9
10
11
# File 'lib/area_cn/code.rb', line 9

def initialize(code)
  @code = code
end

Instance Attribute Details

#codeObject

Returns the value of attribute code.



7
8
9
# File 'lib/area_cn/code.rb', line 7

def code
  @code
end

Instance Method Details

#<=>(other) ⇒ Object



50
51
52
# File 'lib/area_cn/code.rb', line 50

def <=>(other)
  self.code <=> other.code
end

#ancestor?(other) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
# File 'lib/area_cn/code.rb', line 40

def ancestor?(other)
  other = Code.new(other) unless other.is_a?(Code)
  other.ancestors.include?(self)
end

#ancestorsObject



31
32
33
34
35
36
37
38
# File 'lib/area_cn/code.rb', line 31

def ancestors
  return @ancestors if @ancestors
  child = self
  ancestors = [self] 
  ancestors << child while child = child.parent 
        
  ancestors
end

#child?(other) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
# File 'lib/area_cn/code.rb', line 45

def child?(other)
  other = Code.new(other) unless other.is_a?(Code)
  other.ancestor?(self)
end

#inspectObject



54
55
56
# File 'lib/area_cn/code.rb', line 54

def inspect
  @code
end

#parentObject



26
27
28
29
# File 'lib/area_cn/code.rb', line 26

def parent
  return if @code == ROOT
  @parent ||= Code.new(prefix.sub(/..$/, '00').ljust(6, '0'))
end

#prefixObject



17
18
19
20
21
22
23
24
# File 'lib/area_cn/code.rb', line 17

def prefix
  return if code == ROOT
  return @prefix if @prefix
  code_prefix = code.sub(/0{2,4}$/, '') 
  code_prefix << '0' if code_prefix.length.odd?
   
  code_prefix 
end

#to_sObject



58
59
60
# File 'lib/area_cn/code.rb', line 58

def to_s
  @code
end

#valueObject



13
14
15
# File 'lib/area_cn/code.rb', line 13

def value
  code
end