lode 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0df5dca345e4cec7f64f0ac6b5f13ad2075cc06cd64021ea9e20e6c4b54d3a3e
4
+ data.tar.gz: f6aaec62dc57a6df4fa25d6f2f1dcbf337380a52a4e6eae560d87e1aa70f19db
5
+ SHA512:
6
+ metadata.gz: 2e9985b9140e620b17485c711d43e734bd7dd4d3fd13dcf02a1957afc9e2a69a4650734f16992f698ca27dc7e8b45304ac2d06526ae4c4bf2166ade280330733
7
+ data.tar.gz: 8fbd85cd0ce466be6d9e97db86d5e8dd995f41168af9e619cd63791259890ed4b692f429a3d41380012a15b2a50a8dd4cfab5698c7a4bb6e420fd3c62fb2088a
checksums.yaml.gz.sig ADDED
Binary file
data/LICENSE.adoc ADDED
@@ -0,0 +1,134 @@
1
+ = Hippocratic License
2
+
3
+ Version: 2.1.0.
4
+
5
+ Purpose. The purpose of this License is for the Licensor named above to
6
+ permit the Licensee (as defined below) broad permission, if consistent
7
+ with Human Rights Laws and Human Rights Principles (as each is defined
8
+ below), to use and work with the Software (as defined below) within the
9
+ full scope of Licensor’s copyright and patent rights, if any, in the
10
+ Software, while ensuring attribution and protecting the Licensor from
11
+ liability.
12
+
13
+ Permission and Conditions. The Licensor grants permission by this
14
+ license ("License"), free of charge, to the extent of Licensor’s
15
+ rights under applicable copyright and patent law, to any person or
16
+ entity (the "Licensee") obtaining a copy of this software and
17
+ associated documentation files (the "Software"), to do everything with
18
+ the Software that would otherwise infringe (i) the Licensor’s copyright
19
+ in the Software or (ii) any patent claims to the Software that the
20
+ Licensor can license or becomes able to license, subject to all of the
21
+ following terms and conditions:
22
+
23
+ * Acceptance. This License is automatically offered to every person and
24
+ entity subject to its terms and conditions. Licensee accepts this
25
+ License and agrees to its terms and conditions by taking any action with
26
+ the Software that, absent this License, would infringe any intellectual
27
+ property right held by Licensor.
28
+ * Notice. Licensee must ensure that everyone who gets a copy of any part
29
+ of this Software from Licensee, with or without changes, also receives
30
+ the License and the above copyright notice (and if included by the
31
+ Licensor, patent, trademark and attribution notice). Licensee must cause
32
+ any modified versions of the Software to carry prominent notices stating
33
+ that Licensee changed the Software. For clarity, although Licensee is
34
+ free to create modifications of the Software and distribute only the
35
+ modified portion created by Licensee with additional or different terms,
36
+ the portion of the Software not modified must be distributed pursuant to
37
+ this License. If anyone notifies Licensee in writing that Licensee has
38
+ not complied with this Notice section, Licensee can keep this License by
39
+ taking all practical steps to comply within 30 days after the notice. If
40
+ Licensee does not do so, Licensee’s License (and all rights licensed
41
+ hereunder) shall end immediately.
42
+ * Compliance with Human Rights Principles and Human Rights Laws.
43
+ [arabic]
44
+ . Human Rights Principles.
45
+ [loweralpha]
46
+ .. Licensee is advised to consult the articles of the United Nations
47
+ Universal Declaration of Human Rights and the United Nations Global
48
+ Compact that define recognized principles of international human rights
49
+ (the "Human Rights Principles"). Licensee shall use the Software in a
50
+ manner consistent with Human Rights Principles.
51
+ .. Unless the Licensor and Licensee agree otherwise, any dispute,
52
+ controversy, or claim arising out of or relating to (i) Section 1(a)
53
+ regarding Human Rights Principles, including the breach of Section 1(a),
54
+ termination of this License for breach of the Human Rights Principles,
55
+ or invalidity of Section 1(a) or (ii) a determination of whether any Law
56
+ is consistent or in conflict with Human Rights Principles pursuant to
57
+ Section 2, below, shall be settled by arbitration in accordance with the
58
+ Hague Rules on Business and Human Rights Arbitration (the "Rules");
59
+ provided, however, that Licensee may elect not to participate in such
60
+ arbitration, in which event this License (and all rights licensed
61
+ hereunder) shall end immediately. The number of arbitrators shall be one
62
+ unless the Rules require otherwise.
63
+ +
64
+ Unless both the Licensor and Licensee agree to the contrary: (1) All
65
+ documents and information concerning the arbitration shall be public and
66
+ may be disclosed by any party; (2) The repository referred to under
67
+ Article 43 of the Rules shall make available to the public in a timely
68
+ manner all documents concerning the arbitration which are communicated
69
+ to it, including all submissions of the parties, all evidence admitted
70
+ into the record of the proceedings, all transcripts or other recordings
71
+ of hearings and all orders, decisions and awards of the arbitral
72
+ tribunal, subject only to the arbitral tribunal’s powers to take such
73
+ measures as may be necessary to safeguard the integrity of the arbitral
74
+ process pursuant to Articles 18, 33, 41 and 42 of the Rules; and (3)
75
+ Article 26(6) of the Rules shall not apply.
76
+ . Human Rights Laws. The Software shall not be used by any person or
77
+ entity for any systems, activities, or other uses that violate any Human
78
+ Rights Laws. "Human Rights Laws" means any applicable laws,
79
+ regulations, or rules (collectively, "Laws") that protect human,
80
+ civil, labor, privacy, political, environmental, security, economic, due
81
+ process, or similar rights; provided, however, that such Laws are
82
+ consistent and not in conflict with Human Rights Principles (a dispute
83
+ over the consistency or a conflict between Laws and Human Rights
84
+ Principles shall be determined by arbitration as stated above). Where
85
+ the Human Rights Laws of more than one jurisdiction are applicable or in
86
+ conflict with respect to the use of the Software, the Human Rights Laws
87
+ that are most protective of the individuals or groups harmed shall
88
+ apply.
89
+ . Indemnity. Licensee shall hold harmless and indemnify Licensor (and
90
+ any other contributor) against all losses, damages, liabilities,
91
+ deficiencies, claims, actions, judgments, settlements, interest, awards,
92
+ penalties, fines, costs, or expenses of whatever kind, including
93
+ Licensor’s reasonable attorneys’ fees, arising out of or relating to
94
+ Licensee’s use of the Software in violation of Human Rights Laws or
95
+ Human Rights Principles.
96
+ * Failure to Comply. Any failure of Licensee to act according to the
97
+ terms and conditions of this License is both a breach of the License and
98
+ an infringement of the intellectual property rights of the Licensor
99
+ (subject to exceptions under Laws, e.g., fair use). In the event of a
100
+ breach or infringement, the terms and conditions of this License may be
101
+ enforced by Licensor under the Laws of any jurisdiction to which
102
+ Licensee is subject. Licensee also agrees that the Licensor may enforce
103
+ the terms and conditions of this License against Licensee through
104
+ specific performance (or similar remedy under Laws) to the extent
105
+ permitted by Laws. For clarity, except in the event of a breach of this
106
+ License, infringement, or as otherwise stated in this License, Licensor
107
+ may not terminate this License with Licensee.
108
+ * Enforceability and Interpretation. If any term or provision of this
109
+ License is determined to be invalid, illegal, or unenforceable by a
110
+ court of competent jurisdiction, then such invalidity, illegality, or
111
+ unenforceability shall not affect any other term or provision of this
112
+ License or invalidate or render unenforceable such term or provision in
113
+ any other jurisdiction; provided, however, subject to a court
114
+ modification pursuant to the immediately following sentence, if any term
115
+ or provision of this License pertaining to Human Rights Laws or Human
116
+ Rights Principles is deemed invalid, illegal, or unenforceable against
117
+ Licensee by a court of competent jurisdiction, all rights in the
118
+ Software granted to Licensee shall be deemed null and void as between
119
+ Licensor and Licensee. Upon a determination that any term or provision
120
+ is invalid, illegal, or unenforceable, to the extent permitted by Laws,
121
+ the court may modify this License to affect the original purpose that
122
+ the Software be used in compliance with Human Rights Principles and
123
+ Human Rights Laws as closely as possible. The language in this License
124
+ shall be interpreted as to its fair meaning and not strictly for or
125
+ against any party.
126
+ * Disclaimer. TO THE FULL EXTENT ALLOWED BY LAW, THIS SOFTWARE COMES
127
+ "AS IS," WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED, AND LICENSOR AND
128
+ ANY OTHER CONTRIBUTOR SHALL NOT BE LIABLE TO ANYONE FOR ANY DAMAGES OR
129
+ OTHER LIABILITY ARISING FROM, OUT OF, OR IN CONNECTION WITH THE SOFTWARE
130
+ OR THIS LICENSE, UNDER ANY KIND OF LEGAL CLAIM.
131
+
132
+ This Hippocratic License is an link:https://ethicalsource.dev[Ethical Source license] and is offered
133
+ for use by licensors and licensees at their own risk, on an "AS IS" basis, and with no warranties
134
+ express or implied, to the maximum extent permitted by Laws.
data/README.adoc ADDED
@@ -0,0 +1,329 @@
1
+ :toc: macro
2
+ :toclevels: 5
3
+ :figure-caption!:
4
+
5
+ :dry_monads_link: link:https://dry-rb.org/gems/dry-monads[Dry Monads]
6
+ :pstore_link: link:https://github.com/ruby/pstore[PStore]
7
+ :ruby_link: link:https://www.ruby-lang.org[Ruby]
8
+ :xdg_link: link:https://alchemists.io/projects/xdg[XDG]
9
+
10
+ = Lode
11
+
12
+ Lode -- as in a geological deposit or a vein of minerals -- is a {pstore_link} of object data that can be mined for valuable information.
13
+
14
+ As noted in the {pstore_link} documentation all objects are marshaled which is not without caveats and dangers but, if you only need a simple object store, {pstore_link} is a solution. Lode takes this a step further by allowing you to have a pipeline workflow with a Domain Specific Language (DSL) for creating, updating, finding, and deleting records.
15
+
16
+ toc::[]
17
+
18
+ == Features
19
+
20
+ - Built atop {pstore_link}.
21
+ - Uses the _Railway Pattern_ via {dry_monads_link} for fault tolerant pipelines.
22
+ - Emphasizes use of `Hash`, `Data`, `Struct`, or link:https://alchemists.io/projects/wholable[whole value objects] in general.
23
+ - Great for {xdg_link} caches, simple file-based databases, or simple file stores in general.
24
+
25
+ == Requirements
26
+
27
+ . {ruby_link}.
28
+
29
+ == Setup
30
+
31
+ To install _with_ security, run:
32
+
33
+ [source,bash]
34
+ ----
35
+ # 💡 Skip this line if you already have the public certificate installed.
36
+ gem cert --add <(curl --compressed --location https://alchemists.io/projects/lode/gems.pem)
37
+ gem install lode --trust-policy HighSecurity
38
+ ----
39
+
40
+ To install _without_ security, run:
41
+
42
+ [source,bash]
43
+ ----
44
+ gem install lode
45
+ ----
46
+
47
+ You can also add the gem directly to your project:
48
+
49
+ [source,bash]
50
+ ----
51
+ bundle add lode
52
+ ----
53
+
54
+ Once the gem is installed, you only need to require it:
55
+
56
+ [source,ruby]
57
+ ----
58
+ require "lode"
59
+ ----
60
+
61
+ == Usage
62
+
63
+ To use, create a Lode instance and then use database-like messages to interact with your table of records as desired. For example, the following creates a table of _links_ and stores them within the `demo.store` file and then interacts with those records:
64
+
65
+ [source,ruby]
66
+ ----
67
+ lode = Lode.new "demo.store"
68
+
69
+ lode.commit :links do
70
+ upsert({id: 1, url: "https://one.com"})
71
+ upsert({id: 2, url: "https://2.com"})
72
+ upsert({id: 3, url: "https://three.com"})
73
+ end
74
+
75
+ # Success({:id=>3, :url=>"https://three.com"})
76
+ # (only the last record created/updated is answered back)
77
+
78
+ lode.commit(:links) { upsert({id: 2, url: "https://two.com"}) }
79
+ # Success({:id=>2, :url=>"https://two.com"})
80
+
81
+ lode.commit(:links) { find 1 }
82
+ # Success({:id=>1, :url=>"https://one.com"})
83
+
84
+ lode.commit(:links) { find 13 }
85
+ # Failure("Unable to find id: 13.")
86
+
87
+ lode.commit(:links) { delete 2 }
88
+ # Success({:id=>2, :url=>"https://two.com"})
89
+
90
+ lode.commit(:links) { delete 13 }
91
+ # Failure("Unable to find id: 13.")
92
+
93
+ lode.commit(:links, &:all)
94
+ # Success([{:id=>1, :url=>"https://one.com"}, {:id=>3, :url=>"https://three.com"}])
95
+ ----
96
+
97
+ The default configuration is set up to use a primitive `Hash` which is the default behavior when using {pstore_link}. Everything answered back is a result monad as provided by the {dry_monads_link} gem so you can leverage the _Railway Pattern_ to build robust, fault tolerant, pipelines.
98
+
99
+ === Configuration
100
+
101
+ Lode can be configured using a block or a keyword argument. The following are identical:
102
+
103
+ [source,ruby]
104
+ ----
105
+ # With block.
106
+ lode = Lode.new "demo.store" do |config|
107
+ config.mode = :max
108
+ config.table = Lode::Tables::Value
109
+ config.primary_key = :name
110
+ end
111
+
112
+ # With keyword argument.
113
+ configuration = Lode::Configuration[mode: :max, table: Lode::Tables::Value, primary_key: :name]
114
+ lode = Lode.new "demo.store", configuration:
115
+ ----
116
+
117
+ The default configuration consists of the following attributes:
118
+
119
+ [source,ruby]
120
+ ----
121
+ Lode::Configuration[
122
+ store: PStore,
123
+ mode: :default,
124
+ table: Lode::Tables::Dictionary,
125
+ primary_key: :id,
126
+ registry: {}
127
+ ]
128
+ ----
129
+
130
+ Each key can be configured as follows:
131
+
132
+ * `store`: Any object that adhere's to the {pstore_link} Object API. You'll most likely never need to change this but is available if desired. Default: `PStore`.
133
+ * `mode`: The mode determines {pstore_link} behavior and can be one of the following:
134
+ ** `:default`: The default mode and is identical to `PStore.new path`.
135
+ ** `:thread`: Ensures a thread safe `PStore` instance is created. This is identical to `PStore.new path, true`.
136
+ ** `:file`: Ensures a file safe `PStore` instance is created. This is identical to setting `store.ultra_safe = true` on a `PStore` instance.
137
+ ** `:max`: Ensures a thread _and_ file safe `PStore` instance is created for situations where you need maximum safety.
138
+ * `table`: Defines the _type_ of table used to interact with your records. The following values are supported:
139
+ ** `Lode::Tables::Dictionary`: The default value which allows you to interact with a `Hash` of records but would also work with any object that can respond to `+#[]+` and `+#[]=+`.
140
+ ** `Lode::Tables::Value`: Allows you to interact with whole value objects like `Data`, `Struct`, or link:https://alchemists.io/projects/wholable[whole value objects] in general which have attribute readers and writers.
141
+ * `primary_key`: Defines the primary key used when interacting with your table of records (useful when finding or upserting records). Default: `:id`.
142
+ * `registry`: Used for registering default settings for your tables. _This is not meant to be used directly_ but is documented for transparency.
143
+
144
+ === Registry
145
+
146
+ The registry is part of the configuration and directly accessible via a Lode instance. The registry allows you to customize individual table behavior as desired. For instance, you could have a `Hash` table or value table (i.e. `Data`, `Struct`, etc). Additionally, each table can have different primary keys too. The registry accepts three arguments in this format:
147
+
148
+ ....
149
+ key, model:, primary_key:
150
+ ....
151
+
152
+ The default model is a `Hash` but could be `Data`, `Struct`, or any value object. The default primary key is `:id` but could be any attribute that uniquely identifies a record. This means the following is identical when registering default table settings:
153
+
154
+ [source,ruby]
155
+ ----
156
+ # Initialization with registration.
157
+ lode = Lode.new("demo.store") { |config| config.register :links, primary_key: :slug }
158
+
159
+ # Direct registration.
160
+ lode = Lode.new "demo.store"
161
+ lode.register :links, primary_key: :slug
162
+ ----
163
+
164
+ Given the above, you could now create and find _link_ records by _slug_ like so:
165
+
166
+ [source,ruby]
167
+ ----
168
+ lode.commit(:links) { upsert({id: 1, slug: :demo, url: "https://demo.com"}) }
169
+ lode.read(:links) { find :demo }
170
+
171
+ # Success({:id=>1, :slug=>:demo, :url=>"https://demo.com"})
172
+ ----
173
+
174
+ Keep in mind that the registry _only defines default behavior_. You can override default behavior by specifying a key. Example:
175
+
176
+ [source,ruby]
177
+ ----
178
+ lode.read(:links) { find 1, key: :id }
179
+ # Success({:id=>1, :slug=>:demo, :url=>"https://demo.com"})
180
+ ----
181
+
182
+ Even though the default primary key was registered to be `:slug`, we were able to use `:id` instead. The optional `:key` keyword argument is also available for _all_ table methods.
183
+
184
+ === Tables
185
+
186
+ As mentioned when configuring a Lode instance, two _types_ of tables are available to you. The default (i.e. `Lode::Tables::Dictionary`) allows you to interact with `Hash` records which is compatible with default `PStore` functionality. Example:
187
+
188
+ [source,ruby]
189
+ ----
190
+ lode = Lode.new "demo.store"
191
+ lode.commit(:links) { upsert({id: 1, url: "https://one.com"}) }
192
+ # Success({:id=>1, :url=>"https://one.com"})
193
+ ----
194
+
195
+ The second, and more powerful table type, is a value object table (i.e. `Lode::Tables::Value`). Here's an example using a `Data` model:
196
+
197
+ [source,ruby]
198
+ ----
199
+ Model = Data.define :id, :url
200
+
201
+ lode = Lode.new("demo.store") do |config|
202
+ config.table = Lode::Tables::Value
203
+ config.register :links, model: Model
204
+ end
205
+
206
+ lode.commit :links do
207
+ upsert({id: 1, url: "https://one.com"})
208
+ upsert Model[id: 2, url: "https://"]
209
+ end
210
+
211
+ lode.commit(:links, &:all)
212
+ # Success([#<data Model id=1, url="https://one.com">, #<data Model id=2, url="https://">])
213
+ ----
214
+
215
+ The above would work with a `Struct` or any value object. One of many conveniences when using value objects -- as shown above -- is you can upsert records using a `Hash` or an instance of your value object.
216
+
217
+ Each table supports the following methods:
218
+
219
+ * `#primary_key`: Answers the primary key as defined when the table was registered or the default key (i.e. `:id`).
220
+ * `#all`: Answers all records for a table.
221
+ * `#find`: Finds a record by primary key.
222
+ * `#upsert`: Creates or updates a new or existing record by primary key.
223
+ * `#delete`: Deletes an existing record by primary key.
224
+
225
+ All of the above (except `#primary_key`) support an optional `:key` keyword argument which allows you to use a different key that is not the primary key if desired.
226
+
227
+ === Read/Write
228
+
229
+ You've already seen a few examples of how to read and write to your object store but, to be explicit, the following are supported:
230
+
231
+ * `#commit`: Allows you to read and write records as a single transaction.
232
+ * `#read`: Allows you to _only_ read data from your object store as a single transaction. Write operations will result in an exception.
233
+
234
+ Both of the above methods require you to supply the table name and a block with operations. Since a table name must always be supplied this means you can interact with multiple tables within the same file store or you can write different tables to different files. Up to you. Here's an example of a basic write and read operation:
235
+
236
+ [source,ruby]
237
+ ----
238
+ lode = Lode.new "demo.store"
239
+
240
+ # Read/Write
241
+ lode.commit(:links) { upsert({id: 1, url: "https://demo.com"}) }
242
+
243
+ # Read Only
244
+ lode.read(:links) { find 1 }
245
+ ----
246
+
247
+ Attempting to `#read` with a _write_ operation will result in an error. Example:
248
+
249
+ [source,ruby]
250
+ ----
251
+ lode.read(:links) { delete 1 }
252
+ # in read-only transaction (PStore::Error)
253
+ ----
254
+
255
+ For those familiar with {pstore_link} behavior, a commit and read operation is the equivalent of the following using `PStore` directly:
256
+
257
+ [source,ruby]
258
+ ----
259
+ require "pstore"
260
+
261
+ store = PStore.new "demo.store"
262
+
263
+ # Read/Write
264
+ store.transaction do |store|
265
+ store[:links] = store.fetch(:links, []).append({id: 1, url: "https://demo.com"})
266
+ end
267
+
268
+ # [{:id=>1, :url=>"https://demo.com"}]
269
+
270
+ # Read Only
271
+ store.transaction(true) { |store| store.fetch(:links, []).find { |record| record[:id] == 1 } }
272
+ # {:id=>1, :url=>"https://demo.com"}
273
+ ----
274
+
275
+ === Store
276
+
277
+ If at any time you need access to the original `PStore` instance, you can ask for it. Example:
278
+
279
+ [source,ruby]
280
+ ----
281
+ lode = Lode.new "demo.store"
282
+ load.store
283
+
284
+ # #<PStore:0x000000010c592178 @abort=false, @filename="demo.store", @lock=#<Thread::Mutex:0x000000010c5fbfd8>, @thread_safe=false, @ultra_safe=false>
285
+ ----
286
+
287
+ == Development
288
+
289
+ To contribute, run:
290
+
291
+ [source,bash]
292
+ ----
293
+ git clone https://github.com/bkuhlmann/lode
294
+ cd lode
295
+ bin/setup
296
+ ----
297
+
298
+ You can also use the IRB console for direct access to all objects:
299
+
300
+ [source,bash]
301
+ ----
302
+ bin/console
303
+ ----
304
+
305
+ == Tests
306
+
307
+ To test, run:
308
+
309
+ [source,bash]
310
+ ----
311
+ bin/rake
312
+ ----
313
+
314
+ == link:https://alchemists.io/policies/license[License]
315
+
316
+ == link:https://alchemists.io/policies/security[Security]
317
+
318
+ == link:https://alchemists.io/policies/code_of_conduct[Code of Conduct]
319
+
320
+ == link:https://alchemists.io/policies/contributions[Contributions]
321
+
322
+ == link:https://alchemists.io/projects/lode/versions[Versions]
323
+
324
+ == link:https://alchemists.io/community[Community]
325
+
326
+ == Credits
327
+
328
+ * Built with link:https://alchemists.io/projects/gemsmith[Gemsmith].
329
+ * Engineered by link:https://alchemists.io/team/brooke_kuhlmann[Brooke Kuhlmann].
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "forwardable"
4
+
5
+ module Lode
6
+ # Provides an enhanced PStore-based client.
7
+ class Client
8
+ extend Forwardable
9
+
10
+ attr_reader :path, :store
11
+
12
+ delegate %i[register registry] => :configuration
13
+
14
+ def initialize path, configuration: Configuration.new
15
+ yield configuration if block_given?
16
+
17
+ @path = path
18
+ @configuration = configuration
19
+ @store = configuration.store_for path
20
+ end
21
+
22
+ def read(key, &) = transact(__method__, key, &)
23
+
24
+ def commit(key, &) = transact(__method__, key, &)
25
+
26
+ private
27
+
28
+ attr_reader :configuration
29
+
30
+ def transact(mode, key, &)
31
+ store.transaction mode == :read do
32
+ configuration.table_for(store, key).instance_eval(&)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "pstore"
4
+
5
+ module Lode
6
+ # Models the default configuration.
7
+ Configuration = Struct.new :store, :mode, :table, :primary_key, :registry do
8
+ using Refines::PersistentStore
9
+ using Refinements::Arrays
10
+
11
+ def initialize store: PStore,
12
+ mode: :default,
13
+ table: Tables::Dictionary,
14
+ primary_key: PRIMARY_KEY,
15
+ registry: {}
16
+ super
17
+ end
18
+
19
+ def store_for(path) = store.with(path, mode:)
20
+
21
+ def table_for store, key, setting: Setting.new
22
+ table.new store, key, setting: registry.fetch(key, setting)
23
+ end
24
+
25
+ def register key, model: Hash, primary_key: PRIMARY_KEY
26
+ registry[key] = Setting[model:, primary_key:]
27
+ self
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "pstore"
4
+ require "refinements/arrays"
5
+
6
+ module Lode
7
+ module Refines
8
+ # Refined and enhanced PStore functionality.
9
+ module PersistentStore
10
+ using Refinements::Arrays
11
+
12
+ refine PStore.singleton_class do
13
+ def with path, mode: :default
14
+ case mode
15
+ when :default then new path
16
+ when :thread then new path, true
17
+ when :file then new(path).tap { |instance| instance.ultra_safe = true }
18
+ when :max then new(path, true).tap { |instance| instance.ultra_safe = true }
19
+ else fail PStore::Error, %(Invalid mode. Use: #{MODES.to_usage "or"}.)
20
+ end
21
+ end
22
+ end
23
+
24
+ refine PStore do
25
+ def thread_safe? = @thread_safe
26
+
27
+ def file_safe? = @ultra_safe
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lode
4
+ Setting = Data.define :model, :primary_key do
5
+ def initialize model: Hash, primary_key: PRIMARY_KEY
6
+ super
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "dry/monads"
4
+ require "refinements/arrays"
5
+
6
+ module Lode
7
+ module Tables
8
+ # Provides an abstract table for subclassing.
9
+ class Abstract
10
+ include Dry::Monads[:result]
11
+
12
+ using Refinements::Arrays
13
+
14
+ def initialize store, key, setting: Setting.new
15
+ @store = store
16
+ @key = key
17
+ @setting = setting
18
+ @records = store.fetch key, []
19
+ end
20
+
21
+ def primary_key = setting.primary_key
22
+
23
+ def all = Success records.dup.freeze
24
+
25
+ def find value, key: primary_key
26
+ fail NotImplementedError,
27
+ "`#{self.class}##{__method__} #{method(__method__).parameters}` must be implemented."
28
+ end
29
+
30
+ def upsert value, key: primary_key
31
+ fail NotImplementedError,
32
+ "`#{self.class}##{__method__} #{method(__method__).parameters}` must be implemented."
33
+ end
34
+
35
+ def delete value, key: primary_key
36
+ find(value, key:).fmap do |record|
37
+ records.delete record
38
+ store[key] = records
39
+ record
40
+ end
41
+ end
42
+
43
+ protected
44
+
45
+ attr_reader :store, :key, :setting, :records
46
+
47
+ def update existing, record
48
+ records.supplant existing, record
49
+ store[key] = records
50
+ Success record
51
+ end
52
+
53
+ def append record
54
+ records.append record
55
+ store[key] = records
56
+ Success record
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "dry/monads"
4
+
5
+ module Lode
6
+ module Tables
7
+ # Provides an array-based table for dictionary or hash-like objects.
8
+ class Dictionary < Abstract
9
+ include Dry::Monads[:result]
10
+
11
+ def find value, key: primary_key
12
+ records.find { |record| record[key] == value }
13
+ .then do |record|
14
+ return Success record if record
15
+
16
+ Failure "Unable to find #{key}: #{value.inspect}."
17
+ end
18
+ end
19
+
20
+ def upsert value, key: primary_key
21
+ find(value[key]).either(
22
+ -> existing { update existing, value },
23
+ proc { append value }
24
+ )
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "dry/monads"
4
+
5
+ module Lode
6
+ module Tables
7
+ # Provides an array-based table for value objects.
8
+ class Value < Abstract
9
+ include Dry::Monads[:result]
10
+
11
+ def find value, key: primary_key
12
+ records.find { |record| record.public_send(key) == value }
13
+ .then do |record|
14
+ return Success record if record
15
+
16
+ Failure "Unable to find #{key}: #{value.inspect}."
17
+ end
18
+ end
19
+
20
+ def upsert value, key: primary_key
21
+ record = record_for value
22
+
23
+ find(record.public_send(key)).either(
24
+ -> existing { update existing, record },
25
+ proc { append record }
26
+ )
27
+ end
28
+
29
+ private
30
+
31
+ # :reek:FeatureEnvy
32
+ def record_for value
33
+ model = setting.model
34
+ value.is_a?(model) ? value : model[**value.to_h]
35
+ end
36
+ end
37
+ end
38
+ end
data/lib/lode.rb ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "zeitwerk"
4
+
5
+ Zeitwerk::Loader.for_gem.setup
6
+
7
+ # Main namespace.
8
+ module Lode
9
+ PRIMARY_KEY = :id
10
+ MODES = %i[default thread file max].freeze
11
+
12
+ def self.new(...) = Client.new(...)
13
+ end
data/lode.gemspec ADDED
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "lode"
5
+ spec.version = "0.0.0"
6
+ spec.authors = ["Brooke Kuhlmann"]
7
+ spec.email = ["brooke@alchemists.io"]
8
+ spec.homepage = "https://alchemists.io/projects/lode"
9
+ spec.summary = "A monadic store of marshaled objects."
10
+ spec.license = "Hippocratic-2.1"
11
+
12
+ spec.metadata = {
13
+ "bug_tracker_uri" => "https://github.com/bkuhlmann/lode/issues",
14
+ "changelog_uri" => "https://alchemists.io/projects/lode/versions",
15
+ "documentation_uri" => "https://alchemists.io/projects/lode",
16
+ "funding_uri" => "https://github.com/sponsors/bkuhlmann",
17
+ "label" => "Lode",
18
+ "rubygems_mfa_required" => "true",
19
+ "source_code_uri" => "https://github.com/bkuhlmann/lode"
20
+ }
21
+
22
+ spec.signing_key = Gem.default_key_path
23
+ spec.cert_chain = [Gem.default_cert_path]
24
+
25
+ spec.required_ruby_version = "~> 3.2"
26
+ spec.add_dependency "dry-monads", "~> 1.6"
27
+ spec.add_dependency "refinements", "~> 11.0"
28
+ spec.add_dependency "zeitwerk", "~> 2.6"
29
+
30
+ spec.extra_rdoc_files = Dir["README*", "LICENSE*"]
31
+ spec.files = Dir["*.gemspec", "lib/**/*"]
32
+ end
data.tar.gz.sig ADDED
Binary file
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lode
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Brooke Kuhlmann
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain:
11
+ - |
12
+ -----BEGIN CERTIFICATE-----
13
+ MIIEeDCCAuCgAwIBAgIBATANBgkqhkiG9w0BAQsFADBBMQ8wDQYDVQQDDAZicm9v
14
+ a2UxGjAYBgoJkiaJk/IsZAEZFgphbGNoZW1pc3RzMRIwEAYKCZImiZPyLGQBGRYC
15
+ aW8wHhcNMjMwMzIyMTYxNDQxWhcNMjUwMzIxMTYxNDQxWjBBMQ8wDQYDVQQDDAZi
16
+ cm9va2UxGjAYBgoJkiaJk/IsZAEZFgphbGNoZW1pc3RzMRIwEAYKCZImiZPyLGQB
17
+ GRYCaW8wggGiMA0GCSqGSIb3DQEBAQUAA4IBjwAwggGKAoIBgQCro8tj5/E1Hg88
18
+ f4qfiwPVd2zJQHvdYt4GHVvuHRRgx4HGhJuNp+4BId08RBn7V6V1MW6MY3kezRBs
19
+ M+7QOQ4b1xNLTvY7FYQB1wGK5a4x7TTokDrPYQxDB2jmsdDYCzVbIMrAvUfcecRi
20
+ khyGZCdByiiCl4fKv77P12tTT+NfsvXkLt/AYCGwjOUyGKTQ01Z6eC09T27GayPH
21
+ QQvIkakyFgcJtzSyGzs8bzK5q9u7wQ12MNTjJoXzW69lqp0oNvDylu81EiSUb5S6
22
+ QzzPxZBiRB1sgtbt1gUbVI262ZDq1gR+HxPFmp+Cgt7ZLIJZAtesQvtcMzseXpfn
23
+ hpmm0Sw22KGhRAy/mqHBRhDl5HqS1SJp2Ko3lcnpXeFResp0HNlt8NSu13vhC08j
24
+ GUHU9MyIXbFOsnp3K3ADrAVjPWop8EZkmUR3MV/CUm00w2cZHCSGiXl1KMpiVKvk
25
+ Ywr1gd2ZME4QLSo+EXUtLxDUa/W3xnBS8dBOuMMz02FPWYr3PN8CAwEAAaN7MHkw
26
+ CQYDVR0TBAIwADALBgNVHQ8EBAMCBLAwHQYDVR0OBBYEFAFgmv0tYMZnItuPycSM
27
+ F5wykJEVMB8GA1UdEQQYMBaBFGJyb29rZUBhbGNoZW1pc3RzLmlvMB8GA1UdEgQY
28
+ MBaBFGJyb29rZUBhbGNoZW1pc3RzLmlvMA0GCSqGSIb3DQEBCwUAA4IBgQAX+EGY
29
+ 9RLYGxF1VLZz+G1ACQc4uyrCB6kXwI06kzUa5dF9tPXqTX9ffnz3/W8ck2IQhKzu
30
+ MKO2FVijzbDWTsZeZGglS4E+4Jxpau1lU9HhOIcKolv6LeC6UdALTFudY+GLb8Xw
31
+ REXgaJkjzzhkUSILmEnRwEbY08dVSl7ZAaxVI679vfI2yapLlIwpbBgmQTiTvPr3
32
+ qyyLUno9flYEOv9fmGHunSrM+gE0/0niGTXa5GgXBXYGS2he4LQGgSBfGp/cTwMU
33
+ rDKJRcusZ12lNBeDfgqACz/BBJF8FLodgk6rGMRZz7+ZmjjHEmpG5bQpR6Q2BuWL
34
+ XMtYk/QzaWuhiR7pWjiF8jbdd7RO6or0ohq7iFkokz/5xrtQ/vPzU2RQ3Qc6YaKw
35
+ 3n5C8/6Zh9DYTkpcwPSuIfAga6wf4nXc9m6JAw8AuMLaiWN/r/2s4zJsUHYERJEu
36
+ gZGm4JqtuSg8pYjPeIJxS960owq+SfuC+jxqmRA54BisFCv/0VOJi7tiJVY=
37
+ -----END CERTIFICATE-----
38
+ date: 2023-08-05 00:00:00.000000000 Z
39
+ dependencies:
40
+ - !ruby/object:Gem::Dependency
41
+ name: dry-monads
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.6'
47
+ type: :runtime
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.6'
54
+ - !ruby/object:Gem::Dependency
55
+ name: refinements
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '11.0'
61
+ type: :runtime
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '11.0'
68
+ - !ruby/object:Gem::Dependency
69
+ name: zeitwerk
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '2.6'
75
+ type: :runtime
76
+ prerelease: false
77
+ version_requirements: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '2.6'
82
+ description:
83
+ email:
84
+ - brooke@alchemists.io
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files:
88
+ - README.adoc
89
+ - LICENSE.adoc
90
+ files:
91
+ - LICENSE.adoc
92
+ - README.adoc
93
+ - lib/lode.rb
94
+ - lib/lode/client.rb
95
+ - lib/lode/configuration.rb
96
+ - lib/lode/refines/persistent_store.rb
97
+ - lib/lode/setting.rb
98
+ - lib/lode/tables/abstract.rb
99
+ - lib/lode/tables/dictionary.rb
100
+ - lib/lode/tables/value.rb
101
+ - lode.gemspec
102
+ homepage: https://alchemists.io/projects/lode
103
+ licenses:
104
+ - Hippocratic-2.1
105
+ metadata:
106
+ bug_tracker_uri: https://github.com/bkuhlmann/lode/issues
107
+ changelog_uri: https://alchemists.io/projects/lode/versions
108
+ documentation_uri: https://alchemists.io/projects/lode
109
+ funding_uri: https://github.com/sponsors/bkuhlmann
110
+ label: Lode
111
+ rubygems_mfa_required: 'true'
112
+ source_code_uri: https://github.com/bkuhlmann/lode
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - "~>"
120
+ - !ruby/object:Gem::Version
121
+ version: '3.2'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubygems_version: 3.4.18
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: A monadic store of marshaled objects.
132
+ test_files: []
metadata.gz.sig ADDED
Binary file