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
# 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
        self.class.new_document(attrs)
      end
end

Class Method Details

.adapterObject



58
59
60
# File 'lib/kvom/model/base.rb', line 58

def adapter
  raise "must be overwritten in subclasses"
end

.all_idsObject



62
63
64
65
66
67
68
69
70
71
# File 'lib/kvom/model/base.rb', line 62

def all_ids
  case
  when kvom_uses_key_prefix_as_hash_value?
    adapter.range_values_of_hash_value(key_prefix)
  when kvom_uses_all_index_document?
    adapter.range_values_of_hash_value("#{key_prefix}/all")
  else
    raise "#{name}.all_ids needs to be enabled via has_all_ids"
  end
end

.create(attributes = {}) ⇒ Object



32
33
34
# File 'lib/kvom/model/base.rb', line 32

def create(attributes = {})
  new(attributes).tap { |model| model.save }
end

.destroy_document(document) ⇒ Object

:nodoc:



145
146
147
148
149
150
# File 'lib/kvom/model/base.rb', line 145

def destroy_document(document)
  if kvom_uses_all_index_document?
    adapter.destroy_index(all_key_for(document))
  end
  adapter.destroy(document)
end

.find(id) ⇒ Object



36
37
38
39
# File 'lib/kvom/model/base.rb', line 36

def find(id)
  raise "no id given" if id.blank?
  new(find_document(id))
end

.find_by_id(id) ⇒ Object



41
42
43
44
45
# File 'lib/kvom/model/base.rb', line 41

def find_by_id(id)
  find(id)
rescue Kvom::NotFound
  nil
end

.has_all_ids(options = {}) ⇒ Object

options:

  • hotspot: true # does not save an additional index document, but creates a hotspot



27
28
29
30
# File 'lib/kvom/model/base.rb', line 27

def has_all_ids(options = {})
  self.kvom_uses_key_prefix_as_hash_value = options[:hotspot]
  self.kvom_uses_all_index_document = !kvom_uses_key_prefix_as_hash_value?
end

.key_prefixObject



21
22
23
# File 'lib/kvom/model/base.rb', line 21

def key_prefix
  to_s
end

.new_document(attrs) ⇒ Object

:nodoc:



128
129
130
131
# File 'lib/kvom/model/base.rb', line 128

def new_document(attrs)
  id = attrs["id"] ||= SecureRandom.hex(8)
  adapter.new_document(id_key_for(id), attrs)
end

.property(name_raw) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/kvom/model/base.rb', line 47

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

.save_document(document, is_new) ⇒ Object

:nodoc:



134
135
136
137
138
139
140
141
142
# File 'lib/kvom/model/base.rb', line 134

def save_document(document, is_new)
  adapter.save(document)
  if is_new
    if kvom_uses_all_index_document?
      adapter.save_index(all_key_for(document))
    end
  end
  true
end

Instance Method Details

#destroyObject



108
109
110
# File 'lib/kvom/model/base.rb', line 108

def destroy
  self.class.destroy_document(@document)
end

#documentObject



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

def document
  @document
end

#idObject



100
101
102
# File 'lib/kvom/model/base.rb', line 100

def id
  @document["id"]
end

#new?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/kvom/model/base.rb', line 104

def new?
  @new
end

#saveObject



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

def save
  self.class.save_document(@document, @new)
  @new = false
end