Class: ConcurrentPipeline::Stores::Yaml::Db
- Inherits:
-
Object
- Object
- ConcurrentPipeline::Stores::Yaml::Db
- Defined in:
- lib/concurrent_pipeline/stores/yaml/db.rb
Defined Under Namespace
Classes: Reader
Instance Attribute Summary collapse
-
#after_apply ⇒ Object
readonly
Returns the value of attribute after_apply.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#registry ⇒ Object
readonly
Returns the value of attribute registry.
Instance Method Summary collapse
- #all(type) ⇒ Object
- #apply(chgs) ⇒ Object
- #changeset ⇒ Object
- #create(type:, attributes:) ⇒ Object
- #everything ⇒ Object
- #find(type, id) ⇒ Object
-
#initialize(data:, registry:, after_apply: nil) ⇒ Db
constructor
A new instance of Db.
- #reader ⇒ Object
- #reader? ⇒ Boolean
- #set(data) ⇒ Object
- #to_h ⇒ Object
- #update(id:, type:, attributes:) ⇒ Object
Constructor Details
#initialize(data:, registry:, after_apply: nil) ⇒ Db
Returns a new instance of Db.
37 38 39 40 41 |
# File 'lib/concurrent_pipeline/stores/yaml/db.rb', line 37 def initialize(data:, registry:, after_apply: nil) @data = data @registry = registry @after_apply = after_apply end |
Instance Attribute Details
#after_apply ⇒ Object (readonly)
Returns the value of attribute after_apply.
36 37 38 |
# File 'lib/concurrent_pipeline/stores/yaml/db.rb', line 36 def after_apply @after_apply end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
36 37 38 |
# File 'lib/concurrent_pipeline/stores/yaml/db.rb', line 36 def data @data end |
#registry ⇒ Object (readonly)
Returns the value of attribute registry.
36 37 38 |
# File 'lib/concurrent_pipeline/stores/yaml/db.rb', line 36 def registry @registry end |
Instance Method Details
#all(type) ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/concurrent_pipeline/stores/yaml/db.rb', line 73 def all(type) type = registry.type_for(type) data .fetch(type, []) .map { registry.build(type, _1) } end |
#apply(chgs) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/concurrent_pipeline/stores/yaml/db.rb', line 47 def apply(chgs) changesets = chgs.is_a?(Array) ? chgs : [chgs] results = changesets.flat_map { _1.apply(self) } diffed = results.any?(&:diff?) after_apply&.call(changesets) if diffed diffed end |
#changeset ⇒ Object
43 44 45 |
# File 'lib/concurrent_pipeline/stores/yaml/db.rb', line 43 def changeset Changeset.new(registry: registry) end |
#create(type:, attributes:) ⇒ Object
91 92 93 94 95 96 |
# File 'lib/concurrent_pipeline/stores/yaml/db.rb', line 91 def create(type:, attributes:) type = registry.type_for(type) data[type] ||= [] data[type] << attributes end |
#everything ⇒ Object
67 68 69 70 71 |
# File 'lib/concurrent_pipeline/stores/yaml/db.rb', line 67 def everything data.each_with_object({}) do |(type, subdata), all_of_it| all_of_it[type] = subdata.map { |attrs| registry.build(type, attrs) } end end |
#find(type, id) ⇒ Object
81 82 83 84 85 |
# File 'lib/concurrent_pipeline/stores/yaml/db.rb', line 81 def find(type, id) type = registry.type_for(type) attrs = (data[type] || []).find { |attrs| attrs[:id] == id } registry.build(type, attrs) end |
#reader ⇒ Object
59 60 61 |
# File 'lib/concurrent_pipeline/stores/yaml/db.rb', line 59 def reader Reader.new(self) end |
#reader? ⇒ Boolean
55 56 57 |
# File 'lib/concurrent_pipeline/stores/yaml/db.rb', line 55 def reader? false end |
#set(data) ⇒ Object
87 88 89 |
# File 'lib/concurrent_pipeline/stores/yaml/db.rb', line 87 def set(data) @data = data end |
#to_h ⇒ Object
63 64 65 |
# File 'lib/concurrent_pipeline/stores/yaml/db.rb', line 63 def to_h data end |
#update(id:, type:, attributes:) ⇒ Object
98 99 100 101 102 103 104 105 106 |
# File 'lib/concurrent_pipeline/stores/yaml/db.rb', line 98 def update(id:, type:, attributes:) type = registry.type_for(type) attrs = (data[type] || []).find { |attrs| attrs[:id] == id } raise "Not found" unless attrs was_updated = attributes.any? { |k, v| attrs[k] != v } attrs.merge!(attributes) was_updated end |