Class: AreaCN::Code
Constant Summary collapse
- ROOT =
"000000"
Instance Attribute Summary collapse
-
#code ⇒ Object
Returns the value of attribute code.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #ancestor?(other) ⇒ Boolean
- #ancestors ⇒ Object
- #child?(other) ⇒ Boolean
-
#initialize(code) ⇒ Code
constructor
A new instance of Code.
- #inspect ⇒ Object
- #parent ⇒ Object
- #prefix ⇒ Object
- #to_s ⇒ Object
- #value ⇒ Object
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
#code ⇒ Object
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
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 |
#ancestors ⇒ Object
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
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 |
#inspect ⇒ Object
54 55 56 |
# File 'lib/area_cn/code.rb', line 54 def inspect @code end |
#parent ⇒ Object
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 |
#prefix ⇒ Object
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_s ⇒ Object
58 59 60 |
# File 'lib/area_cn/code.rb', line 58 def to_s @code end |
#value ⇒ Object
13 14 15 |
# File 'lib/area_cn/code.rb', line 13 def value code end |