Class: Phony::DSL

Inherits:
Object
  • Object
show all
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.

Examples:

Phony.define { ... }

Instance Method Summary collapse

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.

Examples:

Add a country with country code 27.

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

Parameters:

  • country_code (String)

    The country code of the country.

  • definition (Phony::CountryCodes)

    Rules for this country.

Returns:

  • Undefined.



43
44
45
46
47
48
# File 'lib/phony/dsl.rb', line 43

def country(country_code, definition, options = {})
  return unless Phony.config.load?(country_code)

  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.

Examples:

France. Uses a fixed NDC of size 1.

country '33', fixed(1) >> split(2,2,2,2)

Parameters:

  • length (Fixnum)

    The length of the fixed length NDC.

  • options (Hash) (defaults to: {})

    An options hash (set zero: false to not add a zero on format :national).

Returns:

  • NationalSplitters::Fixed A fixed length national splitter.



110
111
112
113
# File 'lib/phony/dsl.rb', line 110

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?

Examples:

NANP

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

Parameters:

  • ndc (Regexp, String)

    A regexp or a string of invalid NDCs.

Returns:

  • Validators::NDC An NDC validator



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.

Examples:

With several NDC matchers.

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)

Parameters:

  • regex (Regexp)

    A Regexp describing the NDCs (First group must contain the NDC, eg. /^(0d2)d+$/) contains all NDCs with 0xx…

Returns:

  • NationalSplitters::Regex A regexp-based national splitter.



160
161
162
163
164
165
166
# File 'lib/phony/dsl.rb', line 160

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 /\(/.match?(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.

Examples:

Norway

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

Parameters:

  • options (Hash) (defaults to: {})

    Can contain option :fallback A fallback amount of group sizes in case it doesn’t match.

Returns:

  • LocalSplitters::Regex A regexp-based local splitter.



202
203
204
# File 'lib/phony/dsl.rb', line 202

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

#noneObject

Marks the country as not using an NDC. This rule will always match.

Examples:

Denmark, Norway, Iceland.

Returns:

  • NationalSplitters::None A no-ndc national splitter.



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.

Examples:

With two NDCs

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

Parameters:

  • ndcs (Array<String>)

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

Returns:

  • NationalSplitters::Variable A variable size national splitter.



137
138
139
140
141
142
143
144
145
# File 'lib/phony/dsl.rb', line 137

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

  # Ruby 1.8 compatibility mode.
  #
  ndcs = ndcs.first if ndcs.first.is_a?(Array)

  NationalSplitters::Variable.new options[: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.

Examples:

Designate country code 27 as reserved.

reserved('27')

Parameters:

  • country_code (String)

    The country code of the country.

Returns:

  • nil



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.

Examples:

Splitting in 4 even groups of two digits.

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

Parameters:

  • local (Array<Fixnum>)

    The local group sizes to split the local number part into.

Returns:

  • LocalSplitters::Fixed A fixed size local splitter.



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

#todoObject

Define a country to use default rules (and to be done at some point).

Examples:

Define country 27 to use default rules.

country '27', todo

Returns:

  • Rules for a country.



71
72
73
# File 'lib/phony/dsl.rb', line 71

def todo
  none >> split(10)
end

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

This country uses a trunk code.

Examples:

Hungary uses 06.

country '36', trunk('06', normalize: false) | ...

North America uses 1.

country '1', trunk('1%s', normalize: true) | ...

Most countries which use a trunk code use 0. E.g. Romania.

country '40', trunk('0') | ...

Parameters:

  • code (String)

    The trunk code.

  • options (Hash) (defaults to: {})

    Options hash. Pass :normalize (true/false) to indicate whether it needs to be normalized.

Returns:

  • TrunkCode A trunk code handler.



91
92
93
# File 'lib/phony/dsl.rb', line 91

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