easy_talk 0.1.6 → 0.1.7

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: ef35c90e8f4ecd74af7743e5c4e8503a3f1df9099c4395c5d8376d8b7be2f325
4
- data.tar.gz: 85e4bec40186ed896834b59707ad68a9ede133db9fe1c4da99570de416f8abb9
3
+ metadata.gz: f412e6a4ef70f8a8912def5e60e4f9a2fbb80a47c0f22e7f46af9caed9d153fa
4
+ data.tar.gz: ee7bfc2e77fef086bff23db293eec6f6d7f47ed47488a803737e2831895feb88
5
5
  SHA512:
6
- metadata.gz: 0a74dd4133b14bb81882224e06c97488be0ac1e85e4c3a52452c3ce396a52928aefc13de45a7e284c846ae8acc09e73043381bb46d9acad7cfcfdfcf5e290a45
7
- data.tar.gz: 0f78ef87354030469e5c9b8897331870a1232724bc33a2e6d0af7bffaf44617f86e8ecd4a44684955d964d9dd55fbdca5944a14699db49fc981d0803a31489a9
6
+ metadata.gz: feb1884f28b4c0400ccbe3fb651f2d87e4650a25180ed804bab8c7b2493ebc7f0d7fb769271788a7dc0e0e6cdc6c2793fb1cbaa064df881aef2d797242b70860
7
+ data.tar.gz: 1c2ac812a423169c0bc0d43afac4f86711acad7bbab28aeb40e249df65258be4cb845903edc585d43f835b2717b9fc4003817b0ae2c704d20a28ff583290f3c5
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ ## [0.1.7] - 2024-04-16
2
+ - general cleanup and refactoring.
3
+
1
4
  ## [0.1.6] - 2024-04-16
2
5
  - model instance takes a hash and converts it to attribute methods.
3
6
 
@@ -53,6 +53,10 @@ module EasyTalk
53
53
  obj[value[:key]] = T.let(@options[key], value[:type])
54
54
  end
55
55
  end
56
+
57
+ def self.collection_type?
58
+ false
59
+ end
56
60
  end
57
61
  end
58
62
  end
@@ -11,12 +11,11 @@ module EasyTalk
11
11
  # VALID_OPTIONS defines the valid options for a boolean property.
12
12
  VALID_OPTIONS = {
13
13
  enum: { type: T::Array[T::Boolean], key: :enum },
14
- const: { type: T::Boolean, key: :const },
15
14
  default: { type: T::Boolean, key: :default }
16
15
  }.freeze
17
16
 
18
- sig { params(name: Symbol, _type: T.untyped, constraints: Hash).void }
19
- def initialize(name, _type = nil, constraints = {})
17
+ sig { params(name: Symbol, constraints: Hash).void }
18
+ def initialize(name, constraints = {})
20
19
  super(name, { type: 'boolean' }, constraints, VALID_OPTIONS)
21
20
  end
22
21
  end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EasyTalk
4
+ module Builders
5
+ # Base builder class for array-type properties.
6
+ module CollectionHelpers
7
+ def collection_type?
8
+ true
9
+ end
10
+ end
11
+ end
12
+ end
@@ -1,11 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'base_builder'
3
+ require_relative 'collection_helpers'
4
4
 
5
5
  module EasyTalk
6
6
  module Builders
7
7
  # This class represents a builder for composing JSON schemas using the "allOf", "anyOf", or "oneOf" keywords.
8
8
  class CompositionBuilder
9
+ extend CollectionHelpers
9
10
  extend T::Sig
10
11
 
11
12
  COMPOSER_TO_KEYWORD = {
@@ -19,8 +19,8 @@ module EasyTalk
19
19
  }.freeze
20
20
 
21
21
  # Initializes a new instance of the IntegerBuilder class.
22
- sig { params(name: Symbol, _type: T.untyped, constraints: Hash).void }
23
- def initialize(name, _type = nil, constraints = {})
22
+ sig { params(name: Symbol, constraints: Hash).void }
23
+ def initialize(name, constraints = {})
24
24
  super(name, { type: 'integer' }, constraints, VALID_OPTIONS)
25
25
  end
26
26
  end
@@ -7,8 +7,8 @@ module EasyTalk
7
7
  # builder class for Null properties.
8
8
  class NullBuilder < BaseBuilder
9
9
  # Initializes a new instance of the NullBuilder class.
