Class: NoBrainer::Document::AtomicOps::PendingAtomic

Inherits:
Object
  • Object
show all
Defined in:
lib/no_brainer/document/atomic_ops.rb

Direct Known Subclasses

PendingAtomicContainer, PendingAtomicUnset

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance, field, value, is_user_value, type) ⇒ PendingAtomic

Returns a new instance of PendingAtomic.



18
19
20
21
22
23
24
25
# File 'lib/no_brainer/document/atomic_ops.rb', line 18

def initialize(instance, field, value, is_user_value, type)
  @instance = instance
  @field = field.to_s
  @value = value
  @is_user_value = is_user_value
  @type = type
  @ops = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *a, &b) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/no_brainer/document/atomic_ops.rb', line 47

def method_missing(method, *a, &b)
  if method == :<<
    method = :append
    modify_source!
  end

  @ops << [method, a, b]
  self
end

Instance Attribute Details

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

Class Method Details

._new(instance, field, value, is_user_value) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/no_brainer/document/atomic_ops.rb', line 7

def self._new(instance, field, value, is_user_value)
  type = instance.class.fields[field.to_sym].try(:[], :type)
  type ||= value.class unless value.nil?

  case
  when type == Array then PendingAtomicArray
  when type == Set   then PendingAtomicSet
  else self
  end.new(instance, field, value, is_user_value, type)
end

Instance Method Details

#compile_rql_value(rql_doc) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/no_brainer/document/atomic_ops.rb', line 57

def compile_rql_value(rql_doc)
  field = @instance.class.lookup_field_alias(@field)
  if @is_user_value
    casted_value = @instance.class.cast_model_to_db_for(@field, @value)
    value = RethinkDB::RQL.new.expr(casted_value)
  else
    value = rql_doc[field]
  end
  value = value.default(default_value) if default_value
  @ops.reduce(value) { |v, (method, a, b)| v.__send__(method, *a, &b) }
end

#default_valueObject



27
28
29
30
31
32
33
34
35
# File 'lib/no_brainer/document/atomic_ops.rb', line 27

def default_value
  case
  when @type == Array   then []
  when @type == Set     then []
  when @type == Integer then 0
  when @type == Float   then 0.0
  when @type == String  then ""
  end
end

#initialize_copy(other) ⇒ Object



37
38
39
40
# File 'lib/no_brainer/document/atomic_ops.rb', line 37

def initialize_copy(other)
  super
  @ops = @ops.dup
end

#inspectObject



45
# File 'lib/no_brainer/document/atomic_ops.rb', line 45

def inspect; to_s; end

#modify_source!Object



69
70
71
72
73
74
# File 'lib/no_brainer/document/atomic_ops.rb', line 69

def modify_source!
  if (@is_user_value && @instance.instance_eval { @_attributes[@field].equal?(@value) }) ||
      !@instance._is_attribute_touched?(@field)
    @instance._write_attribute(@field, self)
  end
end

#to_sObject



42
43
44
# File 'lib/no_brainer/document/atomic_ops.rb', line 42

def to_s
  "<`#{@field}' with #{@ops.size} pending atomic operations>"
end