Class: KakaxiParse::BaseModel

Inherits:
Object
  • Object
show all
Defined in:
lib/kakaxi_parse/base_model.rb

Constant Summary collapse

DEFUALT_LIMIT =
1000

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, name, updated_at, created_at) ⇒ BaseModel

Returns a new instance of BaseModel.



8
9
10
11
12
13
# File 'lib/kakaxi_parse/base_model.rb', line 8

def initialize(id, name, updated_at, created_at)
   @id = id
   @name = name if name
   @updated_at = DateTime.strptime(updated_at, '%Y-%m-%dT%H:%M:%S.%LZ') if updated_at
   @created_at =  DateTime.strptime(created_at, '%Y-%m-%dT%H:%M:%S.%LZ') if created_at
end

Instance Attribute Details

#created_atObject

Returns the value of attribute created_at.



6
7
8
# File 'lib/kakaxi_parse/base_model.rb', line 6

def created_at
  @created_at
end

#idObject

Returns the value of attribute id.



6
7
8
# File 'lib/kakaxi_parse/base_model.rb', line 6

def id
  @id
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/kakaxi_parse/base_model.rb', line 6

def name
  @name
end

#updated_atObject

Returns the value of attribute updated_at.



6
7
8
# File 'lib/kakaxi_parse/base_model.rb', line 6

def updated_at
  @updated_at
end

Class Method Details

.all(limit: DEFUALT_LIMIT) ⇒ Object



15
16
17
# File 'lib/kakaxi_parse/base_model.rb', line 15

def self.all(limit: DEFUALT_LIMIT)
  to_objects(API.new("classes/#{ self.demodulize }", params: { limit: limit }).get['results'])
end

.demodulizeObject



62
63
64
# File 'lib/kakaxi_parse/base_model.rb', line 62

def self.demodulize
  self.name.split('::').last
end

.find(id) ⇒ Object



33
34
35
# File 'lib/kakaxi_parse/base_model.rb', line 33

def self.find(id)
  where(id: id).first
end

.order(order = 'created_at', limit = DEFUALT_LIMIT) ⇒ Object



24
25
26
27
# File 'lib/kakaxi_parse/base_model.rb', line 24

def self.order(order='created_at', limit=DEFUALT_LIMIT)
  params = { limit: limit }.merge(order: camelize(order))
  to_objects(API.new("classes/#{ self.demodulize }", params: params).get['results'])
end

.short_nameObject

Raises:

  • (NotImplementedError)


29
30
31
# File 'lib/kakaxi_parse/base_model.rb', line 29

def self.short_name
  raise NotImplementedError
end

.where(model, hash, limit, belongs_to = [], related_to = []) ⇒ Object



19
20
21
22
# File 'lib/kakaxi_parse/base_model.rb', line 19

def self.where(model, hash, limit, belongs_to = [], related_to = [])
  params = { limit: limit }.merge(where: format(hash, belongs_to, related_to).to_json)
  API.new("classes/#{ model.demodulize }", params: params).get['results']
end

Instance Method Details

#deleteObject



58
59
60
# File 'lib/kakaxi_parse/base_model.rb', line 58

def delete
  API.new("classes/#{self.class.demodulize}/#{self.id}").delete
end

#save(record, params, belongs_to = [], related_to = []) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/kakaxi_parse/base_model.rb', line 37

def save(record, params, belongs_to = [], related_to = [])
  result = API.new("classes/#{record.class.demodulize}", params: BaseModel.format(params, belongs_to, related_to)).post
  if result.has_key? 'objectId'
    self.created_at = DateTime.strptime(result['createdAt'], '%Y-%m-%dT%H:%M:%S.%LZ')
    self.id = result['objectId']
    true
  else
    false
  end
end

#update(record, params, belongs_to = [], related_to = []) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/kakaxi_parse/base_model.rb', line 48

def update(record, params, belongs_to = [], related_to = [])
  result = API.new("classes/#{record.class.demodulize}/#{record.id}", params: BaseModel.format(params, belongs_to, related_to)).put
  if result.has_key? 'updatedAt'
    self.updated_at = DateTime.strptime(result['updatedAt'], '%Y-%m-%dT%H:%M:%S.%LZ')
    true
  else
    false
  end
end