Class: ExpressTranslate::ExpressTranslateModel
- Inherits:
-
Object
- Object
- ExpressTranslate::ExpressTranslateModel
- Defined in:
- lib/express_translate/express_translate_model.rb
Overview
Construction
id: xyz
......
......
Direct Known Subclasses
Class Attribute Summary collapse
-
.attr ⇒ Object
Returns the value of attribute attr.
-
.has_many ⇒ Object
Returns the value of attribute has_many.
-
.name ⇒ Object
Returns the value of attribute name.
-
.primary ⇒ Object
Returns the value of attribute primary.
-
.unique ⇒ Object
Returns the value of attribute unique.
Class Method Summary collapse
-
.add(params) ⇒ Object
Add item with params Check primary key for present Check unique data Return messages.
-
.add_has_many(items) ⇒ Object
Add relationship for model for item list.
-
.add_has_many_item(item) ⇒ Object
Add relationship for model only one item.
-
.all ⇒ Object
get all item in table.
-
.change_data(before, after, data) ⇒ Object
Messages for change data.
-
.check_unique_allow_add(item) ⇒ Object
Check duplicate primary key or list unique.
-
.delete(id) ⇒ Object
Delete item by id Remove item with primary key.
-
.destroy ⇒ Object
Clear all data of table.
-
.find(code) ⇒ Object
Find item by code Select item with primary key return limit items selected.
-
.notfound ⇒ Object
Messages for not found data.
-
.primary_key ⇒ Object
Messages for duplicate primary key.
-
.primary_nil ⇒ Object
Messages for primary key is nil.
-
.protect_attr(item) ⇒ Object
Check attributes for allow store only one item.
-
.protect_attr_items(items) ⇒ Object
Check attributes for allow store for list items.
-
.save(obj) ⇒ Object
Save obj to redis database Synchronize data to redis database.
-
.successful(data) ⇒ Object
Messages for successful.
-
.update(params) ⇒ Object
Update item by params Remove old item Add new item with exsits data Return json data for status.
Class Attribute Details
.attr ⇒ Object
Returns the value of attribute attr.
9 10 11 |
# File 'lib/express_translate/express_translate_model.rb', line 9 def attr @attr end |
.has_many ⇒ Object
Returns the value of attribute has_many.
9 10 11 |
# File 'lib/express_translate/express_translate_model.rb', line 9 def has_many @has_many end |
.name ⇒ Object
Returns the value of attribute name.
9 10 11 |
# File 'lib/express_translate/express_translate_model.rb', line 9 def name @name end |
.primary ⇒ Object
Returns the value of attribute primary.
9 10 11 |
# File 'lib/express_translate/express_translate_model.rb', line 9 def primary @primary end |
.unique ⇒ Object
Returns the value of attribute unique.
9 10 11 |
# File 'lib/express_translate/express_translate_model.rb', line 9 def unique @unique end |
Class Method Details
.add(params) ⇒ Object
Add item with params Check primary key for present Check unique data Return messages
48 49 50 51 52 53 54 55 56 57 58 59 |
# 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 |
.add_has_many(items) ⇒ Object
Add relationship for model for item list
141 142 143 144 145 |
# 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_has_many_item(item) ⇒ Object
Add relationship for model only one item
148 149 150 151 152 153 154 155 156 157 |
# 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 |
.all ⇒ Object
get all item in table
25 26 27 28 29 30 31 |
# 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 |
.change_data(before, after, data) ⇒ Object
Messages for change data
162 163 164 165 166 167 168 169 |
# 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_unique_allow_add(item) ⇒ Object
Check duplicate primary key or list unique
100 101 102 103 104 105 106 107 108 109 110 111 112 |
# 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 |
.delete(id) ⇒ Object
Delete item by id Remove item with primary key
63 64 65 66 67 68 |
# 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 |
.destroy ⇒ Object
Clear all data of table
40 41 42 |
# File 'lib/express_translate/express_translate_model.rb', line 40 def self.destroy Database.del(@name) end |
.find(code) ⇒ Object
Find item by code Select item with primary key return limit items selected
86 87 88 89 90 91 92 93 94 95 |
# 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 |
.notfound ⇒ Object
Messages for not found data
172 173 174 |
# File 'lib/express_translate/express_translate_model.rb', line 172 def self.notfound return {"success"=> false, "error"=> "Data is not found!"} end |
.primary_key ⇒ Object
Messages for duplicate primary key
177 178 179 |
# File 'lib/express_translate/express_translate_model.rb', line 177 def self.primary_key return {"success" => false, "error" => "Duplicate primary key"} end |
.primary_nil ⇒ Object
Messages for primary key is nil
182 183 184 |
# File 'lib/express_translate/express_translate_model.rb', line 182 def self.primary_nil return {"success" => false, "error" => "Primary data is nil!"} end |
.protect_attr(item) ⇒ Object
Check attributes for allow store only one item
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# 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 |
.protect_attr_items(items) ⇒ Object
Check attributes for allow store for list items
115 116 117 118 119 120 121 |
# 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 |
.save(obj) ⇒ Object
Save obj to redis database Synchronize data to redis database
35 36 37 |
# File 'lib/express_translate/express_translate_model.rb', line 35 def self.save(obj) Database.set(@name, self.protect_attr_items(obj)) end |
.successful(data) ⇒ Object
Messages for successful
187 188 189 |
# File 'lib/express_translate/express_translate_model.rb', line 187 def self.successful(data) return {"success" => true, "#{@name}" => data} end |
.update(params) ⇒ Object
Update item by params Remove old item Add new item with exsits data Return json data for status
74 75 76 77 78 79 80 81 |
# 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 |