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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6b60246c3690ede7ca8f5e449abff10f446d76a6
4
- data.tar.gz: fade8fc6679cc675269bb47ff03f3f0b1f965ac8
3
+ metadata.gz: 5d62a6bb35901d4b3b8a3734f9eb642facd83ea7
4
+ data.tar.gz: 33d5c380b533fee0bbf1ce7d7fe5d46c57f0ff17
5
5
  SHA512:
6
- metadata.gz: 024ec4893a7173adef65b29d2c14eaff0ff7d8ce658a44584aaf4ee11c5ed64c8883a6914eeb737bea6dfbf92b58bc9e2e21ce9d9f59f31cf5d066ab48a232f6
7
- data.tar.gz: bfc7263f3991580a455ddb6f819be4337569e5d3da8203a716f5d4f813cf921b41b59b24e5cd947da995b8019d57877664c25227483c42efa2cbeccce9411ad5
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.respond_to?(:get_set) ? type_inst.get_set(value) : set(value)
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.blank?
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
- filter = instance.class.select(:id).where("#{@name}=?", value)
77
- filter = filter.where('id<>?', instance.id) if instance.id
78
- raise TypeError, @opts[:uniq].class == TrueClass ? "Field #{@name} is uniqe and allready exists" : @opts[:uniq] if filter.first
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)
@@ -4,17 +4,26 @@ module Typero
4
4
  []
5
5
  end
6
6
 
7
- def get(list)
8
- list.to_a
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 = value.to_s.split(/\s*,\s*/) unless value.class.to_s.index('Array')
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
- check_type(type)
15
- value.each { |el| Typero.validate!(el, type) }
20
+ value.map! { |el|
21
+ Typero.validate!(el, type)
22
+ Typero.quick_set(el, type)
23
+ }
16
24
  end
17
- value.to_a
25
+
26
+ value
18
27
  end
19
28
 
20
29
  def validate(list)
@@ -4,6 +4,10 @@ module Typero
4
4
  value.to_f
5
5
  end
6
6
 
7
+ def get(value)
8
+ value.to_f
9
+ end
10
+
7
11
  def validate(input)
8
12
  value = set(input)
9
13
  raise TypeError, "min lenght is #{@opts[:min]}" if @opts[:min] && value < @opts[:min]
@@ -1,4 +1,4 @@
1
1
  module Typero
2
2
  # VERSION = File.read(File.dirname(__FILE__)+'/version.txt').gsub(/\s/,'').freeze
3
- VERSION = '0.2.6'
3
+ VERSION = '0.2.7'
4
4
  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.2.6
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: 2016-10-27 00:00:00.000000000 Z
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.5.1
69
+ rubygems_version: 2.6.8
70
70
  signing_key:
71
71
  specification_version: 4
72
72
  summary: Ruby type system