area_cn 0.0.3 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +18 -3
- data/lib/area_cn/area.rb +6 -7
- data/lib/area_cn/areas.rb +2 -2
- data/lib/area_cn/code.rb +10 -24
- data/lib/area_cn/version.rb +1 -1
- data/lib/area_cn.rb +16 -0
- data/spec/area_cn_spec.rb +48 -3
- metadata +2 -2
    
        data/README.md
    CHANGED
    
    | @@ -18,15 +18,30 @@ Or install it yourself as: | |
| 18 18 |  | 
| 19 19 | 
             
            ## Usage
         | 
| 20 20 |  | 
| 21 | 
            +
              # Find by name or code
         | 
| 22 | 
            +
             | 
| 21 23 | 
             
              zhejiang = AreaCN.areas.find_by_name("浙江省")
         | 
| 22 24 | 
             
              zhejiang.code # => "330000"
         | 
| 23 | 
            -
              zhejiang.children == zhejiang.cities # => ["杭州市", " | 
| 25 | 
            +
              zhejiang.children == zhejiang.cities # => [{:name => "杭州市", :code => "330100"}, {}, {}, ...]
         | 
| 24 26 |  | 
| 25 27 |  | 
| 26 | 
            -
              hangzhou = AreaCN.areas. | 
| 28 | 
            +
              hangzhou = AreaCN.areas.find_by_code("330100")
         | 
| 27 29 | 
             
              hangzhou.code # => "330100"
         | 
| 28 | 
            -
              hangzhou. | 
| 30 | 
            +
              hangzhou.name # => "杭州市"
         | 
| 31 | 
            +
              hangzhou.children == hangzhou.districts # => [{:name => "滨江区", :code => "330108"}, {}, {}, ...]
         | 
| 32 | 
            +
             | 
| 33 | 
            +
              # Return all provinces
         | 
| 34 | 
            +
             | 
| 35 | 
            +
              AreaCN.provinces
         | 
| 36 | 
            +
              AreaCN.areas.provinces
         | 
| 37 | 
            +
             | 
| 38 | 
            +
              # Code methods
         | 
| 39 | 
            +
             | 
| 40 | 
            +
              zhejiang.code.prefix # => return '33'
         | 
| 41 | 
            +
              hangzhou.code.parent # => return '330000'
         | 
| 29 42 |  | 
| 43 | 
            +
              zhejiang.ancestor?(hangzhou) # => return true
         | 
| 44 | 
            +
              hangzhou.child?(zhejiang)    # => return true
         | 
| 30 45 |  | 
| 31 46 |  | 
| 32 47 | 
             
            ## Contributing
         | 
    
        data/lib/area_cn/area.rb
    CHANGED
    
    | @@ -4,7 +4,7 @@ module AreaCN | |
| 4 4 | 
             
              class Area
         | 
| 5 5 | 
             
                include Comparable
         | 
| 6 6 |  | 
| 7 | 
            -
                attr_reader :name, :children
         | 
| 7 | 
            +
                attr_reader :name, :code, :children
         | 
| 8 8 | 
             
                attr_accessor :parent
         | 
| 9 9 |  | 
| 10 10 | 
             
                # area is a json area from areas.json file
         | 
| @@ -14,12 +14,8 @@ module AreaCN | |
| 14 14 | 
             
                  @children = []
         | 
| 15 15 | 
             
                end
         | 
| 16 16 |  | 
| 17 | 
            -
                def code
         | 
| 18 | 
            -
                  @code.value
         | 
| 19 | 
            -
                end
         | 
| 20 | 
            -
             | 
| 21 17 | 
             
                def parent_code
         | 
| 22 | 
            -
                  @code.parent | 
| 18 | 
            +
                  @code.parent
         | 
| 23 19 | 
             
                end
         | 
| 24 20 |  | 
| 25 21 | 
             
                def children_names
         | 
| @@ -27,7 +23,7 @@ module AreaCN | |
| 27 23 | 
             
                end
         | 
| 28 24 |  | 
| 29 25 | 
             
                def children_codes
         | 
| 30 | 
            -
                  @children_codes ||= children.map | 
