Class: Phony::DSL

Inherits:
Object
  • Object
show all
Defined in:
lib/phony/dsl.rb

Instance Method Summary collapse

Instance Method Details

#country(country_code, definition, options = {}) ⇒ Object

Start defining a country.

Example:

country '27', # CC, followed by rules, for example fixed(2) >> ...

52
53
54
55
# File 'lib/phony/dsl.rb', line 52

def country country_code, definition, options = {}
  definition.with country_code, options
  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.

Options:

* length: Length of the fixed length NDC.
* options: Set :zero to false to not add a zero on format :national.

Example:

# France, uses a fixed NDC of size 1.
country '33', fixed(1) >> split(2,2,2,2)

95
96
97
98
# File 'lib/phony/dsl.rb', line 95

def fixed length, options = {}
  options[:zero] = true if options[:zero].nil?
  NationalSplitters::Fixed.instance_for length, options
end

#invalid_ndcs(*ndc) ⇒ Object

Which NDCs are explicitly invalid?

Takes a regexp or a string.

Example:

country '1',
  fixed(3, :zero => false) >> split(3,4),
  invalid_ndcs('911') # The regexp /911/ would also work.

200
201
202
# File 'lib/phony/dsl.rb', line 200

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.

Parameters:

* regex A regexp describing the NDCs (First group must contain the NDC, eg. /^(0\d{2})\d+$/) contains all NDCs with 0xx...

Example:

country '52',
  match(/^(0\d{2})\d+$/)   >> split(2,2,2,2) |
  match(/^(33|55|81)\d+$/) >> split(2,2,2,2) |
  match(/^(\d{3})\d+$/)    >> split(3,2,2)

146
147
148
149
150
151
152
# File 'lib/phony/dsl.rb', line 146

def match regex, options = {}
  # 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 regex.source =~ /\(/
  
  NationalSplitters::Regex.instance_for regex, options[:on_fail_take], options
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.

Options:

* fallback A fallback amount of group sizes in case it doesn't match.

Example:

# Norway
country '47',
  none >> matched_split(/^[1].*$/   => [3],
                        /^[489].*$/ => [3,2,3],
                        :fallback   => [2,2,2,2])

184
185
186
# File 'lib/phony/dsl.rb', line 184

def matched_split options = {}
  Phony::LocalSplitters::Regex.instance_for options
end

#noneObject

This country does not use an NDC. This rule will always match.

Examples:

* Denmark
* Norway
* Iceland
(and more)

108
109
110
# File 'lib/phony/dsl.rb', line 108

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.

Parameters:

* ndcs A list of ndcs of variable length.
* Can contain :max_length => number to denote a maximum NDC length.

Example:

country '51',
  # If it's either 103 or 105, then split ...
  one_of('103', '105') >> split(3,3)

124
125
126
127
128
129
130
131
132
# File 'lib/phony/dsl.rb', line 124

def one_of *ndcs
  options = Hash === ndcs.last ? ndcs.pop : {}

  # Ruby 1.8 compatibility mode.
  #
  ndcs = ndcs.first if Array === ndcs.first

  NationalSplitters::Variable.new options[:max_length], ndcs
end

#reserved(country_code) ⇒ Object

Designates a country code as reserved.


59
60
61
# File 'lib/phony/dsl.rb', line 59

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.

Example:

match(/^(0\d{2})\d+$/) >> split(2,2,2,2) # If it matches, split in 4 groups of size 2.

164
165
166
167
# File 'lib/phony/dsl.rb', line 164

def split *local
  # local << local.pop + 10 # Allow for call-through numbers with an arbitrary size.
  LocalSplitters::Fixed.instance_for local
end

#todoObject

This country still uses a default NDC (and needs to be done, hence the todo).


65
66
67
# File 'lib/phony/dsl.rb', line 65

def todo
  none >> split(10)
end

#trunk(code, options = {}) ⇒ Object

This country uses a trunk code.

Examples:

* Hungary uses 06.
* North America uses 1.
* Most countries which use a trunk code use 0.

76
77
78
# File 'lib/phony/dsl.rb', line 76

def trunk code, options = {}
  TrunkCode.new code, options
end