Class: Tr8n::ValueRule

Inherits:
LanguageRule show all
Defined in:
app/models/tr8n/value_rule.rb

Overview

– Copyright © 2010-2012 Michael Berkovich, tr8n.net

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. ++

– Tr8n::ValueRule Schema Information

Table name: tr8n_language_rules

id               INTEGER         not null, primary key
language_id      integer         not null
translator_id    integer         
type             varchar(255)    
definition       text            
created_at       datetime        
updated_at       datetime

Indexes

index_tr8n_language_rules_on_language_id_and_translator_id    (language_id, translator_id) 
index_tr8n_language_rules_on_language_id                      (language_id)

++

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from LanguageRule

by_id, #clear_cache, create_from_sync_hash, #definition, dependency_label, #destroy_with_log!, for, humanize_values, keyword, options, sanitize_values, #save_with_log!, #to_sync_hash, #token_description

Class Method Details

.default_rules_for(language = Tr8n::Config.current_language) ⇒ Object



61
62
63
# File 'app/models/tr8n/value_rule.rb', line 61

def self.default_rules_for(language = Tr8n::Config.current_language)
  Tr8n::Config.default_value_rules(language.locale)
end

.dependant?(token) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'app/models/tr8n/value_rule.rb', line 53

def self.dependant?(token)
  token.name != Tr8n::Config.rules_engine[:viewing_user_token]
end

.dependencyObject



49
50
51
# File 'app/models/tr8n/value_rule.rb', line 49

def self.dependency
  "value" 
end

.descriptionObject



45
46
47
# File 'app/models/tr8n/value_rule.rb', line 45

def self.description
  "token object may have a value, which"
end

.operator_optionsObject



65
66
67
68
69
# File 'app/models/tr8n/value_rule.rb', line 65

def self.operator_options
  [["starts with", "starts_with"], ["does not start with", "does_not_start_with"], 
   ["ends in", "ends_in"], ["does not end in", "does_not_end_in"],
   ["is", "is"], ["is not", "is_not"]]
end

.suffixesObject



57
58
59
# File 'app/models/tr8n/value_rule.rb', line 57

def self.suffixes
  Tr8n::Config.rules_engine[:value_rule][:token_suffixes]
end

.token_value(token) ⇒ Object



75
76
77
78
# File 'app/models/tr8n/value_rule.rb', line 75

def self.token_value(token)
  return nil unless token and token.respond_to?(Tr8n::Config.rules_engine[:value_rule][:object_method])
  token.send(Tr8n::Config.rules_engine[:value_rule][:object_method])
end

.transformable?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'app/models/tr8n/value_rule.rb', line 71

def self.transformable?
  false
end

Instance Method Details

#descriptionObject

used to describe a context of a given translation



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/models/tr8n/value_rule.rb', line 122

def description
  desc = ""
  case definition["operator"]
    when "starts_with" then desc << " starts with"
    when "does_not_start_with" then desc << " does not start with"        
    when "ends_in" then desc << " ends in"        
    when "does_not_end_in" then desc << " does not end in"        
    when "is" then desc << " is"        
    when "is_not" then desc << " is not"        
  end
  desc << " <strong>'" << Tr8n::LanguageRule.humanize_values(definition["value"]) << "'</strong>"
  desc.html_safe
end

#evaluate(token) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/models/tr8n/value_rule.rb', line 80

def evaluate(token)
  token_value = self.class.token_value(token)
  return false unless token_value

  token_value = token_value.gsub(/<\/?[^>]*>/, "")
  values = Tr8n::LanguageRule.sanitize_values(definition["value"])
  
  case definition["operator"]
    when "starts_with" 
      values.each do |value|
        return true if token_value.to_s =~ /^#{value.to_s}/  
      end
      return false
    when "does_not_start_with"         
      values.each do |value|
        return false if token_value.to_s =~ /^#{value.to_s}/  
      end
      return true
    when "ends_in"
      values.each do |value|
        return true if token_value.to_s =~ /#{value.to_s}$/  
      end
      return false
    when "does_not_end_in"         
      values.each do |value|
        return false if token_value.to_s =~ /#{value.to_s}$/  
      end
      return true
    when "is"         
      return values.include?(token_value)
    when "is_not"        
      return !values.include?(token_value)
  end
  
  false
end

#to_hashObject



117
118
119
# File 'app/models/tr8n/value_rule.rb', line 117

def to_hash
  {:type => self.class.dependency, :operator => definition[:operator], :value => definition[:value]}
end