Module: Modis::Persistence::ClassMethods

Defined in:
lib/modis/persistence.rb

Instance Method Summary collapse

Instance Method Details

#absolute_namespaceObject



48
49
50
# File 'lib/modis/persistence.rb', line 48

def absolute_namespace
  @absolute_namespace ||= [Modis.config.namespace, namespace].compact.join(':')
end

#all_index_enabled?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/modis/persistence.rb', line 68

def all_index_enabled?
  @use_all_index == true || @use_all_index.nil?
end

#bootstrap_sti(parent, child) ⇒ Object

:nodoc:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/modis/persistence.rb', line 17

def bootstrap_sti(parent, child)
  child.instance_eval do
    parent.instance_eval do
      class << self
        attr_accessor :sti_base, :sti_parent
      end
      attribute :type, :string unless attributes.key?('type')
    end

    @sti_child = true
    @sti_parent = parent
    @sti_base = parent.sti_base || parent

    bootstrap_attributes(parent)
    bootstrap_indexes(parent)
  end
end

#create(attrs) ⇒ Object



72
73
74
75
76
# File 'lib/modis/persistence.rb', line 72

def create(attrs)
  model = new(attrs)
  model.save
  model
end

#create!(attrs) ⇒ Object



78
79
80
81
82
# File 'lib/modis/persistence.rb', line 78

def create!(attrs)
  model = new(attrs)
  model.save!
  model
end

#deserialize(record) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/modis/persistence.rb', line 84

def deserialize(record)
  values = record.values
  values = MessagePack.unpack(msgpack_array_header(values.size) + values.join)
  keys = record.keys
  values.each_with_index { |v, i| record[keys[i]] = v }
  record
rescue MessagePack::MalformedFormatError
  record.each do |k, v|
    record[k] = MessagePack.unpack(v)
  end

  record
end

#enable_all_index(bool) ⇒ Object



64
65
66
# File 'lib/modis/persistence.rb', line 64

def enable_all_index(bool)
  @use_all_index = bool
end

#key_for(id) ⇒ Object



56
57
58
# File 'lib/modis/persistence.rb', line 56

def key_for(id)
  "#{absolute_namespace}:#{id}"
end

#namespaceObject



35
36
37
38
39
40
41
# File 'lib/modis/persistence.rb', line 35

def namespace
  @namespace ||= if sti_child?
                   "#{sti_parent.namespace}:#{name.split('::').last.underscore}"
                 else
                   name.split('::').map(&:underscore).join(':')
                 end
end

#namespace=(value) ⇒ Object



43
44
45
46
# File 'lib/modis/persistence.rb', line 43

def namespace=(value)
  @namespace = value
  @absolute_namespace = nil
end

#sti_base_absolute_namespaceObject



52
53
54
# File 'lib/modis/persistence.rb', line 52

def sti_base_absolute_namespace
  @sti_base_absolute_namespace ||= [Modis.config.namespace, sti_base.namespace].compact.join(':')
end

#sti_base_key_for(id) ⇒ Object



60
61
62
# File 'lib/modis/persistence.rb', line 60

def sti_base_key_for(id)
  "#{sti_base_absolute_namespace}:#{id}"
end