Class: Tr8n::NumericRule

Inherits:
LanguageRule show all
Defined in:
app/models/tr8n/numeric_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::NumericRule 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)

++

Direct Known Subclasses

RussianNumericRule

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from LanguageRule

by_id, #clear_cache, create_from_sync_hash, #definition, dependant?, dependency_label, #destroy_with_log!, for, keyword, options, #save_with_log!, #to_sync_hash, #token_description, transformable?

Class Method Details

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



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

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

.default_transform(*args) ⇒ Object

params: [singular form, plural form]



107
108
109
110
111
112
113
114
# File 'app/models/tr8n/numeric_rule.rb', line 107

def self.default_transform(*args)
  unless [1, 2].include?(args.size)
    raise Tr8n::Exception.new("Invalid transform arguments for number token")
  end
  
  return args[1] if args.size == 2
  args[0].pluralize
end

.dependencyObject



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

def self.dependency
  "number" 
end

.descriptionObject



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

def self.description
  "token object may be a number, which"
end

.evaluate_rule_fragment(token_value, name, values) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/models/tr8n/numeric_rule.rb', line 116

def self.evaluate_rule_fragment(token_value, name, values)
  if name == :is
    return true if values.include?(token_value)
    return false
  end
  
  if name == :is_not
    return true unless values.include?(token_value)
    return false
  end

  if name == :ends_in
    values.each do |value|
      return true if token_value.to_s =~ /#{value.to_s}$/  
    end
    return false
  end

  if name == :does_not_end_in
    values.each do |value|
      return false if token_value.to_s =~ /#{value.to_s}$/  
    end
    return true
  end
  
  false
end

.humanize_values(values) ⇒ Object



79
80
81
# File 'app/models/tr8n/numeric_rule.rb', line 79

def self.humanize_values(values)
  sanitize_values(values).join(", ")
end

.number_token_value(token) ⇒ Object



69
70
71
72
# File 'app/models/tr8n/numeric_rule.rb', line 69

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

.operator_optionsObject



65
66
67
# File 'app/models/tr8n/numeric_rule.rb', line 65

def self.operator_options
  ["and", "or"]
end

.rule_optionsObject



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

def self.rule_options
  [["is", "is"], ["is not", "is_not"], ["ends in", "ends_in"], ["does not end in", "does_not_end_in"]]
end

.sanitize_values(values) ⇒ Object



74
75
76
77
# File 'app/models/tr8n/numeric_rule.rb', line 74

def self.sanitize_values(values)
  return [] unless values
  values.split(",").collect{|val| val.strip} 
end

.suffixesObject



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

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

.transform(*args) ⇒ Object

FORM: [object, singular, plural] | message | person, people



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/models/tr8n/numeric_rule.rb', line 86

def self.transform(*args)
  unless [2, 3].include?(args.size)
    raise Tr8n::Exception.new("Invalid transform arguments for number token")
  end
  
  object = args[0]
  object_value = number_token_value(object)
  unless object_value
    raise Tr8n::Exception.new("Token #{object.class.name} does not respond to #{Tr8n::Config.rules_engine[:numeric_rule][:object_method]}")
  end
  
  if object_value == 1
    return args[1]
  elsif args.size == 2
    return args[1].pluralize
  end
  
  args[2]
end

Instance Method Details

#describe_partial_rule(name, value) ⇒ Object



188
189
190
191
192
193
194
195
# File 'app/models/tr8n/numeric_rule.rb', line 188

def describe_partial_rule(name, value)
  return "is #{humanize_values(value)}" if name == :is
  return "is not #{humanize_values(value)}" if name == :is_not
  return "ends in #{humanize_values(value)}" if name == :ends_in
  return "does not end in #{humanize_values(value)}" if name == :does_not_end_in

  "has an unknown rule"
end

#descriptionObject

used to describe a context of a given translation



179
180
181
182
183
184
185
186
# File 'app/models/tr8n/numeric_rule.rb', line 179

def description
  rule_desc = describe_partial_rule(definition[:part1].to_sym, definition[:value1])
  return rule_desc unless definition[:multipart].to_s == "true"
  
  rule_desc << " " << definition[:operator] << " " 
  rule_desc << describe_partial_rule(definition[:part2].to_sym, definition[:value2])
  humanize_description(rule_desc)   
end

#evaluate(token) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'app/models/tr8n/numeric_rule.rb', line 144

def evaluate(token)
  token_value = number_token_value(token)  
  return false unless token_value
  
  result1 = self.class.evaluate_rule_fragment(token_value.to_s, definition[:part1].to_sym, sanitize_values(definition[:value1]))
  return result1 unless definition[:multipart].to_s == "true"
  
  result2 = self.class.evaluate_rule_fragment(token_value.to_s, definition[:part2].to_sym, sanitize_values(definition[:value2]))
  return (result1 or result2) if definition[:operator] == "or"
  return (result1 and result2)
  
  false
end

#humanize_description(desc) ⇒ Object



197
198
199
# File 'app/models/tr8n/numeric_rule.rb', line 197

def humanize_description(desc)
  desc.gsub(" and does not end in", ", but not in")
end

#humanize_values(values) ⇒ Object



166
167
168
# File 'app/models/tr8n/numeric_rule.rb', line 166

def humanize_values(values)
  self.class.humanize_values(values)
end

#number_token_value(token) ⇒ Object



158
159
160
# File 'app/models/tr8n/numeric_rule.rb', line 158

def number_token_value(token)
  self.class.number_token_value(token)
end

#sanitize_values(values) ⇒ Object



162
163
164
# File 'app/models/tr8n/numeric_rule.rb', line 162

def sanitize_values(values)
  self.class.sanitize_values(values)
end

#to_hashObject



170
171
172
173
174
175
176
# File 'app/models/tr8n/numeric_rule.rb', line 170

def to_hash
  { :type => self.class.dependency, 
    :multipart => definition[:multipart], :operator => definition[:operator],  
    :part1 => definition[:part1], :value1 => definition[:value1],
    :part2 => definition[:part2], :value2 => definition[:value2]
  }
end