# File lib/express_translate/express_translate_model.rb, line 177 def self.primary_key return {"success" => false, "error" => "Duplicate primary key"} end
class ExpressTranslate::ExpressTranslateModel
Construction
id: xyz ...... ......
Attributes
Public Class Methods
Add item with params Check primary key for present Check unique data Return messages
# File lib/express_translate/express_translate_model.rb, line 48 def self.add(params) if JSON.parse(params.to_json)[@primary] == "" return self.primary_nil end if self.check_unique_allow_add(params) data = self.all.push(params) self.save(data) return self.successful(data) else return self.primary_key end end
get all item in table
# File lib/express_translate/express_translate_model.rb, line 25 def self.all data = Database.get(@name) data = data.nil? ? [] : (data.is_a?(Array) ? data : [].push(data)) self.add_has_many(data) data.sort_by!{|item| item.first} data end
Delete item by id Remove item with primary key
# File lib/express_translate/express_translate_model.rb, line 63 def self.delete(id) all = self.all count_before = all.count all.reject!{|package| package[@primary] == id} return self.change_data(count_before, all.count, all) end
Clear all data of table
# File lib/express_translate/express_translate_model.rb, line 40 def self.destroy Database.del(@name) end
Find item by code Select item with primary key return limit items selected
# File lib/express_translate/express_translate_model.rb, line 86 def self.find(code) data = self.all search = data.select{|package| package[@primary] == code} if (search.count > 0) self.add_has_many_item(search[0]) else search = [nil] end return search[0] end
Save obj to redis database Synchronize data to redis database
# File lib/express_translate/express_translate_model.rb, line 35 def self.save(obj) Database.set(@name, self.protect_attr_items(obj)) end
Update item by params Remove old item Add new item with exsits data Return json data for status
# File lib/express_translate/express_translate_model.rb, line 74 def self.update(params) all = self.all count_before = all.count all.reject!{|package| package[@primary] == params[@primary]} count_after = all.count all.push(params) if count_before > count_after return self.change_data(count_before, count_after, all) end
Private Class Methods
Add relationship for model for item list
# File lib/express_translate/express_translate_model.rb, line 141 def self.add_has_many(items) items.each do |item| self.add_has_many_item(item) end end
Add relationship for model only one item
# File lib/express_translate/express_translate_model.rb, line 148 def self.add_has_many_item(item) if @has_many.present? @has_many = @has_many.is_a?(Array) ? @has_many : [@has_many] @has_many.each do |model_many_item| if model_many_item.present? item[model_many_item.downcase] = ExpressTranslate.const_get(model_many_item).all.select{|i| i[@name] == item[@primary]} end end end end
Messages for change data
# File lib/express_translate/express_translate_model.rb, line 162 def self.change_data(before, after, data) if before > after self.save(data) return self.successful(data) else return self.notfound end end
Check duplicate primary key or list unique
# File lib/express_translate/express_translate_model.rb, line 100 def self.check_unique_allow_add(item) if @unique.present? @unique = @unique.is_a?(Array) ? @unique : [@unique] check_unique = false @unique.each do |unique| select_check = self.all.select{|i| (i[unique] == item[unique]) and i[@primary] == item[@primary]} check_unique = true if select_check.count == 0 end return check_unique else return self.find(item[@primary]).nil? end end
Messages for not found data
# File lib/express_translate/express_translate_model.rb, line 172 def self.notfound return {"success"=> false, "error"=> "Data is not found!"} end
Messages for duplicate primary key
Messages for primary key is nil
# File lib/express_translate/express_translate_model.rb, line 182 def self.primary_nil return {"success" => false, "error" => "Primary data is nil!"} end
Check attributes for allow store only one item
# File lib/express_translate/express_translate_model.rb, line 124 def self.protect_attr(item) if item.to_json == "" return nil end item = JSON.parse(item.to_json) item_copy = {} item_copy[@primary] = item[@primary] if @attr.present? @attr = @attr.is_a?(Array) ? @attr : [@attr] @attr.each do |attr| item_copy[attr] = item[attr] end end return item_copy end
Check attributes for allow store for list items
# File lib/express_translate/express_translate_model.rb, line 115 def self.protect_attr_items(items) items_copy = [] items.each do |item| items_copy.push(self.protect_attr(item)) if item.present? end return items_copy end
Messages for successful
# File lib/express_translate/express_translate_model.rb, line 187 def self.successful(data) return {"success" => true, "#{@name}" => data} end