Class: Phony::DSL
- Inherits:
-
Object
- Object
- Phony::DSL
- Defined in:
- lib/phony/dsl.rb
Overview
Contains a number of DSL methods. Is used e.g. in the countries.rb file.
Do not use directly, but instead use define to execute a block in the DSL instance’s context.
Instance Method Summary collapse
-
#country(country_code, definition, options = {}) ⇒ Object
Define a country’s rules.
-
#fixed(length, options = {}) ⇒ Object
By default, a fixed length NDC uses a zero prefix when formatted with format :national.
-
#invalid_ndcs(*ndc) ⇒ Object
Which NDCs are explicitly invalid?.
-
#match(regex, options = {}) ⇒ Object
If you have a number of (possibly) variable length NDCs that can be well expressed via regexp, use this.
-
#matched_split(options = {}) ⇒ Object
Matches on the rest of the number and splits according to the given value for the regexp key.
-
#none ⇒ Object
Marks the country as not using an NDC.
-
#one_of(*ndcs) ⇒ Object
If you have a number of (possibly) variable length NDCs that cannot be well expressed via regexp, use this.
-
#reserved(country_code) ⇒ Object
Designates a country code as reserved.
-
#split(*local) ⇒ Object
Splits the number into groups of given sizes.
-
#todo ⇒ Object
Define a country to use default rules (and to be done at some point).
-
#trunk(code, options = {}) ⇒ Object
This country uses a trunk code.
Instance Method Details
#country(country_code, definition, options = {}) ⇒ Object
Define a country’s rules.
Use the other DSL methods to define the country’s rules.
43 44 45 46 47 48 |
# File 'lib/phony/dsl.rb', line 43 def country(country_code, definition, = {}) return unless Phony.config.load?(country_code) definition.with country_code, Phony::CountryCodes.instance.add country_code, definition end |
#fixed(length, options = {}) ⇒ Object
By default, a fixed length NDC uses a zero prefix when formatted with format :national.
110 111 112 113 |
# File 'lib/phony/dsl.rb', line 110 def fixed(length, = {}) [:zero] = true if [:zero].nil? NationalSplitters::Fixed.instance_for length, end |
#invalid_ndcs(*ndc) ⇒ Object
Which NDCs are explicitly invalid?
220 221 222 |
# File 'lib/phony/dsl.rb', line 220 def invalid_ndcs *ndc Validators::NDC.new invalid: ndc end |
#match(regex, options = {}) ⇒ Object
If you have a number of (possibly) variable length NDCs that can be well expressed via regexp, use this.
160 161 162 163 164 165 166 |
# File 'lib/phony/dsl.rb', line 160 def match(regex, = {}) # Check if regexp has a group in it. # raise "Regexp /#{regex.source}/ needs a group in it that defines which digits belong to the NDC." unless /\(/.match?(regex.source) NationalSplitters::Regex.instance_for regex, [:on_fail_take], end |
#matched_split(options = {}) ⇒ Object
Matches on the rest of the number and splits according to the given value for the regexp key.
Also takes ranges.
202 203 204 |
# File 'lib/phony/dsl.rb', line 202 def matched_split( = {}) LocalSplitters::Regex.instance_for end |
#none ⇒ Object
Marks the country as not using an NDC. This rule will always match.
121 122 123 |
# File 'lib/phony/dsl.rb', line 121 def none NationalSplitters::None.instance_for end |
#one_of(*ndcs) ⇒ Object
If you have a number of (possibly) variable length NDCs that cannot be well expressed via regexp, use this.
137 138 139 140 141 142 143 144 145 |
# File 'lib/phony/dsl.rb', line 137 def one_of *ndcs = ndcs.last.is_a?(Hash) ? ndcs.pop : {} # Ruby 1.8 compatibility mode. # ndcs = ndcs.first if ndcs.first.is_a?(Array) NationalSplitters::Variable.new [:max_length], ndcs.map(&:freeze) end |
#reserved(country_code) ⇒ Object
Designates a country code as reserved. A reserved country will result in an exception when trying to be used.
60 61 62 |
# File 'lib/phony/dsl.rb', line 60 def reserved(country_code) # Does nothing, will just fail with an exception. end |
#split(*local) ⇒ Object
Splits the number into groups of given sizes.
Also takes ranges.
182 183 184 185 |
# File 'lib/phony/dsl.rb', line 182 def split *local # local << local.pop + 10 # Allow for call-through numbers with an arbitrary size. LocalSplitters::Fixed.instance_for local end |
#todo ⇒ Object
Define a country to use default rules (and to be done at some point).
71 72 73 |
# File 'lib/phony/dsl.rb', line 71 def todo none >> split(10) end |