app/models/tr8n/language_rule.rb in tr8n-3.1.6 vs app/models/tr8n/language_rule.rb in tr8n-3.1.7

- old
+ new

@@ -59,32 +59,10 @@ def self.keyword dependency end - # {"locale"=>"ru", "label"=>"{count} сообщения", "rank"=>1, "rules"=>[ - # {"token"=>"count", "type"=>"number", "definition"=> - # {"multipart"=>true, "part1"=>"ends_in", "value1"=>"2,3,4", "operator"=>"and", "part2"=>"does_not_end_in", "value2"=>"12,13,14"} - # } - # ] - # } - - def self.for_definition(lang, translator, type, definition, opts = {}) - opts[:force_create] ||= false - - rule_class = Tr8n::Config.language_rule_dependencies[type] - return if rule_class == nil # unsupported rule type, skip this completely - - rule_class.for(lang).each do |rule| - return rule if rule.definition == definition - end - - if opts[:force_create] - rule_class.create(:language => lang, :translator => translator, :definition => definition) - end - end - # TDOD: switch to using keyword def self.dependency raise Tr8n::Exception.new("This method must be implemented in the extending rule") end @@ -100,17 +78,10 @@ def self.humanize_values(values) sanitize_values(values).join(", ") end - def to_api_hash - { - :type => self.class.keyword, - :definition => definition - } - end - def evaluate(token_value) raise Tr8n::Exception.new("This method must be implemented in the extending rule") end def description @@ -145,8 +116,39 @@ destroy end def clear_cache Tr8n::Cache.delete("language_rule_#{id}") + end + + ############################################################### + ## Synchronization Methods + ############################################################### + def to_sync_hash(token) + { + "token" => token, + "type" => self.class.keyword, + "definition" => definition + } + end + + # {"locale"=>"ru", "label"=>"{count} сообщения", "rank"=>1, "rules"=>[ + # {"token"=>"count", "type"=>"number", "definition"=> + # {"multipart"=>true, "part1"=>"ends_in", "value1"=>"2,3,4", "operator"=>"and", "part2"=>"does_not_end_in", "value2"=>"12,13,14"} + # } + # ] + # } + + def self.create_from_sync_hash(lang, translator, rule_hash, opts = {}) + return unless rule_hash["token"] and rule_hash["type"] and rule_hash["definition"] + + rule_class = Tr8n::Config.language_rule_dependencies[rule_hash["type"]] + return unless rule_class # unsupported rule type, skip this completely + + rule_class.for(lang).each do |rule| + return rule if rule.definition == rule_hash["definition"] + end + + rule_class.create(:language => lang, :translator => translator, :definition => rule_hash["definition"]) end end