Class: Sbmt::Outbox::Api::ApplicationRecord

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Attributes, ActiveModel::Model
Defined in:
app/models/sbmt/outbox/api/application_record.rb

Direct Known Subclasses

BoxClass

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ ApplicationRecord

Returns a new instance of ApplicationRecord.



48
49
50
51
# File 'app/models/sbmt/outbox/api/application_record.rb', line 48

def initialize(params)
  super
  assign_attributes(params)
end

Class Method Details

.attribute(name, type = ActiveModel::Type::Value.new, **options) ⇒ Object



37
38
39
40
41
# File 'app/models/sbmt/outbox/api/application_record.rb', line 37

def attribute(name, type = ActiveModel::Type::Value.new, **options)
  super
  # Add predicate methods for boolean types
  alias_method :"#{name}?", name if type == :boolean || type.is_a?(ActiveModel::Type::Boolean)
end

.attributes(*attrs) ⇒ Object



31
32
33
34
35
# File 'app/models/sbmt/outbox/api/application_record.rb', line 31

def attributes(*attrs)
  attrs.each do |name|
    attribute name
  end
end

.delete(id) ⇒ Object



27
28
29
# File 'app/models/sbmt/outbox/api/application_record.rb', line 27

def delete(id)
  redis.call "DEL", redis_key(id)
end

.find(id) ⇒ Object



15
16
17
18
19
20
# File 'app/models/sbmt/outbox/api/application_record.rb', line 15

def find(id)
  attributes = redis.call "HGETALL", redis_key(id)
  return nil if attributes.empty?

  new(attributes)
end

.find_or_initialize(id, params = {}) ⇒ Object



22
23
24
25
# File 'app/models/sbmt/outbox/api/application_record.rb', line 22

def find_or_initialize(id, params = {})
  record = find(id)
  record || new(params.merge(id: id))
end

.redis_key(id) ⇒ Object



43
44
45
# File 'app/models/sbmt/outbox/api/application_record.rb', line 43

def redis_key(id)
  "#{name}:#{id}"
end

Instance Method Details

#as_jsonObject



61
62
63
# File 'app/models/sbmt/outbox/api/application_record.rb', line 61

def as_json(*)
  attributes
end

#destroyObject



57
58
59
# File 'app/models/sbmt/outbox/api/application_record.rb', line 57

def destroy
  self.class.delete(id)
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


65
66
67
68
# File 'app/models/sbmt/outbox/api/application_record.rb', line 65

def eql?(other)
  return false unless other.is_a?(self.class)
  id == other.id
end

#saveObject



53
54
55
# File 'app/models/sbmt/outbox/api/application_record.rb', line 53

def save
  redis.call "HMSET", redis_key, attributes.to_a.flatten.map(&:to_s)
end