dynamicschema 1.0.0.beta02 → 1.0.0.beta03

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0251af8d67cb7dbbaeea74a21dd72dd2385e2449d4aa41c48753f4b05428dfed
4
- data.tar.gz: ce213906df2fe4facdf469717c61f5138aa3cae7469ba0ac3d5dc21e6dadd958
3
+ metadata.gz: 5685a6ff1c79676a075f3f7f2cd5863c35466a402c79f3af46c2e164eb37581a
4
+ data.tar.gz: 374fbd61c218c4dc4bc6620d18fa3a973d197aa96032c6c2998bb4d3ed8da378
5
5
  SHA512:
6
- metadata.gz: 48b889c8c37b1a454819a5ea8de9d63179348b7cede7ce043f0972bef384012cb9ed3a4aaabe8a739f3d51e21bee0f3315231fc5ea6b80cab48c945f3ac17919
7
- data.tar.gz: 3b317ae13cfb0d558fe8f77267136e2132842780f39331ccf192e7f5b65d397ea1ac8bb3fa8d615f38afb9c1adf09a227a689921d63237525b8f6fc04b040cfa
6
+ metadata.gz: 80c7ae562b99ddabf6fe6220a995d845beda151c13683aff13891c47e089700579cb7a099b08a4ac789569ab0499612c2f467222661082b1794cbb75d424a1d9
7
+ data.tar.gz: 2da0f6cf54388b6e2e5d3f3ac05214c6f3ea27c8c763fb1ac9a84bbbddbad92d21b29db328c70388653ffd4317a409a58b0165323626e2e2ea2988b188df14c3
data/README.md CHANGED
@@ -139,7 +139,7 @@ as well as a `Hash` of options, all of which are optional:
139
139
  require 'dynamic_schema'
140
140
 
141
141
  # define a schema structure with values
142
- schema = DynamicSchema::Builder.new.define do
142
+ schema = DynamicSchema.define do
143
143
  api_key
144
144
  version, String, default: '1.0'
145
145
  end
@@ -189,7 +189,7 @@ Notice an *object* does not accept a type as it is always of type `Object`.
189
189
  ```ruby
190
190
  require 'dynamic_schema'
191
191
 
192
- schema = DynamicSchema::Builder.new do
192
+ schema = DynamicSchema.define do
193
193
  api_key, String
194
194
  chat_options do
195
195
  model String, default: 'claude-3'
@@ -236,7 +236,7 @@ the value. If you want to specify multiple types simply provide an array of type
236
236
  ```ruby
237
237
  require 'dynamic_schema'
238
238
 
239
- schema = DynamicSchema::Builder.new do
239
+ schema = DynamicSchema.define do
240
240
  typeless_value
241
241
  symbol_value Symbol
242
242
  boolean_value [ TrueClass, FalseClass ]
@@ -424,16 +424,30 @@ their definition and construction.
424
424
 
425
425
  ### Definable
426
426
 
427
- The `Definable` module, when inclued in a class, will add a `schema` class method to your class.
428
- You can call `schema` with a block and define a schema directly inside your class. The `schema`
429
- method can be called repeatedly, with subsequent calls augmenting, any preexsiting schema.
427
+ The `Definable` module, when inclued in a class, will add the `schema` and the `builder` class
428
+ methods.
430
429
 
431
- This can be used in a class hierarchy to augment base class schemas in derived classes.
430
+ By calling `schema` with a block you can define a schema for that specific class. You may also
431
+ retrieve the defined schema by calling 'schema' ( with or without a block ). The 'schema' method
432
+ may be called repeatedly to build up a schema with each call adding to the existing schema
433
+ ( replacing values and objects of the same name if they appear in subsequent calls ).
432
434
 
433
- ```
434
- class DatabaSettings
435
+ The `schema` method will integrate with a class hierarchy. By including Definable in a base class
436
+ you can call `schema` to define a schema for that base class and then in subsequent dervied classes
437
+ to augment it for those classes.
438
+
439
+ The `builder` method will return a memoized builder of the schema defined by calls to the `schema`
440
+ method which can be used to build and validate schema conformant hashes.
441
+
442
+ ```ruby
443
+ class Setting
435
444
  include DynamicSchema::Definable
436
-
445
+ schema do
446
+ name String
447
+ end
448
+ end
449
+
450
+ class DatabaSetting < Setting
437
451
  schema do
438
452
  database do
439
453
  host String
@@ -444,12 +458,9 @@ class DatabaSettings
444
458
 
445
459
  def initalize( attributes = {} )
446
460
  # validate the attributes
