Class: Kvom::Model::Base

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
Kvom::ModelIdentity
Defined in:
lib/kvom/model/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Kvom::ModelIdentity

#==, #eql?, #hash

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

.adapterObject



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_idsObject



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_prefixObject

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

#destroyObject



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

#documentObject



92
93
94
# File 'lib/kvom/model/base.rb', line 92

def document
  @document
end

#idObject



102
103
104
# File 'lib/kvom/model/base.rb', line 102

def id
  @document["id"]
end

#new?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/kvom/model/base.rb', line 106

def new?
  @new
end

#saveObject



96
97
98
99
100
# File 'lib/kvom/model/base.rb', line 96

def save
  self.class.adapter.save(@document)
  self.class.adapter.save_index(all_key) if @new && is_a?(Feature::AllIds)
  @new = false
end