easy_talk 0.1.5 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -0
- data/lib/easy_talk/builders/base_builder.rb +4 -0
- data/lib/easy_talk/builders/boolean_builder.rb +2 -3
- data/lib/easy_talk/builders/collection_helpers.rb +12 -0
- data/lib/easy_talk/builders/composition_builder.rb +2 -1
- data/lib/easy_talk/builders/integer_builder.rb +2 -2
- data/lib/easy_talk/builders/null_builder.rb +2 -2
- data/lib/easy_talk/builders/number_builder.rb +2 -2
- data/lib/easy_talk/builders/object_builder.rb +2 -0
- data/lib/easy_talk/builders/string_builder.rb +2 -2
- data/lib/easy_talk/builders/typed_array_builder.rb +5 -0
- data/lib/easy_talk/builders/union_builder.rb +3 -1
- data/lib/easy_talk/model.rb +70 -34
- data/lib/easy_talk/property.rb +5 -6
- data/lib/easy_talk/tools/function_builder.rb +2 -0
- data/lib/easy_talk/version.rb +1 -1
- data/lib/easy_talk.rb +0 -9
- metadata +3 -4
- data/lib/easy_talk/builder.rb +0 -51
- data/lib/easy_talk/builders/ref_array_builder.rb +0 -25
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f412e6a4ef70f8a8912def5e60e4f9a2fbb80a47c0f22e7f46af9caed9d153fa
|
4
|
+
data.tar.gz: ee7bfc2e77fef086bff23db293eec6f6d7f47ed47488a803737e2831895feb88
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: feb1884f28b4c0400ccbe3fb651f2d87e4650a25180ed804bab8c7b2493ebc7f0d7fb769271788a7dc0e0e6cdc6c2793fb1cbaa064df881aef2d797242b70860
|
7
|
+
data.tar.gz: 1c2ac812a423169c0bc0d43afac4f86711acad7bbab28aeb40e249df65258be4cb845903edc585d43f835b2717b9fc4003817b0ae2c704d20a28ff583290f3c5
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,12 @@
|
|
1
|
+
## [0.1.7] - 2024-04-16
|
2
|
+
- general cleanup and refactoring.
|
3
|
+
|
4
|
+
## [0.1.6] - 2024-04-16
|
5
|
+
- model instance takes a hash and converts it to attribute methods.
|
6
|
+
|
7
|
+
## [0.1.5] - 2024-04-15
|
8
|
+
- Added helper method for generating an openai function.
|
9
|
+
|
1
10
|
## [0.1.4] - 2024-04-12
|
2
11
|
- Bumped activesupport gem version.
|
3
12
|
|
@@ -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,
|
19
|
-
def initialize(name,
|
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
|
@@ -1,11 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative '
|
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,
|
23
|
-
def initialize(name,
|
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,
|
11
|
-
def initialize(name,
|
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,
|
22
|
-
def initialize(name,
|
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
|
@@ -18,8 +18,8 @@ module EasyTalk
|
|
18
18
|
default: { type: String, key: :default }
|
19
19
|
}.freeze
|
20
20
|
|
21
|
-
sig { params(name: Symbol,
|
22
|
-
def initialize(name,
|
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 '
|
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
|
data/lib/easy_talk/model.rb
CHANGED
@@ -7,39 +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 '
|
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
|
-
|
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
|
-
# This module provides extension methods for subclasses with schema definitions.
|
28
|
-
module SubclassExtension
|
29
|
-
# Returns true if the class inherits a schema.
|
30
|
-
def inherits_schema?
|
31
|
-
true
|
32
|
-
end
|
33
|
-
|
34
|
-
# Returns the superclass that the class inherits from.
|
35
|
-
def inherits_from
|
36
|
-
superclass
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
# 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.
|
41
17
|
#
|
42
|
-
# This module provides methods for defining and accessing the JSON schema of a model.
|
43
18
|
# It includes methods for defining the schema, retrieving the schema definition,
|
44
19
|
# and generating the JSON schema for the model.
|
45
20
|
#
|
@@ -68,37 +43,85 @@ module EasyTalk
|
|
68
43
|
# MyModel.json_schema #=> returns the JSON schema for MyModel
|
69
44
|
#
|
70
45
|
# @see SchemaDefinition
|
71
|
-
#
|
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.
|
72
77
|
module ClassMethods
|
78
|
+
# Returns the schema for the model.
|
79
|
+
#
|
80
|
+
# @return [Schema] The schema for the model.
|
73
81
|
def schema
|
74
|
-
@schema ||=
|
82
|
+
@schema ||= build_schema(schema_definition)
|
75
83
|
end
|
76
84
|
|
85
|
+
# Returns true if the class inherits a schema.
|
86
|
+
#
|
87
|
+
# @return [Boolean] `true` if the class inherits a schema, `false` otherwise.
|
77
88
|
def inherits_schema?
|
78
89
|
false
|
79
90
|
end
|
80
91
|
|
92
|
+
# Returns the reference template for the model.
|
93
|
+
#
|
94
|
+
# @return [String] The reference template for the model.
|
81
95
|
def ref_template
|
82
96
|
"#/$defs/#{name}"
|
83
97
|
end
|
84
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.
|
85
102
|
def function_name
|
86
103
|
name.humanize.titleize
|
87
104
|
end
|
88
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.
|
89
110
|
def validate_json(json)
|
90
111
|
JSON::Validator.validate(json_schema, json)
|
91
112
|
end
|
92
113
|
|
93
114
|
# Returns the JSON schema for the model.
|
115
|
+
#
|
116
|
+
# @return [Hash] The JSON schema for the model.
|
94
117
|
def json_schema
|
95
|
-
@json_schema ||=
|
96
|
-
schema = Builder.new(schema_definition).schema
|
97
|
-
schema.as_json
|
98
|
-
end
|
118
|
+
@json_schema ||= schema.as_json
|
99
119
|
end
|
100
120
|
|
101
|
-
# 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.
|
102
125
|
def define_schema(&block)
|
103
126
|
raise ArgumentError, 'The class must have a name' unless name.present?
|
104
127
|
|
@@ -106,9 +129,22 @@ module EasyTalk
|
|
106
129
|
@schema_definition.instance_eval(&block)
|
107
130
|
end
|
108
131
|
|
132
|
+
# Returns the unvalidated schema definition for the model.
|
133
|
+
#
|
134
|
+
# @return [SchemaDefinition] The unvalidated schema definition for the model.
|
109
135
|
def schema_definition
|
110
136
|
@schema_definition ||= {}
|
111
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
|
112
148
|
end
|
113
149
|
end
|
114
150
|
end
|
data/lib/easy_talk/property.rb
CHANGED
@@ -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.
|
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
|
data/lib/easy_talk/version.rb
CHANGED
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'
|
@@ -17,12 +16,4 @@ module EasyTalk
|
|
17
16
|
|
18
17
|
class UnsupportedTypeError < ArgumentError; end
|
19
18
|
class UnsupportedConstraintError < ArgumentError; end
|
20
|
-
|
21
|
-
def self.schemas
|
22
|
-
@schemas ||= {}
|
23
|
-
end
|
24
|
-
|
25
|
-
def self.add_schema(ref, schema)
|
26
|
-
schemas[ref] = schema
|
27
|
-
end
|
28
19
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: easy_talk
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sergio Bayona
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-04-
|
11
|
+
date: 2024-04-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -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
|
data/lib/easy_talk/builder.rb
DELETED
@@ -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
|