Class: Tr8n::ListRule

Inherits:
LanguageRule show all
Defined in:
app/models/tr8n/list_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::ListRule 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, dependant?, dependency_label, #destroy_with_log!, for, humanize_values, keyword, options, sanitize_values, #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/list_rule.rb', line 57

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

.default_transform(*args) ⇒ Object

params: [one element, at least two elements]



102
103
104
105
106
107
108
# File 'app/models/tr8n/list_rule.rb', line 102

def self.default_transform(*args)
  unless args.size == 2
    raise Tr8n::Exception.new("Invalid transform arguments for list token")
  end
  
  args[1]
end

.dependencyObject



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

def self.dependency
  "list" 
end

.descriptionObject



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

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

.list_optionsObject



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

def self.list_options
  [["one element", "one_element"], ["at least 2 elements", "at_least_two_elements"]]
end

.list_size_token_value(token) ⇒ Object



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

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

.operator_optionsObject



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

def self.operator_options
  [["contains", "contains"]]
end

.suffixesObject



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

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

.transform(*args) ⇒ Object

params: [object, one element, at least two elements] | one element, at least two elements



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/models/tr8n/list_rule.rb', line 80

def self.transform(*args)
  unless args.size == 3
    raise Tr8n::Exception.new("Invalid transform arguments")
  end
  
  object = args[0]
  list_size = list_size_token_value(object)

  unless list_size
    raise Tr8n::Exception.new("Token #{object.class.name} does not respond to #{Tr8n::Config.rules_engine[:gender_list_rule][:object_method]}")
  end
  
  list_size = list_size.to_i
  
  return args[1] if list_size == 1
  return args[2] if list_size >= 2
  
  # should we raise an exception here if the list is empty?
  ""  
end

Instance Method Details

#descriptionObject

used to describe a context of a given translation



132
133
134
135
136
# File 'app/models/tr8n/list_rule.rb', line 132

def description
  return "contains one element"              if "one_element" == definition[:value]
  return "contains at least two elements"    if "at_least_two_elements" == definition[:value]
  "has an unknown rule"
end

#evaluate(token) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'app/models/tr8n/list_rule.rb', line 110

def evaluate(token)
  return false unless token.kind_of?(Enumerable)
  
  list_size = list_size_token_value(token)
  return false if list_size == nil
  list_size = list_size.to_i

  case definition[:value]
    when "one_element" then
      return true if list_size == 1
    when "at_least_two_elements" then
      return true if list_size >= 2
  end
  
  false
end

#list_size_token_value(token) ⇒ Object



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

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

#to_hashObject



127
128
129
# File 'app/models/tr8n/list_rule.rb', line 127

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