Class: LooseLeaf::DocumentAttribute

Inherits:
Object
  • Object
show all
Defined in:
lib/loose_leaf/document_attribute.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, attribute) ⇒ DocumentAttribute

Returns a new instance of DocumentAttribute.



5
6
7
8
9
10
# File 'lib/loose_leaf/document_attribute.rb', line 5

def initialize(document, attribute)
  @document = document
  @attribute = attribute

  @operations = []
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



3
4
5
# File 'lib/loose_leaf/document_attribute.rb', line 3

def attribute
  @attribute
end

#documentObject (readonly)

Returns the value of attribute document.



3
4
5
# File 'lib/loose_leaf/document_attribute.rb', line 3

def document
  @document
end

Instance Method Details

#apply_operation(operation, client_version) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/loose_leaf/document_attribute.rb', line 13

def apply_operation(operation, client_version)
  Rails.logger.debug "Applying client version #{client_version}, server version: #{version}"

  unless client_version > version
    operation = transform_old_operation(operation, client_version)
  end

  store_operation(operation)

  self.value = new_text(operation)

  [version, operation]
end

#clear_cacheObject



41
42
43
# File 'lib/loose_leaf/document_attribute.rb', line 41

def clear_cache
  Rails.cache.delete(value_key)
end

#clear_operations_cacheObject



45
46
47
# File 'lib/loose_leaf/document_attribute.rb', line 45

def clear_operations_cache
  Rails.cache.delete(operations_key)
end

#valueObject



31
32
33
# File 'lib/loose_leaf/document_attribute.rb', line 31

def value
  cached_value || document.attributes[attribute.to_s]
end

#value=(value) ⇒ Object



35
36
37
38
39
# File 'lib/loose_leaf/document_attribute.rb', line 35

def value=(value)
  return unless document.persisted?

  Rails.cache.write(value_key, value)
end

#versionObject



27
28
29
# File 'lib/loose_leaf/document_attribute.rb', line 27

def version
  operations.length
end