Class: Kvom::Model::Base
- Inherits:
-
Object
- Object
- Kvom::Model::Base
- Extended by:
- ActiveModel::Naming
- Includes:
- Kvom::ModelIdentity
- Defined in:
- lib/kvom/model/base.rb
Class Method Summary collapse
- .adapter ⇒ Object
- .create(attributes = {}) ⇒ Object
- .find(id) ⇒ Object
- .find_by_id(id) ⇒ Object
- .has_all_ids ⇒ Object
- .id_key_for(id) ⇒ Object
-
.key_prefix ⇒ Object
default implementation for the class_attribute.
- .property(name_raw) ⇒ Object
Instance Method Summary collapse
- #destroy ⇒ Object
- #document ⇒ Object
- #id ⇒ Object
-
#initialize(doc_or_attrs = {}) ⇒ Base
constructor
Currently, the caller is responsible to not leak non-property attributes into the (persisted!) model by using the mass assignment available by new().
- #new? ⇒ Boolean
- #save ⇒ Object
Methods included from Kvom::ModelIdentity
Constructor Details
#initialize(doc_or_attrs = {}) ⇒ Base
Currently, the caller is responsible to not leak non-property attributes into the (persisted!) model by using the mass assignment available by new(). A filter would require to remember the properties (through a class inheritable attribute)
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/kvom/model/base.rb', line 79 def initialize(doc_or_attrs = {}) @document = case doc_or_attrs when ::Kvom::Adapter::Document doc_or_attrs else @new = true attrs = doc_or_attrs.stringify_keys key = self.class.id_key_for(attrs["id"] ||= SecureRandom.hex(8)) self.class.adapter.new_document(key, attrs) end end |
Class Method Details
.adapter ⇒ Object
55 56 57 |
# File 'lib/kvom/model/base.rb', line 55 def adapter raise "must be overwritten in subclasses" end |
.create(attributes = {}) ⇒ Object
24 25 26 |
# File 'lib/kvom/model/base.rb', line 24 def create(attributes = {}) new(attributes).tap { |model| model.save } end |
.find(id) ⇒ Object
28 29 30 31 |
# File 'lib/kvom/model/base.rb', line 28 def find(id) raise "no id given" if id.blank? new(find_document(id)) end |
.find_by_id(id) ⇒ Object
33 34 35 36 37 |
# File 'lib/kvom/model/base.rb', line 33 def find_by_id(id) find(id) rescue Kvom::NotFound nil end |
.has_all_ids ⇒ Object
20 21 22 |
# File 'lib/kvom/model/base.rb', line 20 def has_all_ids include Feature::AllIds end |
.id_key_for(id) ⇒ Object
59 60 61 |
# File 'lib/kvom/model/base.rb', line 59 def id_key_for(id) key_for("id/#{id}", nil) end |
.key_prefix ⇒ Object
default implementation for the class_attribute
51 52 53 |
# File 'lib/kvom/model/base.rb', line 51 def key_prefix to_s end |
.property(name_raw) ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/kvom/model/base.rb', line 39 def property(name_raw) name = name_raw.to_s define_method name do read_attribute(name) end define_method "#{name}=" do |value| write_attribute(name, value) end end |
Instance Method Details
#destroy ⇒ Object
110 111 112 113 |
# File 'lib/kvom/model/base.rb', line 110 def destroy self.class.adapter.destroy_index(all_key) if is_a?(Feature::AllIds) self.class.adapter.destroy(@document) end |
#document ⇒ Object
92 93 94 |
# File 'lib/kvom/model/base.rb', line 92 def document @document end |
#id ⇒ Object
102 103 104 |
# File 'lib/kvom/model/base.rb', line 102 def id @document["id"] end |
#new? ⇒ Boolean
106 107 108 |
# File 'lib/kvom/model/base.rb', line 106 def new? @new end |