Module: HumanID::Canonicalization

Defined in:
lib/humanid/canonicalization.rb

Defined Under Namespace

Classes: Behaviour, MalformedHumanIDError

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.separator_sequence_regexObject

Returns the value of attribute separator_sequence_regex.



53
54
55
# File 'lib/humanid/canonicalization.rb', line 53

def separator_sequence_regex
  @separator_sequence_regex
end

.surrounding_separators_regexObject

Returns the value of attribute surrounding_separators_regex.



53
54
55
# File 'lib/humanid/canonicalization.rb', line 53

def surrounding_separators_regex
  @surrounding_separators_regex
end

Class Method Details

.behaviourObject



49
50
51
# File 'lib/humanid/canonicalization.rb', line 49

def behaviour
  Behaviour.instance
end

.perform(str) ⇒ Object

HumanID::Canonicalization.perform(‘Well-known English writer’)

=> 'Well-known_English_writer'

HumanID::Canonicalization.perform(‘Пушкин, Александр Сергеевич’)

=> 'Пушкин,_Александр_Сергеевич'


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/humanid/canonicalization.rb', line 14

def perform(str)
  separator = behaviour.separator

  str = str.join(separator) if str.is_a?(Array)

  # This doesn't require comments
  str = UnicodeTools.strip_bidi_override_chars(str)

  # Replace all whitespace characters (including leading,
  # trailing and inner) with HumanID.separator
  str = UnicodeTools.replace_whitespace(str, separator)

  # Fix for Rails format in routes
  # http://coding-journal.com/rails-3-routing-parameters-with-dots/
  str = str.tr('.', separator)

  # Strip leading and trailing separators
  str = str.gsub(surrounding_separators_regex, '')

  # Replace two or more separator sequence with one separator: '__' => '_'
  str.gsub(separator_sequence_regex, separator)
end

.valid?(human_id) ⇒ Boolean



37
38
39
40
41
42
# File 'lib/humanid/canonicalization.rb', line 37

def valid?(human_id)
  human_id = human_id.to_s if human_id.kind_of?(Symbol)
  human_id.kind_of?(String) &&
    UnicodeTools.has_bidi_override?(human_id) == false &&
    UnicodeTools.has_whitespace?(human_id)    == false
end

.validate!(human_id) ⇒ Object



44
45
46
47
# File 'lib/humanid/canonicalization.rb', line 44

def validate!(human_id)
  raise MalformedHumanIDError unless valid?(human_id)
  true
end