typero 0.2.6 → 0.2.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/typero.rb +1 -2
- data/lib/typero/schema.rb +4 -0
- data/lib/typero/type.rb +10 -5
- data/lib/typero/type/array.rb +15 -6
- data/lib/typero/type/float.rb +4 -0
- data/lib/typero/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5d62a6bb35901d4b3b8a3734f9eb642facd83ea7
|
4
|
+
data.tar.gz: 33d5c380b533fee0bbf1ce7d7fe5d46c57f0ff17
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6b51d247bedb7cdde07887f0411944f461ef63b521bccad0b8c09efc541575eb069511661d1fd25c996aa2eae3efecb3092d77f122cb2054417e59be112dbcb2
|
7
|
+
data.tar.gz: 3174416a39d5fcf514a181011732ba4f913c4d68d14017e126be4fcf7ab94593a59396bfbbf4e3f8237437da555ff81ebd74309bbc931b73a769c0d2beda7ef8
|
data/lib/typero.rb
CHANGED
@@ -76,10 +76,9 @@ module Typero
|
|
76
76
|
def quick_set(value, type, opts={})
|
77
77
|
type_klass = "Typero::#{type.to_s.classify}Type".constantize
|
78
78
|
type_inst = type_klass.new(opts)
|
79
|
-
type_inst.
|
79
|
+
type_inst.set(value)
|
80
80
|
end
|
81
81
|
|
82
|
-
|
83
82
|
# Typero.validate_opts params, :ttl, :password
|
84
83
|
# only :ttl, :password and allowed in hash
|
85
84
|
def validate_opts(hash, *args)
|
data/lib/typero/schema.rb
CHANGED
@@ -3,6 +3,7 @@
|
|
3
3
|
# use whenever you want to check hash values of variables
|
4
4
|
# in Lux FW, used in api cell
|
5
5
|
|
6
|
+
# Typero::Schema.load_schema(email:{ req: true, type: :email}, age: { type: Integer, req: false })
|
6
7
|
module Typero
|
7
8
|
class Schema
|
8
9
|
class << self
|
@@ -55,6 +56,9 @@ module Typero
|
|
55
56
|
Typero.validate!(value, type, check_hash) { |msg|
|
56
57
|
errors[field] = "#{field.to_s.capitalize}: #{msg}"
|
57
58
|
}
|
59
|
+
|
60
|
+
# coerce value
|
61
|
+
hash[field] = Typero.quick_set(value, type) if value.present?
|
58
62
|
end
|
59
63
|
end
|
60
64
|
errors.keys.length > 0 ? errors : nil
|
data/lib/typero/type.rb
CHANGED
@@ -49,8 +49,7 @@ module Typero
|
|
49
49
|
def get_value(instance)
|
50
50
|
# get current var value
|
51
51
|
value = instance.respond_to?('[]') ? instance[@name] : instance.instance_variable_get("@#{@name}")
|
52
|
-
if value.
|
53
|
-
# if blank check for default
|
52
|
+
if value.nil?
|
54
53
|
if @opts[:default]
|
55
54
|
@opts[:default].class.to_s == 'Proc' ? @opts[:default].call(instance) : @opts[:default]
|
56
55
|
else
|
@@ -73,9 +72,15 @@ module Typero
|
|
73
72
|
# search for unique fields on set, say for email
|
74
73
|
# so it is not checked on save, it is checked on set
|
75
74
|
if @opts[:uniq]
|
76
|
-
|
77
|
-
|
78
|
-
|
75
|
+
not_unique = false
|
76
|
+
if instance.respond_to?(:is_unique_field?)
|
77
|
+
not_unique = !instance.is_unique_field?(@name, value)
|
78
|
+
else
|
79
|
+
filter = instance.class.select(:id).where("#{@name}=?", value)
|
80
|
+
filter = filter.where('id<>?', instance.id) if instance.id
|
81
|
+
not_unique = true if filter.first
|
82
|
+
end
|
83
|
+
raise TypeError, @opts[:uniq].class == TrueClass ? %[Field #{@name} is uniqe and value "#{value}" allready exists] : @opts[:uniq] if not_unique
|
79
84
|
end
|
80
85
|
|
81
86
|
validate(value)
|
data/lib/typero/type/array.rb
CHANGED
@@ -4,17 +4,26 @@ module Typero
|
|
4
4
|
[]
|
5
5
|
end
|
6
6
|
|
7
|
-
def get(
|
8
|
-
|
7
|
+
def get(value)
|
8
|
+
unless value.class.to_s.index('Array')
|
9
|
+
value = value.to_s.sub(/^\{/,'').sub(/\}$/,'').split(/\s*,\s*/)
|
10
|
+
end
|
11
|
+
value
|
9
12
|
end
|
10
13
|
|
11
14
|
def set(value)
|
12
|
-
value =
|
15
|
+
value = get(value)
|
16
|
+
# value = value.to_a unless value.is_array?
|
17
|
+
|
18
|
+
# force type for all elements of array
|
13
19
|
if type = @opts[:array_type]
|
14
|
-
|
15
|
-
|
20
|
+
value.map! { |el|
|
21
|
+
Typero.validate!(el, type)
|
22
|
+
Typero.quick_set(el, type)
|
23
|
+
}
|
16
24
|
end
|
17
|
-
|
25
|
+
|
26
|
+
value
|
18
27
|
end
|
19
28
|
|
20
29
|
def validate(list)
|
data/lib/typero/type/float.rb
CHANGED
data/lib/typero/version.rb
CHANGED
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.2.
|
4
|
+
version: 0.2.7
|
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: 2017-01-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: fast_blank
|
@@ -66,7 +66,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
66
66
|
version: '0'
|
67
67
|
requirements: []
|
68
68
|
rubyforge_project:
|
69
|
-
rubygems_version: 2.
|
69
|
+
rubygems_version: 2.6.8
|
70
70
|
signing_key:
|
71
71
|
specification_version: 4
|
72
72
|
summary: Ruby type system
|