Class: LooseLeaf::DocumentAttribute
- Inherits:
-
Object
- Object
- LooseLeaf::DocumentAttribute
- Defined in:
- lib/loose_leaf/document_attribute.rb
Instance Attribute Summary collapse
-
#attribute ⇒ Object
readonly
Returns the value of attribute attribute.
-
#document ⇒ Object
readonly
Returns the value of attribute document.
Instance Method Summary collapse
- #apply_operation(operation, client_version) ⇒ Object
- #clear_cache ⇒ Object
- #clear_operations_cache ⇒ Object
-
#initialize(document, attribute) ⇒ DocumentAttribute
constructor
A new instance of DocumentAttribute.
- #value ⇒ Object
- #value=(value) ⇒ Object
- #version ⇒ Object
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
#attribute ⇒ Object (readonly)
Returns the value of attribute attribute.
3 4 5 |
# File 'lib/loose_leaf/document_attribute.rb', line 3 def attribute @attribute end |
#document ⇒ Object (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_cache ⇒ Object
41 42 43 |
# File 'lib/loose_leaf/document_attribute.rb', line 41 def clear_cache Rails.cache.delete(value_key) end |
#clear_operations_cache ⇒ Object
45 46 47 |
# File 'lib/loose_leaf/document_attribute.rb', line 45 def clear_operations_cache Rails.cache.delete(operations_key) end |
#value ⇒ Object
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 |
#version ⇒ Object
27 28 29 |
# File 'lib/loose_leaf/document_attribute.rb', line 27 def version operations.length end |