schema-model 0.5.6 → 0.6.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: 26adf1dd06c9423431abe3f91b7f3c62dd71621130b073f9084f04ed3a886d24
4
- data.tar.gz: 2897fd831fed4eb03297902846b166b023120328b41a10a1c56e6d3e701dcfd7
3
+ metadata.gz: 8fe2234ba4deb51ee1e9bb8310c8d20e0b399ac04555325881256236a3211eae
4
+ data.tar.gz: dafc2e746909a6c5d83994e07391ac9e22164ff8f9af15c4ddc1eb3cd262bcb9
5
5
  SHA512:
6
- metadata.gz: 48c3abb46f720e799c6a9d246adc691f3add9035e51edec112787e1d37e272f24106b0bfed240e9c8b250223019e2ed10bf1dc2e45cda161534fa3661f6fe838
7
- data.tar.gz: 88bbf251505a4ba24309a00dfc707737594a9f9dbc4afe574fceeda64da2c4300c6384361d5a6a936dc6182e6ebb84f9385b69e7a29c291aed2a011aaa4a3071
6
+ metadata.gz: ee6bca17f3ab5539f21de688c80f55e9e234e0d5e32e205c18ed6d9f75abc698997d92f84405cea0a361dc4bd9055cf32632765134bedbffc1f9f6b0cb87f90e
7
+ data.tar.gz: 7093b4d1dca9386a15d51557edac2b9b713157737fe78b8c447c8af432b53103007b7f0c0cf68c8d92fbb2c75ca0c0f74a758052ae1ad65350305676e1b31c64
data/README.md CHANGED
@@ -93,6 +93,15 @@ class CompanySchema
93
93
  validates :type, inclusion: { in: dynamic_type_names }
94
94
  end
95
95
 
96
+ has_many(:admins, from: :hash, hash_key_field: :username) do
97
+ attribute :username, :string
98
+ attribute :email, :string
99
+ attribute :name, :string
100
+
101
+ validates :username, presence: true
102
+ validates :email, presence: true
103
+ end
104
+
96
105
  validates :name, presence: true
97
106
  validates :industry_type, inclusion: { in: industry_schema.dynamic_type_names }
98
107
 
@@ -100,6 +109,7 @@ class CompanySchema
100
109
  validates :industry, presence: true, schema: true
101
110
  validates :locations, presence: true, schema: true
102
111
  validates :employees, presence: true, schema: true
112
+ validates :admins, presence: true, schema: true
103
113
  end
104
114
  ```
105
115
 
@@ -138,6 +148,16 @@ end
138
148
  "start_date": "2018-05-10",
139
149
  "manager_name": "Queen Bee"
140
150
  }
141
- ]
151
+ ],
152
+ "admins": {
153
+ "captain": {
154
+ "email": "captain@example.com",
155
+ "name": "Captain Kurk"
156
+ },
157
+ "joe": {
158
+ "email": "joe.smith@example.com",
159
+ "name": "Joe Smith"
160
+ }
161
+ }
142
162
  }
143
163
  ```
@@ -12,6 +12,8 @@ module Schema
12
12
  @schema_class = base_schema.class.const_get(options[:class_name])
13
13
  @aliases = options.fetch(:aliases, [])
14
14
  @ignorecase = options[:type_ignorecase]
15
+ @is_list = options[:from] != :hash
16
+ @hash_key_field = options[:hash_key_field]
15
17
  configure_dynamic_schema_options(options)
16
18
  end
17
19
 
@@ -31,8 +33,14 @@ module Schema
31
33
  end
32
34
 
33
35
  def create_schemas(base_schema, list)
34
- if list.is_a?(Array)
36
+ if is_list? && list.is_a?(Array)
35
37
  list.each_with_index.map { |data, idx| create_schema(base_schema, data, "#{@schema_name}:#{idx}") }
38
+ elsif !is_list? && list.is_a?(Hash)
39
+ list.map do |(key, data)|
40
+ schema = create_schema(base_schema, data, "#{@schema_name}:#{key}")
41
+ schema.send(schema.class.schema[@hash_key_field][:setter], key)
42
+ schema
43
+ end
36
44
  elsif !list.nil?
37
45
  add_parsing_error(base_schema, @schema_name, INCOMPATABLE)
38
46
  nil
@@ -51,6 +59,10 @@ module Schema
51
59
  !@type_field.nil? || !@external_type_field.nil?
52
60
  end
53
61
 
62
+ def is_list?
63
+ @is_list
64
+ end
65
+
54
66
  def get_dynamic_schema_class(base_schema, data)
55
67
  type = get_dynamic_type(base_schema, data)
56
68
  type = type.to_s.downcase if @ignorecase
data/lib/schema/utils.rb CHANGED
@@ -28,6 +28,7 @@ module Schema
28
28
  options[:class_name] ||= 'Schema' + classify_name(schema_type.to_s) + classify_name(schema_name.to_s)
29
29
  options[:association] = true
30
30
  options[:aliases] = [options[:alias]] if options.key?(:alias)
31
+ options[:hash_key_field] ||= :id if options[:from] == :hash
31
32
  ::Schema::Model.default_attribute_options(schema_name, schema_type).merge(options)
32
33
  end
33
34
 
data/schema-model.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'schema-model'
5
- s.version = '0.5.6'
5
+ s.version = '0.6.0'
6
6
  s.licenses = ['MIT']
7
7
  s.summary = 'Schema Model'
8
8
  s.description = 'Easy way to create models from payloads'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schema-model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.6
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Doug Youch
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-11-22 00:00:00.000000000 Z
11
+ date: 2020-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: inheritance-helper