| 26 | 
            +
                  @children_codes ||= children.map(&:code)
         | 
| 31 27 | 
             
                end
         | 
| 32 28 |  | 
| 33 29 | 
             
                def province?
         | 
| @@ -54,6 +50,9 @@ module AreaCN | |
| 54 50 | 
             
                  to_hash.to_json(*args)
         | 
| 55 51 | 
             
                end
         | 
| 56 52 |  | 
| 53 | 
            +
                def inspect
         | 
| 54 | 
            +
                  to_hash
         | 
| 55 | 
            +
                end
         | 
| 57 56 | 
             
              end
         | 
| 58 57 |  | 
| 59 58 | 
             
              class Province < Area
         | 
    
        data/lib/area_cn/areas.rb
    CHANGED
    
    | @@ -29,9 +29,9 @@ module AreaCN | |
| 29 29 |  | 
| 30 30 | 
             
                def find_by_code(code, area_level = nil)
         | 
| 31 31 | 
             
                  scope = area_level ? instance_variable_get("@#{area_level.to_s.pluralize}") : all
         | 
| 32 | 
            -
                   | 
| 33 | 
            -
                  scope.detect { |area| area.code == code_value }
         | 
| 32 | 
            +
                  scope.detect { |area| area.code == code }
         | 
| 34 33 | 
             
                end
         | 
| 34 | 
            +
                alias_method :get, :find_by_code
         | 
| 35 35 |  | 
| 36 36 | 
             
                def match(name, area_level = nil)
         | 
| 37 37 | 
             
                  scope = area_level ? instance_variable_get("@#{area_level.to_s.pluralize}") : all
         | 
    
        data/lib/area_cn/code.rb
    CHANGED
    
    | @@ -1,13 +1,12 @@ | |
| 1 1 | 
             
            module AreaCN
         | 
| 2 | 
            -
              class Code
         | 
| 3 | 
            -
                include Comparable
         | 
| 4 | 
            -
             | 
| 2 | 
            +
              class Code < String
         | 
| 5 3 | 
             
                ROOT = "000000"
         | 
| 6 4 |  | 
| 7 | 
            -
                 | 
| 5 | 
            +
                attr_reader :code
         | 
| 8 6 |  | 
| 9 7 | 
             
                def initialize(code)
         | 
| 10 8 | 
             
                  @code = code
         | 
| 9 | 
            +
                  super(code)
         | 
| 11 10 | 
             
                end
         | 
| 12 11 |  | 
| 13 12 | 
             
                def value
         | 
| @@ -17,10 +16,10 @@ module AreaCN | |
| 17 16 | 
             
                def prefix
         | 
| 18 17 | 
             
                  return if code == ROOT
         | 
| 19 18 | 
             
                  return @prefix if @prefix
         | 
| 20 | 
            -
                   | 
| 21 | 
            -
                   | 
| 19 | 
            +
                  @prefix = code.sub(/0{2,4}$/, '') 
         | 
| 20 | 
            +
                  @prefix << '0' if @prefix.length.odd?
         | 
| 22 21 |  | 
| 23 | 
            -
                   | 
| 22 | 
            +
                  @prefix 
         | 
| 24 23 | 
             
                end
         | 
| 25 24 |  | 
| 26 25 | 
             
                def parent
         | 
| @@ -31,10 +30,10 @@ module AreaCN | |
| 31 30 | 
             
                def ancestors
         | 
| 32 31 | 
             
                  return @ancestors if @ancestors
         | 
| 33 32 | 
             
                  child = self
         | 
| 34 | 
            -
                  ancestors = [self] 
         | 
| 35 | 
            -
                  ancestors << child while child = child.parent 
         | 
| 33 | 
            +
                  @ancestors = [self] 
         | 
| 34 | 
            +
                  @ancestors << child while child = child.parent 
         | 
| 36 35 |  | 
| 37 | 
            -
                  ancestors
         | 
| 36 | 
            +
                  @ancestors
         | 
| 38 37 | 
             
                end
         | 
| 39 38 |  | 
| 40 39 | 
             
                def ancestor?(other)
         | 
| @@ -43,20 +42,7 @@ module AreaCN | |
| 43 42 | 
             
                end
         | 