447
- schema_builder = DynamicSchema::Builder.new.define( &class.schema )
448
- schema_builder.validate!( attributes )
449
- # initialize from the given attributes here
450
- @host = attributes[ :database ][ :host ]
451
- @port = attributes[ :database ][ :port ]
452
- @name = attributes[ :database ][ :name ]
461
+ self.class.builder.validate!( attributes )
462
+ # retain them for future access
463
+ @attributes = attributes&.dup
453
464
  end
454
465
 
455
466
  end
@@ -462,19 +473,25 @@ building that class using a schema assisted builder pattern. The `Buildable` mod
462
473
  `build!` and `build` methods to the class which can be used to build that class, with and
463
474
  without validation respectivelly.
464
475
 
465
- These methods accept both a hash with attitbutes that follow the schema, as well as a block
476
+ These methods accept both a hash with attributes that follow the schema, as well as a block
466
477
  that can be used to build the class instance. The attributes and block can be used simultanously.
467
478
 
468
- **Important** Note that `Buildable` requires that the initializer accept a `Hash` of attributes.
479
+ **Important** Note that `Buildable` requires a class method `builder` ( which `Definable`
480
+ provides ) and an initializer that accepts a `Hash` of attributes.
469
481
 
470
- ```
471
- class DatabaSettings
482
+ ```ruby
483
+ class Setting
472
484
  include DynamicSchema::Definable
473
485
  include DynamicSchema::Buildable
474
-
486
+ schema do
487
+ name String
488
+ end
489
+ end
490
+
491
+ class DatabaSetting < Setting
475
492
  schema do
476
493
  database do
477
- adapter Symbol
494
+ adapter Symbol,
478
495
  host String
479
496
  port String
480
497
  name String
@@ -482,18 +499,20 @@ class DatabaSettings
482
499
  end
483
500
 
484
501
  def initalize( attributes = {} )
485
- # assign the attributes
486
- # ...
502
+ # validate the attributes
503
+ self.class.builder.validate!( attributes )
504
+ # retain them for the future
505
+ @attributes = attributes&.dup
487
506
  end
488
-
489
507
  end
490
508
 
491
- database_settings = DatabaSettings.build! adapter: :pg do
492
- host "localhost"
493
- port "127.0.0.1"
494
- name "mydb"
509
+ database_settings = DatabaSettings.build! name: 'settings.database' do
510
+ database adapter: :pg do
511
+ host "localhost"
512
+ port "127.0.0.1"
513
+ name "mydb"
514
+ end
495
515
  end
496
-
497
516
  ```
498
517
 
499
518
  ## Validation
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do | spec |
2
2
 
3
3
  spec.name = 'dynamicschema'
4
- spec.version = '1.0.0.beta02'
4
+ spec.version = '1.0.0.beta03'
5
5
  spec.authors = [ 'Kristoph Cichocki-Romanov' ]
6
6
  spec.email = [ 'rubygems.org@kristoph.net' ]
7
7
 
@@ -8,13 +8,11 @@ module DynamicSchema
8
8
  module ClassMethods
9
9
 
10
10
  def build( attributes = nil, &block )
11
- @_builder ||= Builder.new.define( &self.schema )
12
- new( @_builder.build( attributes, &block ) )
11
+ new( builder.build( attributes, &block ) )
13
12
  end
14
13
 
15
14
  def build!( attributes = nil, &block )
16
- @_builder ||= Builder.new.define( &self.schema )
17
- new( @_builder.build!( attributes, &block ) )
15
+ new( builder.build!( attributes, &block ) )
18
16
  end
19
17
 
20
18
  end
@@ -8,8 +8,15 @@ module DynamicSchema
8
8
  module ClassMethods
9
9
 
10
10
  def schema( &block )
11
- @_schema ||= []
12
- @_schema << block if block_given?
11
+ @_schema ||= []
12
+ if block_given?
13
+ # note that the memoized builder is reset when schema is called with a new block so
14
+ # that additions to the schema are incorporated into future builder ( but this does
15
+ # not work if the schema is updated on a superclass after this class' builder has
16
+ # been returned )
17
+ @_builder = nil
18
+ @_schema << block
19
+ end
13
20
  schema_blocks = _collect_schema
14
21
  proc do
15
22
  schema_blocks.each do | block |
@@ -18,6 +25,10 @@ module DynamicSchema
18
25
  end
19
26
  end
20
27
 
28
+ def builder
29
+ @_builder ||= DynamicSchema.define( &schema )
30
+ end
31
+
21
32
  protected
22
33
 
23
34
  def _collect_schema
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dynamicschema
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.beta02
4
+ version: 1.0.0.beta03
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kristoph Cichocki-Romanov