Class: Sbmt::Outbox::Api::ApplicationRecord
- Inherits:
-
Object
- Object
- Sbmt::Outbox::Api::ApplicationRecord
- Includes:
- ActiveModel::Attributes, ActiveModel::Model
- Defined in:
- app/models/sbmt/outbox/api/application_record.rb
Direct Known Subclasses
Class Method Summary collapse
- .attribute(name, type = ActiveModel::Type::Value.new, **options) ⇒ Object
- .attributes(*attrs) ⇒ Object
- .delete(id) ⇒ Object
- .find(id) ⇒ Object
- .find_or_initialize(id, params = {}) ⇒ Object
- .redis_key(id) ⇒ Object
Instance Method Summary collapse
- #as_json ⇒ Object
- #destroy ⇒ Object
- #eql?(other) ⇒ Boolean (also: #==)
-
#initialize(params) ⇒ ApplicationRecord
constructor
A new instance of ApplicationRecord.
- #save ⇒ Object
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, **) 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_json ⇒ Object
61 62 63 |
# File 'app/models/sbmt/outbox/api/application_record.rb', line 61 def as_json(*) attributes end |
#destroy ⇒ Object
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: ==
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 |
#save ⇒ Object
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 |