Class: Isomorfeus::Data::ObjectAccelerator
- Inherits:
-
Object
- Object
- Isomorfeus::Data::ObjectAccelerator
- Defined in:
- lib/isomorfeus/data/object_accelerator.rb
Instance Attribute Summary collapse
-
#object_class ⇒ Object
readonly
Returns the value of attribute object_class.
-
#object_class_name ⇒ Object
readonly
Returns the value of attribute object_class_name.
-
#store ⇒ Object
Returns the value of attribute store.
Class Method Summary collapse
Instance Method Summary collapse
- #close_store ⇒ Object
- #create_object(key, fields, already_saved) ⇒ Object
- #destroy_object(key) ⇒ Object
- #destroy_store ⇒ Object
- #each(&block) ⇒ Object
-
#initialize(ruby_class) ⇒ ObjectAccelerator
constructor
A new instance of ObjectAccelerator.
- #load_object(key: nil, id: nil, already_loaded: {}) ⇒ Object
- #object_from_ref(ref, already_loaded) ⇒ Object
- #save_object(key, fields, already_saved) ⇒ Object
- #search_each(query, options, &block) ⇒ Object
- #serialize(obj) ⇒ Object
- #unserialize(v) ⇒ Object
Constructor Details
#initialize(ruby_class) ⇒ ObjectAccelerator
Returns a new instance of ObjectAccelerator.
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/isomorfeus/data/object_accelerator.rb', line 11 def initialize(ruby_class) @object_class = ruby_class @object_class_name = ruby_class.name @class_cache = Isomorfeus.production? @store_path = File.(File.join(Isomorfeus.data_documents_path, "#{@object_class_name.underscore}")) open_store ObjectSpace.define_finalizer(self, self.class.finalize(self)) end |
Instance Attribute Details
#object_class ⇒ Object (readonly)
Returns the value of attribute object_class.
8 9 10 |
# File 'lib/isomorfeus/data/object_accelerator.rb', line 8 def object_class @object_class end |
#object_class_name ⇒ Object (readonly)
Returns the value of attribute object_class_name.
8 9 10 |
# File 'lib/isomorfeus/data/object_accelerator.rb', line 8 def object_class_name @object_class_name end |
#store ⇒ Object
Returns the value of attribute store.
9 10 11 |
# File 'lib/isomorfeus/data/object_accelerator.rb', line 9 def store @store end |
Class Method Details
.finalize(acc) ⇒ Object
4 5 6 |
# File 'lib/isomorfeus/data/object_accelerator.rb', line 4 def self.finalize(acc) proc { acc.close_store } end |
Instance Method Details
#close_store ⇒ Object
44 45 46 |
# File 'lib/isomorfeus/data/object_accelerator.rb', line 44 def close_store @store.close end |
#create_object(key, fields, already_saved) ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/isomorfeus/data/object_accelerator.rb', line 62 def create_object(key, fields, already_saved) ft = @object_class.field_types hash = fields.to_h do |k, v| [k, serialize_or_save(v, ft[k], already_saved)] end @store.add_document(hash.merge!({key: key})) end |
#destroy_object(key) ⇒ Object
70 71 72 73 |
# File 'lib/isomorfeus/data/object_accelerator.rb', line 70 def destroy_object(key) id = get_object_id(key) @store.delete(id) if id end |
#destroy_store ⇒ Object
39 40 41 42 |
# File 'lib/isomorfeus/data/object_accelerator.rb', line 39 def destroy_store close_store FileUtils.rm_rf(@store_path) end |
#each(&block) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/isomorfeus/data/object_accelerator.rb', line 52 def each(&block) ft = @object_class.field_types @store.each do |doc| hash = doc.to_h do |k, v| [k, unserialize_or_load(v, ft[k], {})] end block.call hash end end |
#load_object(key: nil, id: nil, already_loaded: {}) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/isomorfeus/data/object_accelerator.rb', line 75 def load_object(key: nil, id: nil, already_loaded: {}) hash = nil id = get_object_id(key) if key if id ft = @object_class.field_types hash = @store.doc(id)&.to_h do |k, v| [k, unserialize_or_load(v, ft[k], already_loaded)] end end hash end |
#object_from_ref(ref, already_loaded) ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/isomorfeus/data/object_accelerator.rb', line 22 def object_from_ref(ref, already_loaded) return nil if !ref || ref.empty? _, iso, type_class_name, key = ref.split('---') raise "not a valid object reference '#{ref}'" unless iso == "iso-object-reference" raise "invalid data class #{type_class_name}" unless Isomorfeus.valid_data_class_name?(type_class_name) type_class = Isomorfeus.cached_data_class(type_class_name) type_class.load(key: key, _already_loaded: already_loaded) end |
#save_object(key, fields, already_saved) ⇒ Object
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/isomorfeus/data/object_accelerator.rb', line 87 def save_object(key, fields, already_saved) id = get_object_id(key) return create_object(key, fields, already_saved) unless id ft = @object_class.field_types hash = fields.to_h do |k, v| [k, serialize_or_save(v, ft[k], already_saved)] end @store.update(id, hash.merge!({key: key})) true end |
#search_each(query, options, &block) ⇒ Object
48 49 50 |
# File 'lib/isomorfeus/data/object_accelerator.rb', line 48 def search_each(query, , &block) @store.search_each(query, , &block) end |
#serialize(obj) ⇒ Object
31 32 33 |
# File 'lib/isomorfeus/data/object_accelerator.rb', line 31 def serialize(obj) Oj.dump(obj, mode: :object, circular: true, class_cache: @class_cache) end |
#unserialize(v) ⇒ Object
35 36 37 |
# File 'lib/isomorfeus/data/object_accelerator.rb', line 35 def unserialize(v) Oj.load(v, mode: :object, circular: true, class_cache: @class_cache) end |