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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1ebe87803e47220d1a3f6396a1735fd2aa04b3f2b1428bea43cd873cb32f3bca
4
- data.tar.gz: ecdaaa2801742335160a89a38a92ef9b402be563d3ce0e54c52f3068d75c93ce
3
+ metadata.gz: 8455d741b05a1a65ca7ea3f7e2d529271414fb04881066cc81bbb444399e200c
4
+ data.tar.gz: 9814e429530dbb42da5a3cca3310175ebe98038d91d5b7deeddcd68aa90b3199
5
5
  SHA512:
6
- metadata.gz: 866e44d124957d2149bd67584c0e35129bd922c98da0bb9d4b37cead66636d84b508290be35b4ce79ad3940bd556585ee604aeb79c220f898450624765e3fd50
7
- data.tar.gz: 551cd5c6f3de517efd56fadb407fd723de14764d3520b7be7ee73e12a7027d391816452ad13a00752e0fa690d5da2eef4aff2d0b12b585ed686eeb31635875ac
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)
@@ -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.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EasyTalk
4
- VERSION = '0.1.10'
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.10
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-30 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