10
- sig { params(name: Symbol, _type: T.untyped, _constraints: Hash).void }
11
- def initialize(name, _type = nil, _constraints = {})
10
+ sig { params(name: Symbol, _constraints: Hash).void }
11
+ def initialize(name, _constraints = {})
12
12
  super(name, { type: 'null' }, {}, {})
13
13
  end
14
14
  end
@@ -18,8 +18,8 @@ module EasyTalk
18
18
  }.freeze
19
19
 
20
20
  # Initializes a new instance of the NumberBuilder class.
21
- sig { params(name: Symbol, _type: T.untyped, constraints: Hash).void }
22
- def initialize(name, _type = nil, constraints = {})
21
+ sig { params(name: Symbol, constraints: Hash).void }
22
+ def initialize(name, constraints = {})
23
23
  super(name, { type: 'number' }, constraints, VALID_OPTIONS)
24
24
  end
25
25
  end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'base_builder'
4
+
3
5
  module EasyTalk
4
6
  module Builders
5
7
  # Builder class for json schema objects.
@@ -18,8 +18,8 @@ module EasyTalk
18
18
  default: { type: String, key: :default }
19
19
  }.freeze
20
20
 
21
- sig { params(name: Symbol, _type: T.untyped, constraints: Hash).void }
22
- def initialize(name, _type = nil, constraints = {})
21
+ sig { params(name: Symbol, constraints: Hash).void }
22
+ def initialize(name, constraints = {})
23
23
  super(name, { type: 'string' }, constraints, VALID_OPTIONS)
24
24
  end
25
25
  end
@@ -1,9 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'collection_helpers'
4
+
3
5
  module EasyTalk
4
6
  module Builders
5
7
  # Builder class for array properties.
6
8
  class TypedArrayBuilder < BaseBuilder
