Skip to content

Commit

Permalink
+ Move work to the initialization stage (instead of during runtime)
Browse files Browse the repository at this point in the history
  • Loading branch information
floere committed Mar 18, 2013
1 parent 8464c77 commit e71e18c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
6 changes: 4 additions & 2 deletions lib/phony/dsl.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ class DSL
# Example: # Example:
# country '27', # CC, followed by rules, for example fixed(2) >> ... # country '27', # CC, followed by rules, for example fixed(2) >> ...
# #
def country country_code, country, validator = nil def country country_code, country, validators = nil
Phony::CountryCodes.instance.add country_code, country Phony::CountryCodes.instance.add country_code, country
Phony::Validators.instance.add country_code, validator if validator [*validators].each do |validator|
Phony::Validators.instance.add country_code, validator
end if validators
end end


# National matcher & splitters. # National matcher & splitters.
Expand Down
16 changes: 8 additions & 8 deletions lib/phony/validators.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ def initialize
@country_validators = {} @country_validators = {}
end end



def self.instance def self.instance
@instance ||= new @instance ||= new
end end


# Add a specific country validator (or an array of validators) # Add a specific country validator.
# #
def add cc, validator def add cc, validator
@country_validators[cc] = validator @country_validators[cc] ||= []
@country_validators[cc] << validator
end end


# Is the given number plausible? # Is the given number plausible?
Expand Down Expand Up @@ -72,7 +72,7 @@ def plausible? number, hints = {}


# Country specific checks. # Country specific checks.
# #
validators = [*validator_for(cc)] validators = validators_for cc
validators.map do |validator| validators.map do |validator|
return false unless validator.plausible? ndc, rest return false unless validator.plausible? ndc, rest
end end
Expand All @@ -82,12 +82,12 @@ def plausible? number, hints = {}
return false return false
end end


def validator_for cc def validators_for cc
@country_validators[cc] || default_validator @country_validators[cc] || default_validators
end end


def default_validator def default_validators
@default_validator ||= Validator.new @default_validators ||= [Validator.new]
end end


end end
Expand Down

0 comments on commit e71e18c

Please sign in to comment.