Class: ExpressTranslate::ExpressTranslateModel

Inherits:
Object
  • Object
show all
Defined in:
lib/express_translate/express_translate_model.rb

Overview

Construction

id: xyz
......
......

Direct Known Subclasses

Account, Language, LanguageDetail, Package

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.attrObject

Returns the value of attribute attr.



9
10
11
# File 'lib/express_translate/express_translate_model.rb', line 9

def attr
  @attr
end

.has_manyObject

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

.nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/express_translate/express_translate_model.rb', line 9

def name
  @name
end

.primaryObject

Returns the value of attribute primary.



9
10
11
# File 'lib/express_translate/express_translate_model.rb', line 9

def primary
  @primary
end

.uniqueObject

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

.allObject

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

.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

.destroyObject

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

.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

.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