Module: Findable::Recordable
- Extended by:
- ActiveSupport::Concern
- Includes:
- Serializer
- Included in:
- Base
- Defined in:
- lib/findable/recordable.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
- #attributes ⇒ Object
- #delete ⇒ Object (also: #destroy)
- #hash ⇒ Object
- #id ⇒ Object
-
#id=(_id) ⇒ Object
alias_method :quoted_id, :id.
- #initialize(params = {}) ⇒ Object
- #new_record? ⇒ Boolean
- #persisted? ⇒ Boolean
- #save ⇒ Object (also: #save!)
- #to_json(methods: nil) ⇒ Object
Methods included from Serializer
Instance Method Details
#attributes ⇒ Object
79 80 81 |
# File 'lib/findable/recordable.rb', line 79 def attributes @_attributes ||= {} end |
#delete ⇒ Object Also known as: destroy
56 57 58 |
# File 'lib/findable/recordable.rb', line 56 def delete self.class.delete(id) end |
#hash ⇒ Object
75 76 77 |
# File 'lib/findable/recordable.rb', line 75 def hash id.hash end |
#id ⇒ Object
42 43 44 |
# File 'lib/findable/recordable.rb', line 42 def id attributes[:id].presence || nil end |
#id=(_id) ⇒ Object
alias_method :quoted_id, :id
47 48 49 |
# File 'lib/findable/recordable.rb', line 47 def id=(_id) attributes[:id] = _id end |
#initialize(params = {}) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/findable/recordable.rb', line 35 def initialize(params={}) params = deserialize(params) if params.is_a?(String) params.symbolize_keys! params.keys.each {|attr| self.class.field(attr) } @_attributes = params end |
#new_record? ⇒ Boolean
61 62 63 |
# File 'lib/findable/recordable.rb', line 61 def new_record? id ? !self.class.exists?(self) : true end |
#persisted? ⇒ Boolean
65 66 67 |
# File 'lib/findable/recordable.rb', line 65 def persisted? !new_record? end |
#save ⇒ Object Also known as: save!
51 52 53 |
# File 'lib/findable/recordable.rb', line 51 def save self.class.insert(self) end |
#to_json(methods: nil) ⇒ Object
69 70 71 72 73 |
# File 'lib/findable/recordable.rb', line 69 def to_json(methods: nil) _attrs = attributes.dup _attrs.merge!(methods.to_sym => self.send(methods)) if methods serialize(_attrs) end |