Class: Holistic::Database
- Inherits:
-
Object
- Object
- Holistic::Database
- Defined in:
- lib/holistic/database.rb
Defined Under Namespace
Modules: Migrations Classes: Node, Relation
Instance Attribute Summary collapse
-
#records ⇒ Object
readonly
Returns the value of attribute records.
-
#relations ⇒ Object
readonly
Returns the value of attribute relations.
Instance Method Summary collapse
- #define_relation(name:, inverse_of:) ⇒ Object
- #delete(id) ⇒ Object
- #find(id) ⇒ Object
-
#initialize ⇒ Database
constructor
A new instance of Database.
- #store(id, node_or_attrs) ⇒ Object
Constructor Details
#initialize ⇒ Database
Returns a new instance of Database.
6 7 8 9 |
# File 'lib/holistic/database.rb', line 6 def initialize @records = ::Hash.new @relations = ::Hash.new end |
Instance Attribute Details
#records ⇒ Object (readonly)
Returns the value of attribute records.
4 5 6 |
# File 'lib/holistic/database.rb', line 4 def records @records end |
#relations ⇒ Object (readonly)
Returns the value of attribute relations.
4 5 6 |
# File 'lib/holistic/database.rb', line 4 def relations @relations end |
Instance Method Details
#define_relation(name:, inverse_of:) ⇒ Object
11 12 13 14 15 16 |
# File 'lib/holistic/database.rb', line 11 def define_relation(name:, inverse_of:) raise ::ArgumentError if @relations.key?(name) || @relations.key?(inverse_of) @relations[name] = { inverse_of: } @relations[inverse_of] = { inverse_of: name } end |
#delete(id) ⇒ Object
44 45 46 |
# File 'lib/holistic/database.rb', line 44 def delete(id) records.delete(id) end |
#find(id) ⇒ Object
40 41 42 |
# File 'lib/holistic/database.rb', line 40 def find(id) @records[id] end |
#store(id, node_or_attrs) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/holistic/database.rb', line 18 def store(id, node_or_attrs) if @records.key?(id) return @records[id]&.tap do |node| node.attributes = case node_or_attrs in ::Hash then node_or_attrs in Node then node_or_attrs.attributes end end end node = case node_or_attrs in ::Hash then Node.new(id, node_or_attrs) in Node then node_or_attrs end node.__database__ = self @records[id] = node end |