| 44 43 |  | 
| 45 44 | 
             
                def child?(other)
         | 
| 46 | 
            -
                   | 
| 47 | 
            -
                  other.ancestor?(self)
         | 
| 48 | 
            -
                end
         | 
| 49 | 
            -
             | 
| 50 | 
            -
                def <=>(other)
         | 
| 51 | 
            -
                  self.code <=> other.code
         | 
| 52 | 
            -
                end
         | 
| 53 | 
            -
             | 
| 54 | 
            -
                def inspect
         | 
| 55 | 
            -
                  @code
         | 
| 56 | 
            -
                end
         | 
| 57 | 
            -
             | 
| 58 | 
            -
                def to_s
         | 
| 59 | 
            -
                  @code
         | 
| 45 | 
            +
                  self.ancestors.include?(other)
         | 
| 60 46 | 
             
                end
         | 
| 61 47 | 
             
              end
         | 
| 62 48 | 
             
            end
         | 
    
        data/lib/area_cn/version.rb
    CHANGED
    
    
    
        data/lib/area_cn.rb
    CHANGED
    
    | @@ -23,5 +23,21 @@ module AreaCN | |
| 23 23 | 
             
                def match(name, area_level = nil)
         | 
| 24 24 | 
             
                  areas.match(name, area_level)
         | 
| 25 25 | 
             
                end
         | 
| 26 | 
            +
             | 
| 27 | 
            +
                def provinces
         | 
| 28 | 
            +
                  areas.provinces
         | 
| 29 | 
            +
                end
         | 
| 30 | 
            +
             | 
| 31 | 
            +
                def cities
         | 
| 32 | 
            +
                  areas.cities
         | 
| 33 | 
            +
                end
         | 
| 34 | 
            +
             | 
| 35 | 
            +
                def districts
         | 
| 36 | 
            +
                  areas.districts
         | 
| 37 | 
            +
                end
         | 
| 38 | 
            +
             | 
| 39 | 
            +
                def all
         | 
| 40 | 
            +
                  areas.all
         | 
| 41 | 
            +
                end
         | 
| 26 42 | 
             
              end
         | 
| 27 43 | 
             
            end
         | 
    
        data/spec/area_cn_spec.rb
    CHANGED
    
    | @@ -49,7 +49,21 @@ describe AreaCN, "中国地区数据" do | |
| 49 49 | 
             
              end
         | 
| 50 50 |  | 
| 51 51 | 
             
              describe AreaCN::Code do
         | 
| 52 | 
            -
                 | 
| 52 | 
            +
                let(:code) { code = AreaCN::Code.new('110100') }
         | 
| 53 | 
            +
                  
         | 
| 54 | 
            +
                it "code should behave like string" do
         | 
| 55 | 
            +
                  code.should == '110100'
         | 
| 56 | 
            +
             | 
| 57 | 
            +
                  code.should > '110000'
         | 
| 58 | 
            +
             | 
| 59 | 
            +
                  "code: #{code}".should == "code: 110100"
         | 
| 60 | 
            +
                end
         | 
| 61 | 
            +
             | 
| 62 | 
            +
                it "#value should return raw code string" do
         | 
| 63 | 
            +
                  code.value.should == '110100'
         | 
| 64 | 
            +
                end
         | 
| 65 | 
            +
             | 
| 66 | 
            +
                it "#prefix should return code's prefix" do
         | 
| 53 67 | 
             
                  code = AreaCN::Code.new("110000")
         | 
| 54 68 | 
             
                  code.prefix.should == '11'
         | 
| 55 69 |  | 
| @@ -63,26 +77,57 @@ describe AreaCN, "中国地区数据" do | |
| 63 77 | 
             
                  code.prefix.should == '1101'
         | 
| 64 78 | 
             
                end
         | 
| 65 79 |  | 
| 66 | 
            -
                it "code  | 
| 80 | 
            +
                it "#parent should return parent code instance" do
         | 
| 67 81 | 
             
                  code = AreaCN::Code.new("110000")
         | 
| 82 | 
            +
                  code.parent.should == "000000"
         | 
