Module: NoBrainer::Document::Persistance

Extended by:
ActiveSupport::Concern
Defined in:
lib/no_brainer/document/persistance.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#_create(options = {}) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/no_brainer/document/persistance.rb', line 57

def _create(options={})
  attrs = self.class.persistable_attributes(@_attributes)
  result = NoBrainer.run(self.class.rql_table.insert(attrs))
  self.pk_value ||= result['generated_keys'].to_a.first
  @new_record = false
  unlock_unique_fields # just an optimization for the uniquness validation
  true
end

#_initialize(attrs = {}, options = {}) ⇒ Object



4
5
6
7
# File 'lib/no_brainer/document/persistance.rb', line 4

def _initialize(attrs={}, options={})
  @new_record = !options[:from_db]
  super
end

#_reload(options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/no_brainer/document/persistance.rb', line 21

def _reload(options={})
  criteria = root_class.raw
  if opt = options[:missing_attributes]
    criteria = criteria.pluck(opt[:pluck]) if opt[:pluck]
    criteria = criteria.without(opt[:without]) if opt[:without]
  end
  attrs = criteria.find(pk_value)

  options = options.merge(:pristine => true, :from_db => true)

  if options[:keep_ivars]
    assign_attributes(attrs, options)
  else
    instance_variables.each { |ivar| remove_instance_variable(ivar) }
    initialize(attrs, options)
  end

  self
end

#_save?(options = {}) ⇒ Boolean

Returns:



86
87
88
# File 'lib/no_brainer/document/persistance.rb', line 86

def _save?(options={})
  new_record? ? _create(options) : _update_only_changed_attrs(options)
end

#_update(attrs) ⇒ Object



66
67
68
69
# File 'lib/no_brainer/document/persistance.rb', line 66

def _update(attrs)
  rql = ->(doc){ self.class.persistable_attributes(attrs, :rql_doc => doc) }
  NoBrainer.run { selector.update(&rql) }
end

#_update_only_changed_attrs(options = {}) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/no_brainer/document/persistance.rb', line 71

def _update_only_changed_attrs(options={})
  # We won't be using the `changes` values, because they went through
  # read_attribute(), and we want the raw values.
  attrs = Hash[self.changed.map do |k|
    attr = @_attributes[k]
    # If we have a hash to save, we need to specify r.literal(),
    # otherwise, the hash would just get merged with the existing one.
    attr = RethinkDB::RQL.new.literal(attr) if attr.is_a?(Hash)
    [k, attr]
  end]
  _update(attrs) if attrs.present?
  unlock_unique_fields # just an optimization for the uniquness validation
  true
end

#deleteObject



117
118
119
120
121
122
123
124
# File 'lib/no_brainer/document/persistance.rb', line 117

def delete
  unless @destroyed
    NoBrainer.run { selector.delete }
    @destroyed = true
  end
  @_attributes.freeze
  true
end

#destroyObject



126
127
128
# File 'lib/no_brainer/document/persistance.rb', line 126

def destroy
  delete
end

#destroyed?Boolean

Returns:



13
14
15
# File 'lib/no_brainer/document/persistance.rb', line 13

def destroyed?
  !!@destroyed
end

#new_record?Boolean

Returns:



9
10
11
# File 'lib/no_brainer/document/persistance.rb', line 9

def new_record?
  !!@new_record
end

#persisted?Boolean

Returns:



17
18
19
# File 'lib/no_brainer/document/persistance.rb', line 17

def persisted?
  !new_record? && !destroyed?
end

#reload(options = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/no_brainer/document/persistance.rb', line 41

def reload(options={})
  [:without, :pluck].each do |type|
    next unless v = options.delete(type)

    v = Hash[v.flatten.map { |k| [k, true] }] if v.is_a?(Array)
    v = {v => true} unless v.is_a?(Hash)
    v = v.select { |k,_v| _v }
    v = v.with_indifferent_access
    next unless v.present?

    options[:missing_attributes] ||= {}
    options[:missing_attributes][type] = v
  end
  _reload(options)
end

#save(*args) ⇒ Object



98
99
100
# File 'lib/no_brainer/document/persistance.rb', line 98

def save(*args)
  save?(*args)
end

#save!(*args) ⇒ Object



94
95
96
# File 'lib/no_brainer/document/persistance.rb', line 94

def save!(*args)
  save?(*args) or raise NoBrainer::Error::DocumentInvalid, self
end

#save?(options = {}) ⇒ Boolean

Returns:



90
91
92
# File 'lib/no_brainer/document/persistance.rb', line 90

def save?(options={})
  _save?(options)
end

#update(*args) ⇒ Object Also known as: update_attributes



112
113
114
# File 'lib/no_brainer/document/persistance.rb', line 112

def update(*args)
  update?(*args)
end

#update!(*args) ⇒ Object Also known as: update_attributes!



107
108
109
# File 'lib/no_brainer/document/persistance.rb', line 107

def update!(*args)
  update?(*args) or raise NoBrainer::Error::DocumentInvalid, self
end

#update?(attrs, options = {}) ⇒ Boolean

Returns:



102
103
104
105
# File 'lib/no_brainer/document/persistance.rb', line 102

def update?(attrs, options={})
  assign_attributes(attrs, options)
  save?(options)
end