Class: Proxima::Model

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming, ActiveModel::Translation
Includes:
ActiveModel::AttributeMethods, ActiveModel::Conversion, ActiveModel::Dirty, ActiveModel::Model, ActiveModel::Serializers::JSON, ActiveModel::Validations, Attributes, Paths, Serialization, Validation
Defined in:
lib/proxima/model.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Validation

errors, included, #read_attribute_for_validation

Methods included from Serialization

#as_json, #from_json, included, #to_h

Methods included from Paths

included

Methods included from Attributes

#attributes, #attributes=, included

Constructor Details

#initialize(record = {}) ⇒ Model

Returns a new instance of Model.



89
90
91
92
# File 'lib/proxima/model.rb', line 89

def initialize(record = {})
  self.new_record = true
  self.attributes = record
end

Class Method Details

.api(api = nil) ⇒ Object

TODO: Implement callbacks extend ActiveModel::Callbacks define_model_callbacks :create, :update



30
31
32
33
# File 'lib/proxima/model.rb', line 30

def self.api(api = nil)
  @api = api if api
  @api
end

.count(query = {}, opts = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/proxima/model.rb', line 62

def self.count(query = {}, opts = {})
  query['$limit'] = 0
  opts[:query]    = self.convert_query_or_delta_to_json query
  response        = self.api.get self.find_path.call(query), opts

  return nil unless response.code == 200

  response.headers[:x_total_count] || 0
end

.create(record) ⇒ Object



35
36
37
38
39
40
# File 'lib/proxima/model.rb', line 35

def self.create(record)
  return record.map { |record| self.create record } if record.is_a? Array
  model = self.new(record)
  return nil unless model.save
  model
end

.find(query = {}, opts = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/proxima/model.rb', line 42

def self.find(query = {}, opts = {})
  opts[:query] = self.convert_query_or_delta_to_json query
  response     = self.api.get self.find_path.call(query), opts

  return [] unless response.code == 200

  records = ActiveSupport::JSON.decode response.body
  records.map do |record|
    model = self.new
    model.from_json record
    model.new_record = false
    model
  end
end

.find_by_id(id, query = {}, opts = {}) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/proxima/model.rb', line 72

def self.find_by_id(id, query = {}, opts = {})
  if opts.empty? && query
    opts  = query
    query = {}
  end

  query[:id] = id
  response = self.api.get self.find_by_id_path.call(query), opts

  return nil unless response.code == 200

  model = self.new
  model.from_json response.body
  model.new_record = false
  model
end

.find_one(query = {}, opts = {}) ⇒ Object



57
58
59
60
# File 'lib/proxima/model.rb', line 57

def self.find_one(query = {}, opts = {})
  query['$limit'] = 1;
  self.find(query, opts)[0]
end

Instance Method Details

#destroyObject



147
148
149
150
151
152
153
154
# File 'lib/proxima/model.rb', line 147

def destroy
  return false if new_record?

  response = self.class.api.delete(self.class.delete_by_id_path.call(self.to_h))

  return false unless response.code == 204
  self.persisted = true
end

#new_record=(val) ⇒ Object



107
108
109
110
111
# File 'lib/proxima/model.rb', line 107

def new_record=(val)
  @new_record = !!val
  @persisted  = !val
  clear_changes_information unless val
end

#new_record?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/proxima/model.rb', line 103

def new_record?
  @new_record
end

#persisted=(val) ⇒ Object



98
99
100
101
# File 'lib/proxima/model.rb', line 98

def persisted=(val)
  @persisted = !!val
  changes_applied if val
end

#persisted?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/proxima/model.rb', line 94

def persisted?
  @persisted
end

#reload!Object



139
140
141
# File 'lib/proxima/model.rb', line 139

def reload!
  self.clear_changes_information
end

#restoreObject



156
157
158
159
160
161
162
163
# File 'lib/proxima/model.rb', line 156

def restore
  return false if new_record?

  response = self.class.api.post(self.class.restore_by_id_path.call(self.to_h))

  return false unless response.code == 204
  self.persisted = true
end

#rollback!Object



143
144
145
# File 'lib/proxima/model.rb', line 143

def rollback!
  self.restore_attributes
end

#save(options = {}) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/proxima/model.rb', line 113

def save(options = {})
  return false unless self.valid?

  if self.new_record?
    path     = self.class.create_path.call self.to_h
    payload  = { json: self.as_json(options) }
    response = self.class.api.post path, payload

    return false unless response.code == 201

    self.from_json response.body, options[:include_root]
    self.new_record = false
    return true
  end

  return true if self.persisted?

  options[:flatten] = true if options[:flatten] == nil
  path     = self.class.update_by_id_path.call self.to_h
  payload  = { json: self.as_json(options) }
  response = self.class.api.put path, payload

  return false unless response.code == 204
  self.persisted = true
end