easy_talk 0.1.10 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +3 -0
- data/lib/easy_talk/builders/object_builder.rb +2 -0
- data/lib/easy_talk/model.rb +20 -26
- data/lib/easy_talk/version.rb +1 -1
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8455d741b05a1a65ca7ea3f7e2d529271414fb04881066cc81bbb444399e200c
|
4
|
+
data.tar.gz: 9814e429530dbb42da5a3cca3310175ebe98038d91d5b7deeddcd68aa90b3199
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5a85e59b3eaa7e0e9c2d57eb0283f2b4a45e6dfeb88778682738969617f751149aaf99cd93b4760c323eb84da476ac0ee8aba59eef003156d8d82aae60a940fd
|
7
|
+
data.tar.gz: 0c382d7989016e5a1c376439c96cd8b1d4acb7a6758a8fb2b406239551445ede641fb3a3ed64f833ce591409a75eefb0f3023a4a60cfffc92ca49ee5827bc24d
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
## [0.2.0] - 2024-05-01
|
2
|
+
- Added ActiveModel::API functionality to EasyTalk::Model module. That means you get all the benefits of ActiveModel::API including attribute assignment, introspections, validations, translation (i18n) and more. See https://api.rubyonrails.org/classes/ActiveModel/API.html for more information.
|
3
|
+
|
1
4
|
## [0.1.10] - 2024-04-29
|
2
5
|
- Accept `:optional` key as constraint which excludes property from required node.
|
3
6
|
- Spec fixes
|
@@ -41,6 +41,7 @@ module EasyTalk
|
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
44
|
+
# rubocop:disable Style/DoubleNegation
|
44
45
|
def add_required_property(property_name, options)
|
45
46
|
return if options.is_a?(Hash) && !!(options[:type].respond_to?(:nilable?) && options[:type].nilable?)
|
46
47
|
|
@@ -48,6 +49,7 @@ module EasyTalk
|
|
48
49
|
|
49
50
|
@required_properties << property_name
|
50
51
|
end
|
52
|
+
# rubocop:enable Style/DoubleNegation
|
51
53
|
|
52
54
|
def build_property(property_name, options)
|
53
55
|
if options.is_a?(EasyTalk::SchemaDefinition)
|
data/lib/easy_talk/model.rb
CHANGED
@@ -6,6 +6,7 @@ require 'active_support/core_ext'
|
|
6
6
|
require 'active_support/time'
|
7
7
|
require 'active_support/concern'
|
8
8
|
require 'active_support/json'
|
9
|
+
require 'active_model'
|
9
10
|
require 'json-schema'
|
10
11
|
require_relative 'builders/object_builder'
|
11
12
|
require_relative 'schema_definition'
|
@@ -20,44 +21,26 @@ module EasyTalk
|
|
20
21
|
#
|
21
22
|
# Example usage:
|
22
23
|
#
|
23
|
-
# class
|
24
|
-
#
|
24
|
+
# class Person
|
25
|
+
# include EasyTalk::Model
|
25
26
|
#
|
26
27
|
# define_schema do
|
27
|
-
#
|
28
|
+
# property :name, String, description: 'The person\'s name'
|
29
|
+
# property :age, Integer, description: 'The person\'s age'
|
28
30
|
# end
|
29
31
|
# end
|
30
32
|
#
|
31
|
-
#
|
32
|
-
#
|
33
|
-
#
|
34
|
-
#
|
35
|
-
# MyModel.ref_template #=> returns the reference template for MyModel
|
36
|
-
#
|
37
|
-
# MyModel.inherits_schema? #=> returns false
|
38
|
-
#
|
39
|
-
# MyModel.schema #=> returns the validated schema for MyModel
|
40
|
-
#
|
41
|
-
# MyModel.schema_definition #=> returns the unvalidated schema definition for MyModel
|
42
|
-
#
|
43
|
-
# MyModel.json_schema #=> returns the JSON schema for MyModel
|
33
|
+
# Person.json_schema #=> returns the JSON schema for Person
|
34
|
+
# jim = Person.new(name: 'Jim', age: 30)
|
35
|
+
# jim.valid? #=> returns true
|
44
36
|
#
|
45
37
|
# @see SchemaDefinition
|
46
38
|
#
|
47
39
|
def self.included(base)
|
40
|
+
base.include ActiveModel::API # Include ActiveModel::API in the class including EasyTalk::Model
|
48
41
|
base.extend(ClassMethods)
|
49
42
|
end
|
50
43
|
|
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
44
|
# Checks if the model is valid.
|
62
45
|
#
|
63
46
|
# This method calls the `validate_json` class method on the current class,
|
@@ -103,6 +86,14 @@ module EasyTalk
|
|
103
86
|
name.humanize.titleize
|
104
87
|
end
|
105
88
|
|
89
|
+
def properties
|
90
|
+
@properties ||= begin
|
91
|
+
return unless schema[:properties].present?
|
92
|
+
|
93
|
+
schema[:properties].keys.map(&:to_sym)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
106
97
|
# Validates the given JSON against the model's JSON schema.
|
107
98
|
#
|
108
99
|
# @param json [Hash] The JSON to validate.
|
@@ -127,6 +118,9 @@ module EasyTalk
|
|
127
118
|
|
128
119
|
@schema_definition = SchemaDefinition.new(name)
|
129
120
|
@schema_definition.instance_eval(&block)
|
121
|
+
attr_accessor(*properties)
|
122
|
+
|
123
|
+
@schema_defintion
|
130
124
|
end
|
131
125
|
|
132
126
|
# Returns the unvalidated schema definition for the model.
|
data/lib/easy_talk/version.rb
CHANGED
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: easy_talk
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
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-
|
11
|
+
date: 2024-05-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activemodel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '7.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '7.0'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: activesupport
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|