Class: DataMaker::CN::Address::GenerateAddress

Inherits:
Object
  • Object
show all
Defined in:
lib/data_maker/cn/address.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ GenerateAddress



36
37
38
39
40
41
42
43
44
45
# File 'lib/data_maker/cn/address.rb', line 36

def initialize(*args)
  self.options  = (args.last.is_a?(::Hash) ? args.last : {}).delete_if { |k, v| v.nil? }
  self.district = options[:district]
  self.city     = options[:city]
  self.province = options[:province]
  self.locale   = options[:locale] || :zh
  unless options.empty?
    validate
  end
end

Instance Attribute Details

#cityObject

Returns the value of attribute city.



34
35
36
# File 'lib/data_maker/cn/address.rb', line 34

def city
  @city
end

#districtObject

Returns the value of attribute district.



34
35
36
# File 'lib/data_maker/cn/address.rb', line 34

def district
  @district
end

#localeObject

Returns the value of attribute locale.



34
35
36
# File 'lib/data_maker/cn/address.rb', line 34

def locale
  @locale
end

#optionsObject

Returns the value of attribute options.



34
35
36
# File 'lib/data_maker/cn/address.rb', line 34

def options
  @options
end

#provinceObject

Returns the value of attribute province.



34
35
36
# File 'lib/data_maker/cn/address.rb', line 34

def province
  @province
end

#street_addressObject

Returns the value of attribute street_address.



34
35
36
# File 'lib/data_maker/cn/address.rb', line 34

def street_address
  @street_address
end

Instance Method Details

#address_stringObject



70
71
72
73
# File 'lib/data_maker/cn/address.rb', line 70

def address_string
  generate
  build_address
end

#address_structObject



75
76
77
78
79
80
81
82
83
# File 'lib/data_maker/cn/address.rb', line 75

def address_struct
  generate
  OpenStruct.new(street_address: street_address,
                 city:           translate('city', city),
                 district:       (translate('district', district) unless district.nil?),
                 province:       translate('province', province),
                 postal_code:    postal_code
                )
end

#generateObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/data_maker/cn/address.rb', line 47

def generate
  if options.empty?
    # Generate a random address from a province
    self.province = provinces.sample
    self.city     = province_cities.sample
    self.district = city_districts.nil? ? nil : city_districts.sample
  end

  if options[:province] && options[:city].nil? && options[:district].nil?
    self.city     = province_cities.sample
    self.district = city_districts.nil? ? nil : city_districts.sample
  end

  if options[:province].nil? && options[:city] && options[:district].nil?
    self.province = city_province
    self.district = city_districts.nil? ? nil : city_districts.sample
  end
end

#postal_codeObject



85
86
87
88
89
90
91
92
93
# File 'lib/data_maker/cn/address.rb', line 85

def postal_code
  postal_code = nil
  province_postal_codes = DataMaker::CN::Address::PROVINCE_POSTAL_CODES
  province_postal_codes.to_set.select do |province_postal_code|
    p, pc = province_postal_code.split(",")
    p == province ? postal_code = pc : nil
  end
  postal_code
end