typero 0.9.3 → 0.9.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.version +1 -1
- data/lib/adapters/sequel.rb +1 -1
- data/lib/typero/params.rb +13 -10
- data/lib/typero/schema.rb +13 -2
- data/lib/typero/type/type.rb +17 -2
- data/lib/typero/type/types/date_type.rb +3 -0
- data/lib/typero/type/types/datetime_type.rb +3 -0
- data/lib/typero/type/types/string_type.rb +3 -0
- data/lib/typero/typero.rb +19 -9
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0f8d03e15d714e38774a8f817aaf02c710e3ddca8b65f9d413aefb01678bd31b
|
4
|
+
data.tar.gz: ebd4cb0d01d43f7a93c3e2575daa832ba960dd8ece2a284676c763f4c43ef30c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7a8dc1fdac410f59695f486860f078359a8344492335759b2df69f2fa382e3366000d70d994c4c05bdfa613c6c9dc207b66fefd2e4731f97dca97f94eda94b85
|
7
|
+
data.tar.gz: e7c6581d6fc6558687b023505564c669f59ce8e5dc0a298392f83fab92e1f47b27704a0b7644143161c6a264d601c2ac2a382c8cc0bdfb54f97ea52789846891
|
data/.version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.9.
|
1
|
+
0.9.4
|
data/lib/adapters/sequel.rb
CHANGED
@@ -27,7 +27,7 @@ module Sequel::Plugins::TyperoAttributes
|
|
27
27
|
|
28
28
|
# we only check if field is changed
|
29
29
|
if value.present? && column_changed?(field) && self.class.xwhere('LOWER(%s)=LOWER(?) and id<>?' % field, value, id).first
|
30
|
-
error = unique.class == TrueClass ? %[Value
|
30
|
+
error = unique.class == TrueClass ? %[Value "#{value}" for field "#{field}" has been already used, please chose another value.] : unique
|
31
31
|
errors.add(field, error) unless (errors.on(field) || []).include?(error)
|
32
32
|
end
|
33
33
|
end
|
data/lib/typero/params.rb
CHANGED
@@ -49,11 +49,8 @@ module Typero
|
|
49
49
|
opts[:required] = false
|
50
50
|
end
|
51
51
|
|
52
|
-
|
53
|
-
|
54
|
-
else
|
55
|
-
opts[:required] = true if opts[:required].nil?
|
56
|
-
end
|
52
|
+
opts[:required] = opts.delete(:req) unless opts[:req].nil?
|
53
|
+
opts[:required] = true if opts[:required].nil?
|
57
54
|
|
58
55
|
# array that allows duplicates
|
59
56
|
if opts[:type].is_a?(Array)
|
@@ -74,12 +71,16 @@ module Typero
|
|
74
71
|
opts[:required] = false
|
75
72
|
opts[:default] = true
|
76
73
|
opts[:type] = :boolean
|
77
|
-
elsif opts[:type].is_a?(FalseClass) || opts[:type] == :false
|
78
|
-
opts[:required] = false
|
79
|
-
opts[:default] = false
|
74
|
+
elsif opts[:type].is_a?(FalseClass) || opts[:type] == :false || opts[:type] == :boolean
|
75
|
+
opts[:required] = false if opts[:required].nil?
|
76
|
+
opts[:default] = false if opts[:default].nil?
|
80
77
|
opts[:type] = :boolean
|
81
78
|
end
|
82
79
|
|
80
|
+
# model / schema
|
81
|
+
if opts[:type].class.ancestors.include?(Typero::Schema)
|
82
|
+
opts[:model] = opts.delete(:type)
|
83
|
+
end
|
83
84
|
opts[:model] = opts.delete(:schema) if opts[:schema]
|
84
85
|
opts[:type] = :model if opts[:model]
|
85
86
|
|
@@ -94,8 +95,10 @@ module Typero
|
|
94
95
|
opts[:description] = opts.delete(:desc) unless opts[:desc].nil?
|
95
96
|
|
96
97
|
# chek alloed params, all optional should go in meta
|
97
|
-
|
98
|
-
|
98
|
+
opts.keys.each do |key|
|
99
|
+
type = Typero::Type.load opts[:type]
|
100
|
+
type.allowed_opt?(key) {|err| raise ArgumentError, err }
|
101
|
+
end
|
99
102
|
|
100
103
|
field = field.to_sym
|
101
104
|
|
data/lib/typero/schema.rb
CHANGED
@@ -23,6 +23,9 @@ module Typero
|
|
23
23
|
end
|
24
24
|
|
25
25
|
@schema.rules.each do |field, opts|
|
26
|
+
# force filed as a symbol
|
27
|
+
field = field.to_sym
|
28
|
+
|
26
29
|
for k in opts.keys
|
27
30
|
opts[k] = @object.instance_exec(&opts[k]) if opts[k].is_a?(Proc)
|
28
31
|
end
|
@@ -30,8 +33,16 @@ module Typero
|
|
30
33
|
# set value to default if value is blank and default given
|
31
34
|
@object[field] = opts[:default] if opts[:default] && @object[field].blank?
|
32
35
|
|
33
|
-
|
34
|
-
|
36
|
+
if @object.respond_to?(:key?)
|
37
|
+
if @object.key?(field)
|
38
|
+
value = @object[field]
|
39
|
+
elsif @object.key?(field.to_s)
|
40
|
+
# invalid string key, needs fix
|
41
|
+
value = @object[field] = @object.delete(field.to_s)
|
42
|
+
end
|
43
|
+
else
|
44
|
+
value = @object[field]
|
45
|
+
end
|
35
46
|
|
36
47
|
if opts[:array]
|
37
48
|
unless value.respond_to?(:each)
|
data/lib/typero/type/type.rb
CHANGED
@@ -22,6 +22,7 @@ module Typero
|
|
22
22
|
:array,
|
23
23
|
:default,
|
24
24
|
:description,
|
25
|
+
:delimiter,
|
25
26
|
:max_count,
|
26
27
|
:meta,
|
27
28
|
:min_count,
|
@@ -53,11 +54,23 @@ module Typero
|
|
53
54
|
end
|
54
55
|
|
55
56
|
def opts key, desc
|
56
|
-
OPTS_KEYS.push key unless OPTS_KEYS.include?(key)
|
57
|
-
|
58
57
|
OPTS[self] ||= {}
|
59
58
|
OPTS[self][key] = desc
|
60
59
|
end
|
60
|
+
|
61
|
+
def allowed_opt? name
|
62
|
+
return true if OPTS_KEYS.include?(name)
|
63
|
+
|
64
|
+
OPTS[self] ||= {}
|
65
|
+
return true if OPTS[self][name]
|
66
|
+
|
67
|
+
msg = %[Unallowed param "#{name}" for type "#{to_s}" found. Allowed are "#{OPTS_KEYS.join(', ')}"]
|
68
|
+
msg += %[ + "#{OPTS[self].keys.join(', ')}"] if OPTS[self].keys.first
|
69
|
+
|
70
|
+
block_given? ? yield(msg) : raise(ArgumentError, msg)
|
71
|
+
|
72
|
+
false
|
73
|
+
end
|
61
74
|
end
|
62
75
|
|
63
76
|
###
|
@@ -65,6 +78,8 @@ module Typero
|
|
65
78
|
def initialize value, opts={}, &block
|
66
79
|
value = value.strip.rstrip if value.is_a?(String)
|
67
80
|
|
81
|
+
opts.keys.each {|key| self.class.allowed_opt?(key) }
|
82
|
+
|
68
83
|
@value = value
|
69
84
|
@opts = opts
|
70
85
|
@block = block
|
@@ -7,6 +7,9 @@ class Typero::StringType < Typero::Type
|
|
7
7
|
value(&:to_s)
|
8
8
|
value(&:downcase) if opts[:downcase]
|
9
9
|
|
10
|
+
# this is database default for string type and it is good to define default unless defined
|
11
|
+
opts[:max] ||= 255
|
12
|
+
|
10
13
|
error_for(:min_length_error, opts[:min], value.length) if opts[:min] && value.length < opts[:min]
|
11
14
|
error_for(:max_length_error, opts[:max], value.length) if opts[:max] && value.length > opts[:max]
|
12
15
|
end
|
data/lib/typero/typero.rb
CHANGED
@@ -22,18 +22,28 @@ module Typero
|
|
22
22
|
VERSION = File.read File.expand_path "../../.version", File.dirname(__FILE__)
|
23
23
|
|
24
24
|
# check and coerce value
|
25
|
-
# Typero.
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
if
|
31
|
-
|
32
|
-
false
|
25
|
+
# Typero.type(:label) -> Typero::LabelType
|
26
|
+
# Typero.type(:label, 'Foo bar') -> "foo-bar"
|
27
|
+
def type klass_name, value = :_undefined, opts = {}, &block
|
28
|
+
klass = Typero::Type.load(klass_name)
|
29
|
+
|
30
|
+
if value == :_undefined
|
31
|
+
klass
|
33
32
|
else
|
34
|
-
|
33
|
+
begin
|
34
|
+
check = klass.new value, opts
|
35
|
+
check.get
|
36
|
+
rescue TypeError => error
|
37
|
+
if block
|
38
|
+
block.call error
|
39
|
+
false
|
40
|
+
else
|
41
|
+
raise error
|
42
|
+
end
|
43
|
+
end
|
35
44
|
end
|
36
45
|
end
|
46
|
+
alias :set :type
|
37
47
|
|
38
48
|
# load or set type schema
|
39
49
|
# Typero.schema(:blog) { ... }
|
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.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dino Reic
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-07-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: fast_blank
|
@@ -76,7 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
76
76
|
- !ruby/object:Gem::Version
|
77
77
|
version: '0'
|
78
78
|
requirements: []
|
79
|
-
rubygems_version: 3.
|
79
|
+
rubygems_version: 3.2.22
|
80
80
|
signing_key:
|
81
81
|
specification_version: 4
|
82
82
|
summary: Ruby type system
|