typero 0.9.4 → 0.9.6
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/.version +1 -1
- data/lib/adapters/sequel.rb +2 -2
- data/lib/typero/params.rb +4 -3
- data/lib/typero/schema.rb +43 -8
- data/lib/typero/type/type.rb +9 -1
- data/lib/typero/type/types/hash_type.rb +1 -0
- data/lib/typero/type/types/model_type.rb +2 -1
- data/lib/typero/type/types/point_type.rb +2 -1
- data/lib/typero/type/types/simple_point_type.rb +20 -0
- data/lib/typero/typero.rb +23 -58
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 54d18d3e66cf73fd0d6f6c42f319cb744fb3cc3f5e11e5e4a24b961c36792a34
|
4
|
+
data.tar.gz: 65f87c041f1a498c82d707d897d93eab7294a0d373a4d9aca5ec9aefe5da27b7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1a63e1a470f0e21162f3c11e40fe4977b4432bb30d3f154330840f344b1dc91c49dc453213139b8e5299f4afe587ae3bf6427fa0aabedd0cc0910188e6463ca5
|
7
|
+
data.tar.gz: 0dd2e24597fa25a0543b851fa3218fc69e129fa671c3ed52edf6110994876c14e3ceb88002ebfec130782722b51dee184cf30dbb0012384da9eba7654d8db24c
|
data/.version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.9.
|
1
|
+
0.9.6
|
data/lib/adapters/sequel.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
module Sequel::Plugins::
|
3
|
+
module Sequel::Plugins::Typero
|
4
4
|
module ClassMethods
|
5
5
|
def typero
|
6
6
|
Typero.schema self
|
@@ -48,5 +48,5 @@ module Sequel::Plugins::TyperoAttributes
|
|
48
48
|
end
|
49
49
|
end
|
50
50
|
|
51
|
-
Sequel::Model.plugin :
|
51
|
+
# Sequel::Model.plugin :typero
|
52
52
|
|
data/lib/typero/params.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
# Base class for schema validation
|
1
|
+
# Base class for schema validation.
|
2
|
+
# Accepts set of params and returns hash of porsed rules
|
2
3
|
|
3
4
|
module Typero
|
4
5
|
class Params
|
@@ -114,7 +115,7 @@ module Typero
|
|
114
115
|
# db :timestamps
|
115
116
|
# db :add_index, :code -> t.add_index :code
|
116
117
|
def db *args
|
117
|
-
@db_rules.push args
|
118
|
+
@db_rules.push args.unshift(:db_rule!)
|
118
119
|
end
|
119
120
|
|
120
121
|
# set :age, type: :integer -> integer :age
|
@@ -126,4 +127,4 @@ module Typero
|
|
126
127
|
set field, *args, &block
|
127
128
|
end
|
128
129
|
end
|
129
|
-
end
|
130
|
+
end
|
data/lib/typero/schema.rb
CHANGED
@@ -1,17 +1,51 @@
|
|
1
|
+
# Typero.schema :user, type: :model do
|
2
|
+
|
3
|
+
# Typero.schema :some_name, type: :model, db: DB_LOG do
|
4
|
+
# set :name, String, req: true
|
5
|
+
# set :email, :email, req: true
|
6
|
+
# set :emails, [:email], min: 2
|
7
|
+
# end
|
8
|
+
#
|
9
|
+
# rules = Typero.schema :some_name
|
10
|
+
#
|
11
|
+
# or
|
12
|
+
#
|
13
|
+
# rules = Typero.schema do
|
14
|
+
# string :name, req: true # generic email string
|
15
|
+
# email :email, req: true # string of type email
|
16
|
+
# emails [:skills], min: 2 # list of emails in filed named "emails"
|
17
|
+
# end
|
18
|
+
#
|
19
|
+
# errors = rules.validate (@object || @hash)
|
20
|
+
# rules.valid? (@object)
|
21
|
+
# rules.validate(@object) {|errors| ... }
|
22
|
+
|
1
23
|
module Typero
|
2
24
|
class Schema
|
3
|
-
|
4
|
-
|
25
|
+
SCHEMA_STORE ||= {}
|
26
|
+
|
27
|
+
attr_reader :klass
|
28
|
+
attr_reader :schema
|
29
|
+
attr_reader :opts
|
5
30
|
|
6
31
|
# accepts dsl block to
|
7
|
-
def initialize &block
|
8
|
-
|
9
|
-
|
32
|
+
def initialize name, opts = nil, &block
|
33
|
+
if block
|
34
|
+
@opts = opts || {}
|
35
|
+
@schema = Params.new &block
|
36
|
+
|
37
|
+
if name
|
38
|
+
@klass = name
|
39
|
+
SCHEMA_STORE[name] = self
|
40
|
+
end
|
41
|
+
else
|
42
|
+
raise "Use Typero.schema(:name) to load stored schema"
|
43
|
+
end
|
10
44
|
end
|
11
45
|
|
12
46
|
# validates any instance object with hash variable interface
|
13
47
|
# it also coarces values
|
14
|
-
def validate object, options=nil
|
48
|
+
def validate object, options = nil
|
15
49
|
@options = options || {}
|
16
50
|
@object = object
|
17
51
|
@errors = {}
|
@@ -67,11 +101,12 @@ module Typero
|
|
67
101
|
|
68
102
|
# if value is not list of allowed values, raise error
|
69
103
|
allowed = opts[:allow] || opts[:allowed] || opts[:values]
|
70
|
-
if value && allowed && !allowed.include?(value)
|
104
|
+
if value && allowed && !allowed.map(&:to_s).include?(value.to_s)
|
71
105
|
add_error field, 'Value "%s" is not allowed' % value, opts
|
72
106
|
end
|
73
107
|
|
74
108
|
value = check_filed_value field, value, opts
|
109
|
+
|
75
110
|
add_required_error field, value, opts
|
76
111
|
end
|
77
112
|
|
@@ -101,7 +136,7 @@ module Typero
|
|
101
136
|
schema_opts = @schema.rules[field]
|
102
137
|
opts[:array] = true if schema_opts[:array]
|
103
138
|
|
104
|
-
total << [
|
139
|
+
total << [field, type, opts]
|
105
140
|
end
|
106
141
|
|
107
142
|
out += @schema.db_rules
|
data/lib/typero/type/type.rb
CHANGED
@@ -71,6 +71,10 @@ module Typero
|
|
71
71
|
|
72
72
|
false
|
73
73
|
end
|
74
|
+
|
75
|
+
def db_schema
|
76
|
+
new(nil).db_schema
|
77
|
+
end
|
74
78
|
end
|
75
79
|
|
76
80
|
###
|
@@ -98,7 +102,11 @@ module Typero
|
|
98
102
|
opts[:default].nil? ? default : opts[:default]
|
99
103
|
else
|
100
104
|
set
|
101
|
-
|
105
|
+
|
106
|
+
if opts[:values] && !opts[:values].map(&:to_s).include?(@value.to_s)
|
107
|
+
error_for(:not_in_range, opts[:values].join(', '))
|
108
|
+
end
|
109
|
+
|
102
110
|
value
|
103
111
|
end
|
104
112
|
end
|
@@ -3,7 +3,6 @@ class Typero::ModelType < Typero::Type
|
|
3
3
|
value(&:to_h)
|
4
4
|
|
5
5
|
errors = {}
|
6
|
-
|
7
6
|
schema = opts[:model].is_a?(Typero::Schema) ? opts[:model] : Typero.schema(opts[:model])
|
8
7
|
|
9
8
|
# by default models in schems are strict true (remove undefined keys)
|
@@ -11,6 +10,8 @@ class Typero::ModelType < Typero::Type
|
|
11
10
|
errors[field] = error
|
12
11
|
end
|
13
12
|
|
13
|
+
@value.delete_if {|_, v| v.empty? }
|
14
|
+
|
14
15
|
raise TypeError.new errors.to_json if errors.keys.first
|
15
16
|
end
|
16
17
|
|
@@ -4,12 +4,13 @@
|
|
4
4
|
class Typero::PointType < Typero::Type
|
5
5
|
def set
|
6
6
|
if value.include?('/@')
|
7
|
+
# extract value from google maps link
|
7
8
|
point = value.split('/@', 2).last.split(',')
|
8
9
|
value { [point[0], point[1]].join(',') }
|
9
10
|
end
|
10
11
|
|
11
12
|
if !value.include?('POINT') && value.include?(',')
|
12
|
-
point = value.sub(
|
13
|
+
point = value.sub(/\s*,\s*/, ' ')
|
13
14
|
value { 'SRID=4326;POINT(%s)' % point }
|
14
15
|
end
|
15
16
|
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# Same as point, but we keep data as a float in Array
|
2
|
+
|
3
|
+
class Typero::SimplePointType < Typero::Type
|
4
|
+
def set
|
5
|
+
if value.include?('/@')
|
6
|
+
# extract value from google maps link
|
7
|
+
point = value.split('/@', 2).last.split(',')[0,2]
|
8
|
+
value { point }
|
9
|
+
end
|
10
|
+
|
11
|
+
if !value.include?('POINT') && value.include?(',')
|
12
|
+
value { value.split(/\s*,\s*/)[0,2] }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def db_schema
|
17
|
+
[:float, { array: true }]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
data/lib/typero/typero.rb
CHANGED
@@ -1,25 +1,7 @@
|
|
1
|
-
# rules = Typero.schema do
|
2
|
-
# set :name, String, req: true
|
3
|
-
# set :email, :email, req: true
|
4
|
-
# set :skills, [:email], min: 2
|
5
|
-
# end
|
6
|
-
#
|
7
|
-
# or
|
8
|
-
#
|
9
|
-
# rules = Typero.schema do
|
10
|
-
# string :name, req: true
|
11
|
-
# email :email, req: true
|
12
|
-
# email [:skills], min: 2
|
13
|
-
# end
|
14
|
-
#
|
15
|
-
# errors = rules.validate @object
|
16
|
-
# rules.valid?
|
17
|
-
# rules.validate(@object) { |errors| ... }
|
18
|
-
|
19
1
|
module Typero
|
20
2
|
extend self
|
21
3
|
|
22
|
-
VERSION
|
4
|
+
VERSION ||= File.read File.expand_path "../../.version", File.dirname(__FILE__)
|
23
5
|
|
24
6
|
# check and coerce value
|
25
7
|
# Typero.type(:label) -> Typero::LabelType
|
@@ -45,39 +27,39 @@ module Typero
|
|
45
27
|
end
|
46
28
|
alias :set :type
|
47
29
|
|
48
|
-
#
|
30
|
+
# type schema
|
49
31
|
# Typero.schema(:blog) { ... }
|
32
|
+
|
33
|
+
# type schema with option
|
50
34
|
# Typero.schema(:blog, type: :model) { ... }
|
35
|
+
|
36
|
+
# get schema
|
51
37
|
# Typero.schema(:blog)
|
38
|
+
|
39
|
+
# get all schema that matches any option
|
52
40
|
# Typero.schema(type: :model)
|
53
|
-
def schema name=nil, opts=nil, &block
|
41
|
+
def schema name = nil, opts = nil, &block
|
54
42
|
klass = name.to_s.classify if name && !name.is_a?(Hash)
|
55
43
|
|
56
44
|
if block_given?
|
57
|
-
Typero
|
58
|
-
|
59
|
-
Typero::Schema::SCHEMAS[klass] = schema
|
60
|
-
|
61
|
-
if opts && opts[:type]
|
62
|
-
Typero::Schema::TYPES[opts[:type]] ||= []
|
63
|
-
Typero::Schema::TYPES[opts[:type]].push klass unless Typero::Schema::TYPES[opts[:type]].include?(klass)
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
45
|
+
# Typero.schema type: :model, db: DB_LOG do
|
46
|
+
Typero::Schema.new(klass, opts, &block)
|
67
47
|
else
|
68
|
-
# Schema not given, get schema
|
69
48
|
if name.is_a?(Hash)
|
70
|
-
#
|
71
|
-
|
72
|
-
|
49
|
+
# Schema not given, get schema
|
50
|
+
# Typero.schema type: :model
|
51
|
+
out = []
|
52
|
+
|
53
|
+
for schema in Schema::SCHEMA_STORE.values
|
54
|
+
if schema.opts[name.keys.first] == name.values.first
|
55
|
+
out.push schema.klass
|
56
|
+
end
|
73
57
|
end
|
74
|
-
|
75
|
-
|
76
|
-
schema = Typero::Schema::SCHEMAS[klass]
|
77
|
-
schema ||= class_finder klass, :schema
|
78
|
-
schema || nil
|
58
|
+
|
59
|
+
out
|
79
60
|
else
|
80
|
-
|
61
|
+
# Typero.schema :user
|
62
|
+
Typero::Schema::SCHEMA_STORE[klass] || raise('Schema "%s" not found' % klass)
|
81
63
|
end
|
82
64
|
end
|
83
65
|
end
|
@@ -88,21 +70,4 @@ module Typero
|
|
88
70
|
rescue ArgumentError
|
89
71
|
false
|
90
72
|
end
|
91
|
-
|
92
|
-
private
|
93
|
-
|
94
|
-
# class_finder :user, :exporter, :representer
|
95
|
-
# find first UserExporter, User::Exporter, User::Representer, UserRepresenter
|
96
|
-
def class_finder *args
|
97
|
-
name = args.shift.to_s.classify
|
98
|
-
|
99
|
-
for el in args
|
100
|
-
for separator in ['_','/']
|
101
|
-
klass = [name, el].join(separator).classify
|
102
|
-
return klass.constantize if const_defined? klass
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
|
-
nil
|
107
|
-
end
|
108
73
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: typero
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dino Reic
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-12-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: fast_blank
|
@@ -51,6 +51,7 @@ files:
|
|
51
51
|
- "./lib/typero/type/types/model_type.rb"
|
52
52
|
- "./lib/typero/type/types/oib_type.rb"
|
53
53
|
- "./lib/typero/type/types/point_type.rb"
|
54
|
+
- "./lib/typero/type/types/simple_point_type.rb"
|
54
55
|
- "./lib/typero/type/types/string_type.rb"
|
55
56
|
- "./lib/typero/type/types/text_type.rb"
|
56
57
|
- "./lib/typero/type/types/time_type.rb"
|
@@ -76,7 +77,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
76
77
|
- !ruby/object:Gem::Version
|
77
78
|
version: '0'
|
78
79
|
requirements: []
|
79
|
-
rubygems_version: 3.2.
|
80
|
+
rubygems_version: 3.2.3
|
80
81
|
signing_key:
|
81
82
|
specification_version: 4
|
82
83
|
summary: Ruby type system
|