| 68 83 | 
             
                  code.parent.should == AreaCN::Code.new("000000")
         | 
| 69 84 |  | 
| 70 85 | 
             
                  AreaCN::Code.new("000000").parent.should == nil
         | 
| 71 86 | 
             
                end
         | 
| 72 87 |  | 
| 73 | 
            -
                it " | 
| 88 | 
            +
                it "#ancestors" do
         | 
| 89 | 
            +
                  code.ancestors.should include("110100")
         | 
| 90 | 
            +
                  code.ancestors.should == ['110100', '110000', '000000']
         | 
| 91 | 
            +
                end
         | 
| 92 | 
            +
             | 
| 93 | 
            +
                it "code ancestor?" do
         | 
| 94 | 
            +
                  raw_code1 = '330101'
         | 
| 95 | 
            +
                  raw_code2 = '330100'
         | 
| 96 | 
            +
                  raw_code3 = '330000'
         | 
| 97 | 
            +
             | 
| 74 98 | 
             
                  code1 = AreaCN::Code.new("330101")
         | 
| 75 99 | 
             
                  code2 = AreaCN::Code.new("330100")
         | 
| 76 100 | 
             
                  code3 = AreaCN::Code.new("330000")
         | 
| 77 101 |  | 
| 78 102 | 
             
                  code2.ancestor?(code1).should be_true
         | 
| 103 | 
            +
                  code2.ancestor?(raw_code1).should be_true
         | 
| 104 | 
            +
             | 
| 79 105 | 
             
                  code3.ancestor?(code2).should be_true
         | 
| 106 | 
            +
                  code3.ancestor?(raw_code2).should be_true
         | 
| 107 | 
            +
             | 
| 80 108 | 
             
                  code3.ancestor?(code1).should be_true
         | 
| 109 | 
            +
                  code3.ancestor?(raw_code1).should be_true
         | 
| 81 110 |  | 
| 82 111 | 
             
                  code1.child?(code2).should be_true
         | 
| 83 112 | 
             
                  code1.child?(code3).should be_true
         | 
| 84 113 | 
             
                end
         | 
| 85 114 |  | 
| 115 | 
            +
                it "#child" do
         | 
| 116 | 
            +
                  raw_code1 = '330101'
         | 
| 117 | 
            +
                  raw_code2 = '330100'
         | 
| 118 | 
            +
                  raw_code3 = '330000'
         | 
| 119 | 
            +
             | 
| 120 | 
            +
                  code1 = AreaCN::Code.new("330101")
         | 
| 121 | 
            +
                  code2 = AreaCN::Code.new("330100")
         | 
| 122 | 
            +
                  code3 = AreaCN::Code.new("330000")
         | 
| 123 | 
            +
             | 
| 124 | 
            +
                  code1.child?(code2).should be_true
         | 
| 125 | 
            +
                  code1.child?(raw_code2).should be_true
         | 
| 126 | 
            +
             | 
| 127 | 
            +
                  code1.child?(code3).should be_true
         | 
| 128 | 
            +
                  code1.child?(raw_code3).should be_true
         | 
| 129 | 
            +
                end
         | 
| 130 | 
            +
             | 
| 86 131 | 
             
                it "code should can be compare" do
         | 
| 87 132 | 
             
                  AreaCN::Code.new("330101").should == AreaCN::Code.new("330101")
         | 
| 88 133 | 
             
                end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: area_cn
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.0. | 
| 4 | 
            +
              version: 0.0.5
         | 
| 5 5 | 
             
              prerelease: 
         | 
| 6 6 | 
             
            platform: ruby
         | 
| 7 7 | 
             
            authors:
         | 
| @@ -9,7 +9,7 @@ authors: | |
| 9 9 | 
             
            autorequire: 
         | 
| 10 10 | 
             
            bindir: bin
         | 
| 11 11 | 
             
            cert_chain: []
         | 
| 12 | 
            -
            date: 2012-10- | 
| 12 | 
            +
            date: 2012-10-25 00:00:00.000000000 Z
         | 
| 13 13 | 
             
            dependencies:
         | 
| 14 14 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 15 15 | 
             
              name: rspec
         |