Class: Addressing::Subdivision
- Inherits:
-
Object
- Object
- Addressing::Subdivision
- Defined in:
- lib/addressing/subdivision.rb
Constant Summary collapse
- @@definitions =
Subdivision definitions.
{}
- @@parents =
Parent subdivisions.
Used as a cache to speed up instantiating subdivisions with the same parent. Contains only parents instead of all instantiated subdivisions to minimize duplicating the data in $this->definitions, thus reducing memory usage.
{}
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#country_code ⇒ Object
readonly
Returns the value of attribute country_code.
-
#iso_code ⇒ Object
readonly
Returns the value of attribute iso_code.
-
#local_code ⇒ Object
readonly
Returns the value of attribute local_code.
-
#local_name ⇒ Object
readonly
Returns the value of attribute local_name.
-
#locale ⇒ Object
readonly
Returns the value of attribute locale.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#postal_code_pattern ⇒ Object
readonly
Returns the value of attribute postal_code_pattern.
-
#postal_code_pattern_type ⇒ Object
readonly
Returns the value of attribute postal_code_pattern_type.
Class Method Summary collapse
-
.all(parents) ⇒ Object
Returns all subdivision instances for the provided parents.
- .get(code, parents) ⇒ Object
-
.list(parents, locale = nil) ⇒ Object
Returns a list of subdivisions for the provided parents.
Instance Method Summary collapse
- #children? ⇒ Boolean
-
#initialize(definition = {}) ⇒ Subdivision
constructor
A new instance of Subdivision.
- #to_h ⇒ Object
Constructor Details
#initialize(definition = {}) ⇒ Subdivision
Returns a new instance of Subdivision.
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/addressing/subdivision.rb', line 198 def initialize(definition = {}) # Validate the presence of required properties. [:country_code, :code, :name].each do |required_property| if definition[required_property].nil? raise ArgumentError, "Missing required property #{required_property}." end end # Add defaults for properties that are allowed to be empty. definition = { parent: nil, locale: nil, local_code: nil, local_name: nil, iso_code: nil, postal_code_pattern: nil, postal_code_pattern_type: PatternType.default, children: {} }.merge(definition) @parent = definition[:parent] @country_code = definition[:country_code] @locale = definition[:locale] @code = definition[:code] @local_code = definition[:local_code] @name = definition[:name] @local_name = definition[:local_name] @iso_code = definition[:iso_code] @postal_code_pattern = definition[:postal_code_pattern] @postal_code_pattern_type = definition[:postal_code_pattern_type] @children = definition[:children] end |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
196 197 198 |
# File 'lib/addressing/subdivision.rb', line 196 def children @children end |
#code ⇒ Object (readonly)
Returns the value of attribute code.
196 197 198 |
# File 'lib/addressing/subdivision.rb', line 196 def code @code end |
#country_code ⇒ Object (readonly)
Returns the value of attribute country_code.
196 197 198 |
# File 'lib/addressing/subdivision.rb', line 196 def country_code @country_code end |
#iso_code ⇒ Object (readonly)
Returns the value of attribute iso_code.
196 197 198 |
# File 'lib/addressing/subdivision.rb', line 196 def iso_code @iso_code end |
#local_code ⇒ Object (readonly)
Returns the value of attribute local_code.
196 197 198 |
# File 'lib/addressing/subdivision.rb', line 196 def local_code @local_code end |
#local_name ⇒ Object (readonly)
Returns the value of attribute local_name.
196 197 198 |
# File 'lib/addressing/subdivision.rb', line 196 def local_name @local_name end |
#locale ⇒ Object (readonly)
Returns the value of attribute locale.
196 197 198 |
# File 'lib/addressing/subdivision.rb', line 196 def locale @locale end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
196 197 198 |
# File 'lib/addressing/subdivision.rb', line 196 def name @name end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
196 197 198 |
# File 'lib/addressing/subdivision.rb', line 196 def parent @parent end |
#postal_code_pattern ⇒ Object (readonly)
Returns the value of attribute postal_code_pattern.
196 197 198 |
# File 'lib/addressing/subdivision.rb', line 196 def postal_code_pattern @postal_code_pattern end |
#postal_code_pattern_type ⇒ Object (readonly)
Returns the value of attribute postal_code_pattern_type.
196 197 198 |
# File 'lib/addressing/subdivision.rb', line 196 def postal_code_pattern_type @postal_code_pattern_type end |
Class Method Details
.all(parents) ⇒ Object
Returns all subdivision instances for the provided parents.
23 24 25 26 27 28 29 30 |
# File 'lib/addressing/subdivision.rb', line 23 def all(parents) definitions = load_definitions(parents) return {} if definitions.empty? definitions["subdivisions"].each_with_object({}) do |(code, definition), subdivisions| subdivisions[code] = create_subdivision_from_definitions(code, definitions) end end |
.get(code, parents) ⇒ Object
17 18 19 20 |
# File 'lib/addressing/subdivision.rb', line 17 def get(code, parents) definitions = load_definitions(parents) create_subdivision_from_definitions(code, definitions) end |
.list(parents, locale = nil) ⇒ Object
Returns a list of subdivisions for the provided parents.
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/addressing/subdivision.rb', line 33 def list(parents, locale = nil) definitions = load_definitions(parents) return {} if definitions.empty? use_local_name = Locale.match_candidates(locale, definitions["locale"] || "") definitions["subdivisions"].each_with_object({}) do |(code, definition), list| list[code] = use_local_name ? definition["local_name"] : definition["name"] end end |
Instance Method Details
#children? ⇒ Boolean
231 232 233 |
# File 'lib/addressing/subdivision.rb', line 231 def children? @children.any? end |
#to_h ⇒ Object
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/addressing/subdivision.rb', line 235 def to_h { parent: parent, country_code: country_code, locale: locale, code: code, local_code: local_code, name: name, local_name: local_name, iso_code: iso_code, postal_code_pattern: postal_code_pattern, postal_code_pattern_type: postal_code_pattern_type, children: children } end |