Class: Tr8n::LanguageCase

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

Overview

– Copyright © 2014 Michael Berkovich, TranslationExchange.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ++

Constant Summary collapse

TR8N_HTML_TAGS_REGEX =
/<\/?[^>]*>/

Instance Attribute Summary

Attributes inherited from Base

#attributes

Instance Method Summary collapse

Methods inherited from Base

attributes, belongs_to, has_many, hash_value, #hash_value, #method_missing, #to_hash, #update_attributes

Constructor Details

#initialize(attrs = {}) ⇒ LanguageCase

Returns a new instance of LanguageCase.



32
33
34
35
36
37
38
# File 'lib/tr8n/language_case.rb', line 32

def initialize(attrs = {})
  super
  self.attributes[:rules] = []
  if hash_value(attrs, :rules)
    self.attributes[:rules] = hash_value(attrs, :rules).collect{ |rule| Tr8n::LanguageCaseRule.new(rule.merge(:language_case => self)) }
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Tr8n::Base

Instance Method Details

#apply(value, object = nil, options = {}) ⇒ Object

Evaluation Methods



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/tr8n/language_case.rb', line 51

def apply(value, object = nil, options = {})
  value = value.to_s

  options = options.merge(:skip_decorations => true) if value.index('not_translated')

  html_tokens = value.scan(TR8N_HTML_TAGS_REGEX).uniq
  sanitized_value = value.gsub(TR8N_HTML_TAGS_REGEX, "")

  if application.to_sym == :phrase
    words = [sanitized_value]
  else
    words = sanitized_value.split(/[\s\/\\]/).uniq
  end

  # replace html tokens with temporary placeholders {$h1}
  html_tokens.each_with_index do |html_token, index|
    value = value.gsub(html_token, "{$h#{index}}")
  end

  # replace words with temporary placeholders {$w1}
  words.each_with_index do |word, index|
    value = value.gsub(word, "{$w#{index}}")
  end

  transformed_words = []
  words.each do |word|
    case_rule = find_matching_rule(word, object)
    case_value = case_rule ? case_rule.apply(word) : word
    transformed_words << decorate(word, case_value, case_rule, options)
  end

  # replace back the temporary placeholders with the html tokens
  transformed_words.each_with_index do |word, index|
    value = value.gsub("{$w#{index}}", word)
  end

  # replace back the temporary placeholders with the html tokens
  html_tokens.each_with_index do |html_token, index|
    value = value.gsub("{$h#{index}}", html_token)
  end

  value
end

#decorate(word, case_value, case_rule, options = {}) ⇒ Object

Decoration Methods - TODO: move to decorators



99
100
101
102
103
104
105
106
# File 'lib/tr8n/language_case.rb', line 99

def decorate(word, case_value, case_rule, options = {})
  return case_value if options[:skip_decorations]
  return case_value if language.default?
  return case_value unless Tr8n.session.current_translator
  return case_value unless Tr8n.session.current_translator.inline?

  "<span class='tr8n_language_case' data-case_id='#{id}' data-rule_id='#{case_rule ? case_rule.id : ''}' data-case_key='#{word.gsub("'", "\'")}'>#{case_value}</span>"
end

#find_matching_rule(value, object = nil) ⇒ Object



40
41
42
43
44
45
# File 'lib/tr8n/language_case.rb', line 40

def find_matching_rule(value, object = nil)
  rules.each do |rule|
    return rule if rule.evaluate(value, object)
  end
  nil
end

#to_cache_hashObject

Cache Methods



112
113
114
115
116
117
118
119
# File 'lib/tr8n/language_case.rb', line 112

def to_cache_hash
  hash = to_hash(:id, :keyword, :description, :latin_name, :native_name, :application)
  hash["rules"] = []
  rules.each do |rule|
    hash["rules"] << rule.to_cache_hash
  end
  hash
end