Class: Tr8n::TokenizedLabel

Inherits:
Object
  • Object
show all
Defined in:
lib/tr8n/tokenized_label.rb

Instance Method Summary collapse

Constructor Details

#initialize(label) ⇒ TokenizedLabel

constracts the label



28
29
30
# File 'lib/tr8n/tokenized_label.rb', line 28

def initialize(label)
  @label = label
end

Instance Method Details

#allowed_token?(token) ⇒ Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/tr8n/tokenized_label.rb', line 129

def allowed_token?(token)
  not sanitized_tokens_hash[token.sanitized_name].nil?
end

#data_tokensObject

scans for all token types



37
38
39
# File 'lib/tr8n/tokenized_label.rb', line 37

def data_tokens
  @data_tokens ||= Tr8n::Token.register_data_tokens(label)
end

#data_tokens?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/tr8n/tokenized_label.rb', line 41

def data_tokens?
  data_tokens.any?
end

#decoration_tokensObject



45
46
47
# File 'lib/tr8n/tokenized_label.rb', line 45

def decoration_tokens
  @decoration_tokens ||= Tr8n::Token.register_decoration_tokens(label)
end

#decoration_tokens?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/tr8n/tokenized_label.rb', line 49

def decoration_tokens?
  decoration_tokens.any?
end

#labelObject



32
33
34
# File 'lib/tr8n/tokenized_label.rb', line 32

def label
  @label
end

#sanitized_labelObject



80
81
82
83
84
85
86
87
88
# File 'lib/tr8n/tokenized_label.rb', line 80

def sanitized_label
  @sanitized_label ||= begin 
    lbl = label.clone
    data_tokens.each do |token|
      lbl = token.prepare_label_for_translator(lbl)
    end
    lbl
  end 
end

#sanitized_tokens_hashObject



57
58
59
60
61
62
63
64
65
# File 'lib/tr8n/tokenized_label.rb', line 57

def sanitized_tokens_hash
  @sanitized_tokens_hash ||= begin
    hash = {}
    tokens.each do |token|
      hash[token.sanitized_name] = token
    end
    hash
  end
end

#suggestion_tokensObject



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/tr8n/tokenized_label.rb', line 100

def suggestion_tokens
  @suggestion_tokens ||= begin
    toks = []
    tokens.each do |token|
      if token.decoration?
        toks << token.name
      else  
        toks << token.sanitized_name          
      end
    end
    toks
  end
end

#tokenless_labelObject



90
91
92
93
94
95
96
97
98
# File 'lib/tr8n/tokenized_label.rb', line 90

def tokenless_label
  @tokenless_label ||= begin
    lbl = label.clone
    tokens.each_with_index do |token, index|
      lbl = token.prepare_label_for_suggestion(lbl, index)
    end
    lbl
  end
end

#tokensObject



53
54
55
# File 'lib/tr8n/tokenized_label.rb', line 53

def tokens
  @tokens = data_tokens + decoration_tokens
end

#tokens?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/tr8n/tokenized_label.rb', line 67

def tokens?
  tokens.any?
end

#translation_tokensObject

tokens that can be used by the user in translation



72
73
74
# File 'lib/tr8n/tokenized_label.rb', line 72

def translation_tokens
  @translation_tokens ||= tokens.select{|token| token.allowed_in_translation?} 
end

#translation_tokens?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/tr8n/tokenized_label.rb', line 76

def translation_tokens?
  translation_tokens.any?
end

#wordsObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/tr8n/tokenized_label.rb', line 114

def words
  return [] if label.blank?

  @words ||= begin 
    clean_label = sanitized_label
    parts = []
    clean_label = clean_label.gsub(/[\,\.\;\!\-\:\'\"\[\]{}]/, "")
  
    clean_label.split(" ").each do |w|
      parts << w.strip.capitalize if w.length > 3
    end
    parts
  end
end