easy_talk 0.1.9 → 0.2.0

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: cac3597b203d800e1c15c79ea019a6d81080dd5c5ed669dc96500e262a32a505
4
- data.tar.gz: 626bb67445db8ffa2d41df7a0b82ceba30db827ed0cb610f4d1b382d161a5191
3
+ metadata.gz: 8455d741b05a1a65ca7ea3f7e2d529271414fb04881066cc81bbb444399e200c
4
+ data.tar.gz: 9814e429530dbb42da5a3cca3310175ebe98038d91d5b7deeddcd68aa90b3199
5
5
  SHA512:
6
- metadata.gz: e58629e7e0b4aff314f04d0a0d14f3129e0af8b54c23c1d061d57464648aa97caaf2db514353fcf34713d1d14113eb13c599ccc28b6106a3c5f06cd4197671af
7
- data.tar.gz: f3ba1ec6c87ff2028a5bd26f22ebade4446717c8d9e27702c94f5e4080fd951885dd033a0d4f88078d44849cb7b251fc1dbb303f9da84b2a592bf85c1daaf9ef
6
+ metadata.gz: 5a85e59b3eaa7e0e9c2d57eb0283f2b4a45e6dfeb88778682738969617f751149aaf99cd93b4760c323eb84da476ac0ee8aba59eef003156d8d82aae60a940fd
7
+ data.tar.gz: 0c382d7989016e5a1c376439c96cd8b1d4acb7a6758a8fb2b406239551445ede641fb3a3ed64f833ce591409a75eefb0f3023a4a60cfffc92ca49ee5827bc24d
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
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
+
4
+ ## [0.1.10] - 2024-04-29
5
+ - Accept `:optional` key as constraint which excludes property from required node.
6
+ - Spec fixes
1
7
  ## [0.1.9] - 2024-04-29
2
8
  - Added the ability to describe an object schema withing the define_schema block. Example:
3
9
  ```ruby
@@ -11,7 +11,8 @@ module EasyTalk
11
11
  # representing schema properties.
12
12
  COMMON_OPTIONS = {
13
13
  title: { type: T.nilable(String), key: :title },
14
- description: { type: T.nilable(String), key: :description }
14
+ description: { type: T.nilable(String), key: :description },
15
+ optional: { type: T.nilable(T::Boolean), key: :optional }
15
16
  }.freeze
16
17
 
17
18
  attr_reader :name, :schema, :options
@@ -14,7 +14,7 @@ module EasyTalk
14
14
  properties: { type: T::Hash[T.any(Symbol, String), T.untyped], key: :properties },
15
15
  additional_properties: { type: T::Boolean, key: :additionalProperties },
16
16
  subschemas: { type: T::Array[T.untyped], key: :subschemas },
17
- required: { type: T::Array[Symbol], key: :required },
17
+ required: { type: T::Array[T.any(Symbol, String)], key: :required },
18
18
  defs: { type: T.untyped, key: :$defs },
19
19
  allOf: { type: T.untyped, key: :allOf },
20
20
  anyOf: { type: T.untyped, key: :anyOf },
@@ -41,11 +41,15 @@ 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
- return unless options.is_a?(Hash) && !(options[:type].respond_to?(:nilable?) && options[:type].nilable?)
46
+ return if options.is_a?(Hash) && !!(options[:type].respond_to?(:nilable?) && options[:type].nilable?)
47
+
48
+ return if options.respond_to?(:optional?) && options.optional?
46
49
 
47
50
  @required_properties << property_name
48
51
  end
52
+ # rubocop:enable Style/DoubleNegation
49
53
 
50
54
  def build_property(property_name, options)
51
55
  if options.is_a?(EasyTalk::SchemaDefinition)
@@ -15,7 +15,8 @@ module EasyTalk
15
15
  max_length: { type: Integer, key: :maxLength },
16
16
  enum: { type: T::Array[String], key: :enum },
17
17
  const: { type: String, key: :const },
18
- default: { type: String, key: :default }
18
+ default: { type: String, key: :default },
19
+ optional: { type: T::Boolean, key: :optional }
19
20
  }.freeze
20
21
 
21
22
  sig { params(name: Symbol, constraints: Hash).void }
@@ -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 MyModel
24
- # extend ClassMethods
24
+ # class Person
25
+ # include EasyTalk::Model
25
26
  #
26
27
  # define_schema do
27
- # # schema definition goes here
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
- # MyModel.json_schema #=> returns the JSON schema for MyModel
32
- #
33
- # MyModel.schema_definition #=> returns the unvalidated schema definition for MyModel
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.
@@ -16,8 +16,8 @@ module EasyTalk
16
16
 
17
17
  attr_reader :name, :schema
18
18
 
19
- def initialize(name)
20
- @schema = {}
19
+ def initialize(name, schema = {})
20
+ @schema = schema
21
21
  @name = name
22
22
  end
23
23
 
@@ -39,12 +39,16 @@ module EasyTalk
39
39
  @schema[:properties] ||= {}
40
40
 
41
41
  if block_given?
42
- property_schema = SchemaDefinition.new(name)
42
+ property_schema = SchemaDefinition.new(name, constraints)
43
43
  property_schema.instance_eval(&blk)
44
44
  @schema[:properties][name] = property_schema
45
45
  else
46
46
  @schema[:properties][name] = { type:, constraints: }
47
47
  end
48
48
  end
49
+
50
+ def optional?
51
+ @schema[:optional]
52
+ end
49
53
  end
50
54
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EasyTalk
4
- VERSION = '0.1.9'
4
+ VERSION = '0.2.0'
5
5
  end
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.1.9
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-04-29 00:00:00.000000000 Z
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
@@ -177,7 +191,6 @@ files:
177
191
  - LICENSE.txt
178
192
  - README.md
179
193
  - Rakefile
180
- - easy_talk.gemspec
181
194
  - lib/easy_talk.rb
182
195
  - lib/easy_talk/builders/all_of_builder.rb
183
196
  - lib/easy_talk/builders/any_of_builder.rb
data/easy_talk.gemspec DELETED
@@ -1,45 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'lib/easy_talk/version'
4
-
5
- Gem::Specification.new do |spec|
6
- spec.name = 'easy_talk'
7
- spec.version = EasyTalk::VERSION
8
- spec.authors = ['Sergio Bayona']
9
- spec.email = ['bayona.sergio@gmail.com']
10
-
11
- spec.summary = 'Generate json-schema from Ruby classes.'
12
- spec.description = 'Generate json-schema from plain Ruby classes.'
13
- spec.homepage = 'https://github.com/sergiobayona/easy_talk'
14
- spec.license = 'MIT'
15
- spec.required_ruby_version = '>= 3.2'
16
-
17
- spec.metadata['allowed_push_host'] = 'https://rubygems.org'
18
-
19
- spec.metadata['homepage_uri'] = spec.homepage
20
- spec.metadata['source_code_uri'] = 'https://github.com/sergiobayona/easy_talk'
21
- spec.metadata['changelog_uri'] = 'https://github.com/sergiobayona/easy_talk/blob/main/CHANGELOG.md'
22
-
23
- # Specify which files should be added to the gem when it is released.
24
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
- spec.files = Dir.chdir(__dir__) do
26
- `git ls-files -z`.split("\x0").reject do |f|
27
- (File.expand_path(f) == __FILE__) ||
28
- f.start_with?(*%w[bin/ spec/ .git .github Gemfile])
29
- end
30
- end
31
-
32
- spec.require_paths = ['lib']
33
-
34
- spec.add_dependency 'activesupport', '~> 7.0'
35
- spec.add_dependency 'json-schema', '~> 4'
36
- spec.add_dependency 'sorbet-runtime', '~> 0.5'
37
- spec.add_development_dependency 'pry-byebug', '>= 3.10'
38
- spec.add_development_dependency 'rake', '~> 13.1'
39
- spec.add_development_dependency 'rspec', '~> 3.0'
40
- spec.add_development_dependency 'rspec-json_expectations', '~> 2.0'
41
- spec.add_development_dependency 'rspec-mocks', '~> 3.13'
42
- spec.add_development_dependency 'rubocop', '~> 1.21'
43
- spec.add_development_dependency 'rubocop-rake', '~> 0.6'
44
- spec.add_development_dependency 'rubocop-rspec', '~> 2.29'
45
- end