dynamicschema 1.0.0.beta01 → 1.0.0.beta02

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 32cb8398626d7ac822a228c89778e566ef62182b054ab267b8edf45bc718eda3
4
- data.tar.gz: 3475b1a090f57e4702076677b426300c892d91a06ac55841cd6e48657f69d7aa
3
+ metadata.gz: 0251af8d67cb7dbbaeea74a21dd72dd2385e2449d4aa41c48753f4b05428dfed
4
+ data.tar.gz: ce213906df2fe4facdf469717c61f5138aa3cae7469ba0ac3d5dc21e6dadd958
5
5
  SHA512:
6
- metadata.gz: a39f196fc0f7e3a657a6e776163ac5d1d90d42ff8cba556b99691074dead341d42bc9e8230cfab4ece6fd81617540a09462278ce4d5fbca97eadec218ee09902
7
- data.tar.gz: bd2c0f880a7c088ccecd5df2574acf32e6242dacac8b730d44141d9b95f334828c0b39c860a24f7abd7e815eaf82c97499119c95664f565b557f80688ad955ac
6
+ metadata.gz: 48b889c8c37b1a454819a5ea8de9d63179348b7cede7ce043f0972bef384012cb9ed3a4aaabe8a739f3d51e21bee0f3315231fc5ea6b80cab48c945f3ac17919
7
+ data.tar.gz: 3b317ae13cfb0d558fe8f77267136e2132842780f39331ccf192e7f5b65d397ea1ac8bb3fa8d615f38afb9c1adf09a227a689921d63237525b8f6fc04b040cfa
data/README.md CHANGED
@@ -57,6 +57,9 @@ You can find a full OpenAI request example in the `/examples` folder of this rep
57
57
  - [as Option](#as-option)
58
58
  - [in Option (Values Only)](#in-option)
59
59
  - [arguments Option](#arguments-option)
60
+ - [Class Schema](#class-schemas)
61
+ - [Definable](#definable)
62
+ - [Buildable](#buildable)
60
63
  - [Validation Methods](#validation-methods)
61
64
  - [Validation Rules](#validation-rules)
62
65
  - [validate!](#validate)
@@ -414,6 +417,85 @@ result = schema.build! do
414
417
  end
415
418
  ```
416
419
 
420
+ ## Class Schemas
421
+
422
+ DynamicSchema provides a number of modules you can include into your own classes to simplify
423
+ their definition and construction.
424
+
425
+ ### Definable
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.
430
+
431
+ This can be used in a class hierarchy to augment base class schemas in derived classes.
432
+
433
+ ```
434
+ class DatabaSettings
435
+ include DynamicSchema::Definable
436
+
437
+ schema do
438
+ database do
439
+ host String
440
+ port String
441
+ name String
442
+ end
443
+ end
444
+
445
+ def initalize( attributes = {} )
446
+ # 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 ]
453
+ end
454
+
455
+ end
456
+ ```
457
+
458
+ ### Buildable
459
+
460
+ The `Buildable` module can be included in a class, in addition to `Definable` to faciliate
461
+ building that class using a schema assisted builder pattern. The `Buildable` module adds
462
+ `build!` and `build` methods to the class which can be used to build that class, with and
463
+ without validation respectivelly.
464
+
465
+ These methods accept both a hash with attitbutes that follow the schema, as well as a block
466
+ that can be used to build the class instance. The attributes and block can be used simultanously.
467
+
468
+ **Important** Note that `Buildable` requires that the initializer accept a `Hash` of attributes.
469
+
470
+ ```
471
+ class DatabaSettings
472
+ include DynamicSchema::Definable
473
+ include DynamicSchema::Buildable
474
+
475
+ schema do
476
+ database do
477
+ adapter Symbol
478
+ host String
479
+ port String
480
+ name String
481
+ end
482
+ end
483
+
484
+ def initalize( attributes = {} )
485
+ # assign the attributes
486
+ # ...
487
+ end
488
+
489
+ end
490
+
491
+ database_settings = DatabaSettings.build! adapter: :pg do
492
+ host "localhost"
493
+ port "127.0.0.1"
494
+ name "mydb"
495
+ end
496
+
497
+ ```
498
+
417
499
  ## Validation
418
500
 
419
501
  DynamicSchema provides three different methods for validating Hash structures against your
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do | spec |
2
2
 
3
3
  spec.name = 'dynamicschema'
4
- spec.version = '1.0.0.beta01'
4
+ spec.version = '1.0.0.beta02'
5
5
  spec.authors = [ 'Kristoph Cichocki-Romanov' ]
6
6
  spec.email = [ 'rubygems.org@kristoph.net' ]
7
7
 
@@ -0,0 +1,24 @@
1
+ module DynamicSchema
2
+ module Buildable
3
+
4
+ def self.included( base )
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+
10
+ def build( attributes = nil, &block )
11
+ @_builder ||= Builder.new.define( &self.schema )
12
+ new( @_builder.build( attributes, &block ) )
13
+ end
14
+
15
+ def build!( attributes = nil, &block )
16
+ @_builder ||= Builder.new.define( &self.schema )
17
+ new( @_builder.build!( attributes, &block ) )
18
+ end
19
+
20
+ end
21
+
22
+ end
23
+ end
24
+
@@ -0,0 +1,36 @@
1
+ module DynamicSchema
2
+ module Definable
3
+
4
+ def self.included( base )
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+
10
+ def schema( &block )
11
+ @_schema ||= []
12
+ @_schema << block if block_given?
13
+ schema_blocks = _collect_schema
14
+ proc do
15
+ schema_blocks.each do | block |
16
+ instance_eval( &block )
17
+ end
18
+ end
19
+ end
20
+
21
+ protected
22
+
23
+ def _collect_schema
24
+ schema_blocks = []
25
+ if superclass.singleton_methods.include?( :_collect_schema )
26
+ schema_blocks.concat( superclass._collect_schema )
27
+ end
28
+ schema_blocks.concat( @_schema ) if defined?( @_schema )
29
+ schema_blocks
30
+ end
31
+
32
+ end
33
+
34
+ end
35
+ end
36
+
@@ -103,7 +103,9 @@ module DynamicSchema
103
103
  @defaults_assigned[ method ] = false
104
104
  @values[ name ] = value
105
105
  else
106
- super
106
+ ::Kernel.raise ::NoMethodError,
107
+ "There is no schema value or object '#{method}' defined in this scope which includes: " \
108
+ "#{@schema.keys.join( ', ' )}."
107
109
  end
108
110
  end
109
111
 
@@ -1,6 +1,8 @@
1
1
  require_relative 'dynamic_schema/errors'
2
2
  require_relative 'dynamic_schema/builder'
3
- require_relative 'dynamic_schema/definition'
3
+
4
+ require_relative 'dynamic_schema/definable'
5
+ require_relative 'dynamic_schema/buildable'
4
6
 
5
7
  module DynamicSchema
6
8
  def self.define( schema = {}, &block )
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dynamicschema
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.beta01
4
+ version: 1.0.0.beta02
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kristoph Cichocki-Romanov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-10-25 00:00:00.000000000 Z
11
+ date: 2024-10-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -55,10 +55,11 @@ files:
55
55
  - README.md
56
56
  - dynamicschema.gemspec
57
57
  - lib/dynamic_schema.rb
58
+ - lib/dynamic_schema/buildable.rb
58
59
  - lib/dynamic_schema/builder.rb
59
60
  - lib/dynamic_schema/builder_methods/conversion.rb
60
61
  - lib/dynamic_schema/builder_methods/validation.rb
61
- - lib/dynamic_schema/definition.rb
62
+ - lib/dynamic_schema/definable.rb
62
63
  - lib/dynamic_schema/errors.rb
63
64
  - lib/dynamic_schema/receiver.rb
64
65
  - lib/dynamic_schema/resolver.rb
@@ -1,23 +0,0 @@
1
- require_relative 'builder'
2
-
3
- module DynamicSchema
4
- module Definition
5
-
6
- def schema( schema = nil, &block )
7
- return @_schema_builder if ( schema.nil? || schema.empty? ) && !block
8
- @_schema_builder = DynamicSchema::Builder.new( schema ).define( &block )
9
- end
10
-
11
- def build_with_schema( attributes = nil, &block )
12
- raise RuntimeError, "The schema has not been defined." if @_schema_builder.nil?
13
- @_schema_builder.build( attributes, &block )
14
- end
15
-
16
- def build_with_schema!( attributes = nil, &block )
17
- raise RuntimeError, "The schema has not been defined." if @_schema_builder.nil?
18
- @_schema_builder.build!( attributes, &block )
19
- end
20
-
21
- end
22
- end
23
-