9
+ extend CollectionHelpers
10
+ extend T::Sig
11
+
7
12
  VALID_OPTIONS = {
8
13
  min_items: { type: Integer, key: :minItems },
9
14
  max_items: { type: Integer, key: :maxItems },
@@ -1,12 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'base_builder'
3
+ require_relative 'collection_helpers'
4
4
 
5
5
  module EasyTalk
6
6
  module Builders
7
7
  # Base builder class for array-type properties.
8
8
  class UnionBuilder
9
+ extend CollectionHelpers
9
10
  extend T::Sig
11
+
10
12
  sig { params(name: Symbol, type: T.untyped, constraints: T.untyped).void }
11
13
  def initialize(name, type, constraints)
12
14
  @name = name
@@ -7,54 +7,14 @@ require 'active_support/time'
7
7
  require 'active_support/concern'
8
8
  require 'active_support/json'
9
9
  require 'json-schema'
10
- require_relative 'builder'
10
+ require_relative 'builders/object_builder'
11
11
  require_relative 'schema_definition'
12
12
 
13
13
  module EasyTalk
14
14
  # The Model module can be included in a class to add JSON schema definition and generation support.
15
15
  module Model
16
- def self.included(base)
17
- base.extend(ClassMethods)
18
-
19
- base.singleton_class.instance_eval do
20
- define_method(:inherited) do |subclass|
21
- super(subclass)
22
- subclass.extend(SubclassExtension)
23
- end
24
- end
25
- end
26
-
27
- def initialize(properties = {})
28
- properties.each do |key, value|
29
- instance_variable_set("@#{key}", value)
30
- self.class.class_eval { attr_reader key }
31
- end
32
- end
33
-
34
- def valid?
35
- self.class.validate_json(properties)
36
- end
37
-
38
- def properties
39
- as_json.symbolize_keys!
40
- end
41
-
42
- # This module provides extension methods for subclasses with schema definitions.
43
- module SubclassExtension
44
- # Returns true if the class inherits a schema.
45
- def inherits_schema?
46
- true
47
- end
48
-
49
- # Returns the superclass that the class inherits from.
50
- def inherits_from
51
- superclass
52
- end
53
- end
54
-
55
- # Module containing class-level methods for defining and accessing the schema of a model.
16
+ # The `Model` module is a mixin that provides functionality for defining and accessing the schema of a model.
56
17
  #
57
- # This module provides methods for defining and accessing the JSON schema of a model.
58
18
  # It includes methods for defining the schema, retrieving the schema definition,
59
19
  # and generating the JSON schema for the model.
60
20
  #
@@ -83,37 +43,85 @@ module EasyTalk
83
43
  # MyModel.json_schema #=> returns the JSON schema for MyModel
84
44
  #
85
45
  # @see SchemaDefinition
86
- # @see Builder
46
+ #
47
+ def self.included(base)
48
+ base.extend(ClassMethods)
49
+ end
50
+
51
+ # Initializes a new instance of the Model class.
52
+ #
53
+ # @param properties [Hash] The properties to set for the instance.
54
+ def initialize(properties = {})
55
+ properties.each do |key, value|
56
+ instance_variable_set("@#{key}", value)
57
+ self.class.class_eval { attr_reader key }
58
+ end
59
+ end
60
+
61
+ # Checks if the model is valid.
62
+ #
63
+ # This method calls the `validate_json` class method on the current class,
64
+ # passing the `properties` as the argument.
65
+ #
66
+ # @return [Boolean] true if the model is valid, false otherwise.
67
+ def valid?
68
+ self.class.validate_json(properties)
69
+ end
70
+
71
+ # Returns the properties of the model as a hash with symbolized keys.
72
+ def properties
73
+ as_json.symbolize_keys!
74
+ end
75
+
76
+ # Module containing class-level methods for defining and accessing the schema of a model.
87
77
  module ClassMethods
78
+ # Returns the schema for the model.
79
+ #
80
+ # @return [Schema] The schema for the model.
88
81
  def schema
89
- @schema ||= Builder.new(schema_definition).schema
82
+ @schema ||= build_schema(schema_definition)
90
83
  end
91
84
 
85
+ # Returns true if the class inherits a schema.
86
+ #
87
+ # @return [Boolean] `true` if the class inherits a schema, `false` otherwise.
92
88
  def inherits_schema?
93
89
  false
94
90
  end
95
91
 
92
+ # Returns the reference template for the model.
93
+ #
94
+ # @return [String] The reference template for the model.
96
95
  def ref_template
97
96
  "#/$defs/#{name}"
98
97
  end
99
98
 
99
+ # Returns the name of the model as a human-readable function name.
100
+ #
101
+ # @return [String] The human-readable function name of the model.
100
102
  def function_name
101
103
  name.humanize.titleize
102
104
  end
103
105
 
106
+ # Validates the given JSON against the model's JSON schema.
107
+ #
108
+ # @param json [Hash] The JSON to validate.
109
+ # @return [Boolean] `true` if the JSON is valid, `false` otherwise.
104
110
  def validate_json(json)
105
111
  JSON::Validator.validate(json_schema, json)
106
112
  end
107
113
 
108
114
  # Returns the JSON schema for the model.
115
+ #
116
+ # @return [Hash] The JSON schema for the model.
109
117
  def json_schema
110
- @json_schema ||= begin
111
- schema = Builder.new(schema_definition).schema
112
- schema.as_json
113
- end
118
+ @json_schema ||= schema.as_json
114
119
  end
115
120
 
116
- # Define the schema using the provided block.
121
+ # Define the schema for the model using the provided block.
122
+ #
123
+ # @yield The block to define the schema.
124
+ # @raise [ArgumentError] If the class does not have a name.
117
125
  def define_schema(&block)
118
126
  raise ArgumentError, 'The class must have a name' unless name.present?
119
127
 
@@ -121,9 +129,22 @@ module EasyTalk
121
129
  @schema_definition.instance_eval(&block)
122
130
  end
123
131
 
132
+ # Returns the unvalidated schema definition for the model.
133
+ #
134
+ # @return [SchemaDefinition] The unvalidated schema definition for the model.
124
135
  def schema_definition
125
136
  @schema_definition ||= {}
126
137
  end
138
+
139
+ private
140
+
141
+ # Builds the schema using the provided schema definition.
142
+ #
143
+ # @param schema_definition [SchemaDefinition] The schema definition.
144
+ # @return [Schema] The validated schema.
145
+ def build_schema(schema_definition)
146
+ Builders::ObjectBuilder.new(schema_definition).build
147
+ end
127
148
  end
128
149
  end
129
150
  end
@@ -57,13 +57,12 @@ module EasyTalk
57
57
  end
58
58
 
59
59
  def build
60
- if type.is_a?(T::Types::Simple)
61
- @type = type.raw_type
62
- return self
63
- end
64
-
65
60
  if builder
66
- builder.new(name, type, constraints).build
61
+ if builder.collection_type?
62
+ builder.new(name, type, constraints).build
63
+ else
64
+ builder.new(name, constraints).build
65
+ end
67
66
  else
68
67
  type.respond_to?(:schema) ? type.schema : 'object'
69
68
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module EasyTalk
2
4
  module Tools
3
5
  module FunctionBuilder
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EasyTalk
4
- VERSION = '0.1.6'
4
+ VERSION = '0.1.7'
5
5
  end
data/lib/easy_talk.rb CHANGED
@@ -9,7 +9,6 @@ module EasyTalk
9
9
  require 'easy_talk/types/all_of'
10
10
  require 'easy_talk/types/one_of'
11
11
  require 'easy_talk/model'
12
- require 'easy_talk/builder'
13
12
  require 'easy_talk/property'
14
13
  require 'easy_talk/schema_definition'
15
14
  require 'easy_talk/tools/function_builder'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_talk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sergio Bayona
@@ -95,11 +95,11 @@ files:
95
95
  - Rakefile
96
96
  - easy_talk.gemspec
97
97
  - lib/easy_talk.rb
98
- - lib/easy_talk/builder.rb
99
98
  - lib/easy_talk/builders/all_of_builder.rb
100
99
  - lib/easy_talk/builders/any_of_builder.rb
101
100
  - lib/easy_talk/builders/base_builder.rb
102
101
  - lib/easy_talk/builders/boolean_builder.rb
102
+ - lib/easy_talk/builders/collection_helpers.rb
103
103
  - lib/easy_talk/builders/composition_builder.rb
104
104
  - lib/easy_talk/builders/date_builder.rb
105
105
  - lib/easy_talk/builders/datetime_builder.rb
@@ -108,7 +108,6 @@ files:
108
108
  - lib/easy_talk/builders/number_builder.rb
109
109
  - lib/easy_talk/builders/object_builder.rb
110
110
  - lib/easy_talk/builders/one_of_builder.rb
111
- - lib/easy_talk/builders/ref_array_builder.rb
112
111
  - lib/easy_talk/builders/string_builder.rb
113
112
  - lib/easy_talk/builders/time_builder.rb
114
113
  - lib/easy_talk/builders/typed_array_builder.rb
@@ -1,51 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'property'
4
- require_relative 'builders/object_builder'
5
-
6
- module EasyTalk
7
- # The Builder class is responsible for building a schema for a class.
8
- class Builder
9
- extend T::Sig
10
-
11
- sig { params(schema_definition: SchemaDefinition).void }
12
- # Initializes a new instance of the Builder class.
13
- #
14
- # @param schema_definition [SchemaDefinition] The schema definition.
15
- def initialize(schema_definition)
16
- @schema_definition = schema_definition
17
- end
18
-
19
- sig { returns(Hash) }
20
- # Retrieves the schema document.
21
- #
22
- # @return [Hash] The schema document.
23
- def schema
24
- @schema = schema_document
25
- end
26
-
27
- sig { returns(String) }
28
- # Returns the JSON representation of the schema document.
29
- #
30
- # @return [String] The JSON schema.
31
- def json_schema
32
- @json_schema ||= schema_document.to_json
33
- end
34
-
35
- sig { returns(Hash) }
36
- # Returns the schema document, building it if necessary.
37
- #
38
- # @return [Hash] The schema document.
39
- def schema_document
40
- @schema_document ||= build_schema
41
- end
42
-
43
- sig { returns(Hash) }
44
- # Builds the schema using the schema definition.
45
- #
46
- # Returns the built schema.
47
- def build_schema
48
- Builders::ObjectBuilder.new(@schema_definition).build
49
- end
50
- end
51
- end
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'base_builder'
4
-
5
- module EasyTalk
6
- module Builders
7
- # Builder class for array properties.
8
- class RefArrayBuilder < BaseBuilder
9
- # Initializes a new instance of the ArrayBuilder class.
10
- sig { params(name: Symbol).void }
11
- def initialize(name)
12
- super(name, { type: 'array' }, options, {})
13
- end
14
-
15
- private
16
-
17
- sig { void }
18
- # Updates the option types for the array builder.
19
- def update_option_types
20
- VALID_OPTIONS[:enum][:type] = T::Array[@inner_type]
21
- VALID_OPTIONS[:const][:type] = T::Array[@inner_type]
22
- end
23
- end
24
- end
25
- end