Class: TwitterCldr::Shared::PostalCodes
- Inherits:
-
Object
- Object
- TwitterCldr::Shared::PostalCodes
- Defined in:
- lib/twitter_cldr/shared/postal_codes.rb
Instance Attribute Summary collapse
-
#ast_source ⇒ Object
readonly
Returns the value of attribute ast_source.
-
#multi_regexp ⇒ Object
readonly
Returns the value of attribute multi_regexp.
-
#regexp ⇒ Object
readonly
Returns the value of attribute regexp.
-
#single_regexp ⇒ Object
readonly
Returns the value of attribute single_regexp.
-
#territory ⇒ Object
readonly
Returns the value of attribute territory.
Class Method Summary collapse
Instance Method Summary collapse
- #find_all(postal_codes) ⇒ Object
- #has_generator? ⇒ Boolean
-
#initialize(territory, regexp, ast_source) ⇒ PostalCodes
constructor
A new instance of PostalCodes.
- #sample(sample_size = 1) ⇒ Object
- #valid?(postal_code) ⇒ Boolean
Constructor Details
#initialize(territory, regexp, ast_source) ⇒ PostalCodes
Returns a new instance of PostalCodes.
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/twitter_cldr/shared/postal_codes.rb', line 45 def initialize(territory, regexp, ast_source) @territory = territory @regexp = regexp if @regexp @single_regexp = build_regexp "\\A#{regexp.source}\\z" @multi_regexp = build_regexp "\\b#{regexp.source}\\b" end @ast_source = ast_source end |
Instance Attribute Details
#ast_source ⇒ Object (readonly)
Returns the value of attribute ast_source.
43 44 45 |
# File 'lib/twitter_cldr/shared/postal_codes.rb', line 43 def ast_source @ast_source end |
#multi_regexp ⇒ Object (readonly)
Returns the value of attribute multi_regexp.
43 44 45 |
# File 'lib/twitter_cldr/shared/postal_codes.rb', line 43 def multi_regexp @multi_regexp end |
#regexp ⇒ Object (readonly)
Returns the value of attribute regexp.
43 44 45 |
# File 'lib/twitter_cldr/shared/postal_codes.rb', line 43 def regexp @regexp end |
#single_regexp ⇒ Object (readonly)
Returns the value of attribute single_regexp.
43 44 45 |
# File 'lib/twitter_cldr/shared/postal_codes.rb', line 43 def single_regexp @single_regexp end |
#territory ⇒ Object (readonly)
Returns the value of attribute territory.
43 44 45 |
# File 'lib/twitter_cldr/shared/postal_codes.rb', line 43 def territory @territory end |
Class Method Details
.for_territory(territory) ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/twitter_cldr/shared/postal_codes.rb', line 20 def for_territory(territory) key = territory.to_s.downcase.to_sym if res = resource[key] territory_cache[key] ||= new( territory, res[:regex], res[:ast] ) else raise InvalidTerritoryError, "invalid territory" end end |
.territories ⇒ Object
16 17 18 |
# File 'lib/twitter_cldr/shared/postal_codes.rb', line 16 def territories resource.keys end |
Instance Method Details
#find_all(postal_codes) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/twitter_cldr/shared/postal_codes.rb', line 61 def find_all(postal_codes) # we cannot use String#scan here as some of the CLDR regular # expressions have capture groups while others don't making # it completely unpredictable what that method might return offset = 0; matches = [] while match = multi_regexp.match(postal_codes, offset) matches << match[0] offset += match.offset(0)[1] end matches end |
#has_generator? ⇒ Boolean
77 78 79 |
# File 'lib/twitter_cldr/shared/postal_codes.rb', line 77 def has_generator? !!ast_source end |
#sample(sample_size = 1) ⇒ Object
73 74 75 |
# File 'lib/twitter_cldr/shared/postal_codes.rb', line 73 def sample(sample_size = 1) generator.sample(sample_size) end |
#valid?(postal_code) ⇒ Boolean
57 58 59 |
# File 'lib/twitter_cldr/shared/postal_codes.rb', line 57 def valid?(postal_code) !!(single_regexp && single_regexp =~ postal_code) end |