Class: Tr8n::TokenizedLabel
- Inherits:
-
Object
- Object
- Tr8n::TokenizedLabel
- Defined in:
- lib/tr8n/tokenized_label.rb
Instance Method Summary collapse
- #allowed_token?(token) ⇒ Boolean
-
#data_tokens ⇒ Object
scans for all token types.
- #data_tokens? ⇒ Boolean
- #decoration_tokens ⇒ Object
- #decoration_tokens? ⇒ Boolean
-
#initialize(label) ⇒ TokenizedLabel
constructor
constracts the label.
- #label ⇒ Object
- #sanitized_label ⇒ Object
- #sanitized_tokens_hash ⇒ Object
- #suggestion_tokens ⇒ Object
- #tokenless_label ⇒ Object
- #tokens ⇒ Object
- #tokens? ⇒ Boolean
-
#translation_tokens ⇒ Object
tokens that can be used by the user in translation.
- #translation_tokens? ⇒ Boolean
- #words ⇒ Object
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
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_tokens ⇒ Object
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
41 42 43 |
# File 'lib/tr8n/tokenized_label.rb', line 41 def data_tokens? data_tokens.any? end |
#decoration_tokens ⇒ Object
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
49 50 51 |
# File 'lib/tr8n/tokenized_label.rb', line 49 def decoration_tokens? decoration_tokens.any? end |
#label ⇒ Object
32 33 34 |
# File 'lib/tr8n/tokenized_label.rb', line 32 def label @label end |
#sanitized_label ⇒ Object
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_hash ⇒ Object
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_tokens ⇒ Object
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_label ⇒ Object
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 |
#tokens ⇒ Object
53 54 55 |
# File 'lib/tr8n/tokenized_label.rb', line 53 def tokens @tokens = data_tokens + decoration_tokens end |
#tokens? ⇒ Boolean
67 68 69 |
# File 'lib/tr8n/tokenized_label.rb', line 67 def tokens? tokens.any? end |
#translation_tokens ⇒ Object
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
76 77 78 |
# File 'lib/tr8n/tokenized_label.rb', line 76 def translation_tokens? translation_tokens.any? end |
#words ⇒ Object
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 |