Class: Primitive::CompactFile
- Inherits:
-
Object
- Object
- Primitive::CompactFile
- Extended by:
- T::Sig
- Includes:
- Repository
- Defined in:
- lib/primitive/compact_file.rb
Overview
Knows how to load and save an Entity from/to local disk to a single file. YAML serialization will be used so in order to safely deserialize, the list of permitted classes that can be serialized must be specified.
Instance Attribute Summary collapse
-
#permitted_classes ⇒ Object
readonly
Returns the value of attribute permitted_classes.
Instance Method Summary collapse
-
#initialize(permitted_classes: [Primitive::Entity]) ⇒ CompactFile
constructor
A new instance of CompactFile.
- #retrieve(id) ⇒ Object
- #save(entity, id = nil) ⇒ Object
Constructor Details
#initialize(permitted_classes: [Primitive::Entity]) ⇒ CompactFile
Returns a new instance of CompactFile.
18 19 20 |
# File 'lib/primitive/compact_file.rb', line 18 def initialize(permitted_classes: [Primitive::Entity]) @permitted_classes = permitted_classes end |
Instance Attribute Details
#permitted_classes ⇒ Object (readonly)
Returns the value of attribute permitted_classes.
15 16 17 |
# File 'lib/primitive/compact_file.rb', line 15 def permitted_classes @permitted_classes end |
Instance Method Details
#retrieve(id) ⇒ Object
36 37 38 39 40 |
# File 'lib/primitive/compact_file.rb', line 36 def retrieve(id) deserialize(File.read(id)).tap do |entity| entity.send('id=', id) end end |
#save(entity, id = nil) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/primitive/compact_file.rb', line 23 def save(entity, id = nil) raise ArgumentError, 'id is required' unless id FileUtils.mkdir_p(File.dirname(id)) entity.send('id=', id) File.write(id, serialize(entity)) id end |