schema-model 0.3.0 → 0.3.2
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 +4 -4
- data/lib/schema/active_model_validations.rb +20 -0
- data/lib/schema/associations/schema_creator.rb +15 -9
- data/lib/schema/model.rb +26 -7
- data/lib/schema/parsers/array.rb +2 -2
- data/lib/schema/parsers/common.rb +11 -11
- data/lib/schema/parsers/hash.rb +1 -1
- data/lib/schema/parsers/json.rb +3 -2
- data/lib/schema/parsing_errors.rb +12 -0
- data/lib/schema.rb +4 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2930827fc56c8ef958799e26a4fe5c0e4033449ff61d0e58bbdd107d77dc0f2c
|
4
|
+
data.tar.gz: 43306799287c8c4edb21e07bf21924f2a6b165bdef2cb934401edb2e0549414d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 97ac444c3f460681e01b529f6d5e872f426e3b3ef44097a9c6492168f2175dc403b3f98318e72f1c79702440bbb7b2d45c9158591896e71c2bd94f0b6f4ece8b
|
7
|
+
data.tar.gz: 4a090a51203d1c97c75946ce6f297252e31a1137b7e7637821d148c702ba148f1d13e4de5b0c5c5843e2dbbf4b314a3688d1b7d7ced8e291050f8eb240964ee7
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_model'
|
4
|
+
|
5
|
+
module Schema
|
6
|
+
# Schema::Model adds schema building methods to a class, uses ActiveModel::Errors for parsing_errors
|
7
|
+
module ActiveModelValidations
|
8
|
+
def self.included(base)
|
9
|
+
base.schema_include ::ActiveModel::Validations
|
10
|
+
base.schema_include OverrideParsingErrors
|
11
|
+
end
|
12
|
+
|
13
|
+
# no-doc
|
14
|
+
module OverrideParsingErrors
|
15
|
+
def parsing_errors
|
16
|
+
@parsing_errors ||= ActiveModel::Errors.new(self)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -4,28 +4,28 @@ module Schema
|
|
4
4
|
module Associations
|
5
5
|
# Schema::Associations::SchemaCreator is used to create schema objects for associations
|
6
6
|
class SchemaCreator
|
7
|
+
include ::Schema::ParsingErrors
|
8
|
+
|
7
9
|
def initialize(base_schema, name)
|
8
10
|
options = base_schema.class.schema[name]
|
9
11
|
@schema_name = name
|
10
12
|
@schema_class = base_schema.class.const_get(options[:class_name])
|
11
|
-
@aliases = options
|
12
|
-
@
|
13
|
-
|
14
|
-
@types = options[:types]
|
15
|
-
@ignorecase = !options[:type_ignorecase].nil?
|
13
|
+
@aliases = options.fetch(:aliases, [])
|
14
|
+
@ignorecase = options[:type_ignorecase]
|
15
|
+
configure_dynamic_schema_options(options)
|
16
16
|
end
|
17
17
|
|
18
18
|
def create_schema(base_schema, data, error_name = nil)
|
19
19
|
if data.is_a?(Hash)
|
20
20
|
unless (schema_class = get_schema_class(base_schema, data))
|
21
|
-
add_parsing_error(base_schema, error_name,
|
21
|
+
add_parsing_error(base_schema, error_name, UNKNOWN)
|
22
22
|
return nil
|
23
23
|
end
|
24
24
|
schema = schema_class.from_hash(data)
|
25
|
-
add_parsing_error(base_schema, error_name,
|
25
|
+
add_parsing_error(base_schema, error_name, INVALID) unless schema.parsing_errors.empty?
|
26
26
|
schema
|
27
27
|
elsif !data.nil?
|
28
|
-
add_parsing_error(base_schema, error_name,
|
28
|
+
add_parsing_error(base_schema, error_name, INCOMPATABLE)
|
29
29
|
nil
|
30
30
|
end
|
31
31
|
end
|
@@ -34,7 +34,7 @@ module Schema
|
|
34
34
|
if list.is_a?(Array)
|
35
35
|
list.each_with_index.map { |data, idx| create_schema(base_schema, data, "#{@schema_name}:#{idx}") }
|
36
36
|
elsif !list.nil?
|
37
|
-
add_parsing_error(base_schema, @schema_name,
|
37
|
+
add_parsing_error(base_schema, @schema_name, INCOMPATABLE)
|
38
38
|
nil
|
39
39
|
end
|
40
40
|
end
|
@@ -83,6 +83,12 @@ module Schema
|
|
83
83
|
def add_parsing_error(base_schema, error_name, error_msg)
|
84
84
|
base_schema.parsing_errors.add(error_name || @schema_name, error_msg)
|
85
85
|
end
|
86
|
+
|
87
|
+
def configure_dynamic_schema_options(options)
|
88
|
+
@type_field = options[:type_field]
|
89
|
+
@external_type_field = options[:external_type_field]
|
90
|
+
@types = options[:types]
|
91
|
+
end
|
86
92
|
end
|
87
93
|
end
|
88
94
|
end
|
data/lib/schema/model.rb
CHANGED
@@ -28,6 +28,15 @@ module Schema
|
|
28
28
|
{}.freeze
|
29
29
|
end
|
30
30
|
|
31
|
+
def schema_with_string_keys
|
32
|
+
@schema_with_string_keys ||=
|
33
|
+
begin
|
34
|
+
hsh = {}
|
35
|
+
schema.each { |field_name, field_options| hsh[field_name.to_s] = field_options }
|
36
|
+
hsh.freeze
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
31
40
|
def schema_config
|
32
41
|
{
|
33
42
|
schema_includes: []
|
@@ -82,13 +91,14 @@ module Schema
|
|
82
91
|
end
|
83
92
|
|
84
93
|
def update_attributes(data)
|
85
|
-
|
86
|
-
|
94
|
+
schema = get_schema(data)
|
95
|
+
data.each do |key, value|
|
96
|
+
unless schema.key?(key)
|
97
|
+
parsing_errors.add(key, ::Schema::ParsingErrors::UNKNOWN_ATTRIBUTE)
|
98
|
+
next
|
99
|
+
end
|
87
100
|
|
88
|
-
public_send(
|
89
|
-
field_options[:setter],
|
90
|
-
data[field_options[:key]] || data[field_name.to_sym]
|
91
|
-
)
|
101
|
+
public_send(schema[key][:setter], value)
|
92
102
|
end
|
93
103
|
|
94
104
|
self
|
@@ -109,7 +119,16 @@ module Schema
|
|
109
119
|
alias to_h to_hash
|
110
120
|
|
111
121
|
def parsing_errors
|
112
|
-
@parsing_errors ||=
|
122
|
+
@parsing_errors ||= Errors.new
|
123
|
+
end
|
124
|
+
|
125
|
+
def get_schema(data)
|
126
|
+
data.each_key do |key|
|
127
|
+
break unless key.is_a?(Symbol)
|
128
|
+
|
129
|
+
return self.class.schema
|
130
|
+
end
|
131
|
+
self.class.schema_with_string_keys
|
113
132
|
end
|
114
133
|
end
|
115
134
|
end
|
data/lib/schema/parsers/array.rb
CHANGED
@@ -14,10 +14,10 @@ module Schema
|
|
14
14
|
return data
|
15
15
|
end
|
16
16
|
|
17
|
-
parsing_errors.add(field_name,
|
17
|
+
parsing_errors.add(field_name, ::Schema::ParsingErrors::INCOMPATABLE)
|
18
18
|
nil
|
19
19
|
else
|
20
|
-
parsing_errors.add(field_name,
|
20
|
+
parsing_errors.add(field_name, ::Schema::ParsingErrors::INCOMPATABLE)
|
21
21
|
nil
|
22
22
|
end
|
23
23
|
end
|
@@ -19,16 +19,16 @@ module Schema
|
|
19
19
|
if INTEGER_REGEX.match?(value)
|
20
20
|
Integer(value)
|
21
21
|
else
|
22
|
-
parsing_errors.add(field_name,
|
22
|
+
parsing_errors.add(field_name, ::Schema::ParsingErrors::INVALID)
|
23
23
|
nil
|
24
24
|
end
|
25
25
|
when Float
|
26
|
-
parsing_errors.add(field_name,
|
26
|
+
parsing_errors.add(field_name, ::Schema::ParsingErrors::INCOMPATABLE) if (value % 1) > 0.0
|
27
27
|
value.to_i
|
28
28
|
when nil
|
29
29
|
nil
|
30
30
|
else
|
31
|
-
parsing_errors.add(field_name,
|
31
|
+
parsing_errors.add(field_name, ::Schema::ParsingErrors::UNHANDLED_TYPE)
|
32
32
|
nil
|
33
33
|
end
|
34
34
|
end
|
@@ -39,7 +39,7 @@ module Schema
|
|
39
39
|
when String
|
40
40
|
value
|
41
41
|
when ::Hash, ::Array
|
42
|
-
parsing_errors.add(field_name,
|
42
|
+
parsing_errors.add(field_name, ::Schema::ParsingErrors::INCOMPATABLE)
|
43
43
|
nil
|
44
44
|
when nil
|
45
45
|
nil
|
@@ -58,13 +58,13 @@ module Schema
|
|
58
58
|
if FLOAT_REGEX.match?(value)
|
59
59
|
Float(value)
|
60
60
|
else
|
61
|
-
parsing_errors.add(field_name,
|
61
|
+
parsing_errors.add(field_name, ::Schema::ParsingErrors::INVALID)
|
62
62
|
nil
|
63
63
|
end
|
64
64
|
when nil
|
65
65
|
nil
|
66
66
|
else
|
67
|
-
parsing_errors.add(field_name,
|
67
|
+
parsing_errors.add(field_name, ::Schema::ParsingErrors::UNHANDLED_TYPE)
|
68
68
|
nil
|
69
69
|
end
|
70
70
|
end
|
@@ -79,13 +79,13 @@ module Schema
|
|
79
79
|
begin
|
80
80
|
Time.xmlschema(value)
|
81
81
|
rescue ArgumentError
|
82
|
-
parsing_errors.add(field_name,
|
82
|
+
parsing_errors.add(field_name, ::Schema::ParsingErrors::INVALID)
|
83
83
|
nil
|
84
84
|
end
|
85
85
|
when nil
|
86
86
|
nil
|
87
87
|
else
|
88
|
-
parsing_errors.add(field_name,
|
88
|
+
parsing_errors.add(field_name, ::Schema::ParsingErrors::UNHANDLED_TYPE)
|
89
89
|
nil
|
90
90
|
end
|
91
91
|
end
|
@@ -100,13 +100,13 @@ module Schema
|
|
100
100
|
begin
|
101
101
|
Date.parse(value)
|
102
102
|
rescue ArgumentError
|
103
|
-
parsing_errors.add(field_name,
|
103
|
+
parsing_errors.add(field_name, ::Schema::ParsingErrors::INVALID)
|
104
104
|
nil
|
105
105
|
end
|
106
106
|
when nil
|
107
107
|
nil
|
108
108
|
else
|
109
|
-
parsing_errors.add(field_name,
|
109
|
+
parsing_errors.add(field_name, ::Schema::ParsingErrors::UNHANDLED_TYPE)
|
110
110
|
nil
|
111
111
|
end
|
112
112
|
end
|
@@ -122,7 +122,7 @@ module Schema
|
|
122
122
|
when nil
|
123
123
|
nil
|
124
124
|
else
|
125
|
-
parsing_errors.add(field_name,
|
125
|
+
parsing_errors.add(field_name, ::Schema::ParsingErrors::UNHANDLED_TYPE)
|
126
126
|
nil
|
127
127
|
end
|
128
128
|
end
|
data/lib/schema/parsers/hash.rb
CHANGED
data/lib/schema/parsers/json.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
require 'json'
|
3
4
|
|
4
5
|
module Schema
|
@@ -11,11 +12,11 @@ module Schema
|
|
11
12
|
begin
|
12
13
|
::JSON.parse(value)
|
13
14
|
rescue ::JSON::ParserError
|
14
|
-
parsing_errors.add(field_name,
|
15
|
+
parsing_errors.add(field_name, ::Schema::ParsingErrors::INVALID)
|
15
16
|
nil
|
16
17
|
end
|
17
18
|
else
|
18
|
-
parsing_errors.add(field_name,
|
19
|
+
parsing_errors.add(field_name, ::Schema::ParsingErrors::INCOMPATABLE)
|
19
20
|
nil
|
20
21
|
end
|
21
22
|
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Schema
|
4
|
+
# Schema::ParsingErrors is a collection of parsing error messages
|
5
|
+
module ParsingErrors
|
6
|
+
INVALID = 'invalid'
|
7
|
+
INCOMPATABLE = 'incompatable'
|
8
|
+
UNKNOWN = 'unknown'
|
9
|
+
UNKNOWN_ATTRIBUTE = 'unknown_attribute'
|
10
|
+
UNHANDLED_TYPE = 'unhandled_type'
|
11
|
+
end
|
12
|
+
end
|
data/lib/schema.rb
CHANGED
@@ -1,13 +1,17 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
autoload :SchemaValidator, 'schema_validator'
|
4
|
+
|
3
5
|
# Schema is a series of tools for transforming data into models
|
4
6
|
module Schema
|
7
|
+
autoload :ActiveModelValidations, 'schema/active_model_validations'
|
5
8
|
autoload :ArrayHeaders, 'schema/array_headers'
|
6
9
|
autoload :Arrays, 'schema/arrays'
|
7
10
|
autoload :CSVParser, 'schema/csv_parser'
|
8
11
|
autoload :Errors, 'schema/errors'
|
9
12
|
autoload :CSVParser, 'schema/csv_parser'
|
10
13
|
autoload :Model, 'schema/model'
|
14
|
+
autoload :ParsingErrors, 'schema/parsing_errors'
|
11
15
|
autoload :Utils, 'schema/utils'
|
12
16
|
|
13
17
|
# Schema::Parsers are used to convert values into the correct data type
|
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.3.
|
4
|
+
version: 0.3.2
|
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-08-
|
11
|
+
date: 2019-08-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: inheritance-helper
|
@@ -31,6 +31,7 @@ extensions: []
|
|
31
31
|
extra_rdoc_files: []
|
32
32
|
files:
|
33
33
|
- lib/schema.rb
|
34
|
+
- lib/schema/active_model_validations.rb
|
34
35
|
- lib/schema/array_headers.rb
|
35
36
|
- lib/schema/arrays.rb
|
36
37
|
- lib/schema/associations/base.rb
|
@@ -45,6 +46,7 @@ files:
|
|
45
46
|
- lib/schema/parsers/common.rb
|
46
47
|
- lib/schema/parsers/hash.rb
|
47
48
|
- lib/schema/parsers/json.rb
|
49
|
+
- lib/schema/parsing_errors.rb
|
48
50
|
- lib/schema/utils.rb
|
49
51
|
- lib/schema_validator.rb
|
50
52
|
homepage: https://github.com/dougyouch/schema
|