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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2b0ee2233c2ba484ec5ff98778a8da24a876b7b22eb4d24cc6b3ba3a8886e714
4
- data.tar.gz: 4a96d10f9c6d0e75e8ae36965d34a6cda8853684974f799e2cf5e32d6a7003c1
3
+ metadata.gz: 0f8d03e15d714e38774a8f817aaf02c710e3ddca8b65f9d413aefb01678bd31b
4
+ data.tar.gz: ebd4cb0d01d43f7a93c3e2575daa832ba960dd8ece2a284676c763f4c43ef30c
5
5
  SHA512:
6
- metadata.gz: 880c3e4e551ab660f85530454ad9007ae383118677ea209ffaf0efff54bcd38da220579dc42d87618bd9ea50790b04a46c63dfe3cfd6252508b2bbec6ba1d711
7
- data.tar.gz: 6f10437b894c91f3c7f723da03b426218d60b21c8bd161111d8ae7a745bdf35565f4fba84f26552fc75aa9b40211f3902d1cc2418fc420a5e3bce912ac676bc2
6
+ metadata.gz: 7a8dc1fdac410f59695f486860f078359a8344492335759b2df69f2fa382e3366000d70d994c4c05bdfa613c6c9dc207b66fefd2e4731f97dca97f94eda94b85
7
+ data.tar.gz: e7c6581d6fc6558687b023505564c669f59ce8e5dc0a298392f83fab92e1f47b27704a0b7644143161c6a264d601c2ac2a382c8cc0bdfb54f97ea52789846891
data/.version CHANGED
@@ -1 +1 @@
1
- 0.9.3
1
+ 0.9.4
@@ -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 '"#{value}"' for #{field} allready exists] : unique
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
- if value = opts.delete(:req)
53
- opts[:required] = value
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
- result = opts.keys - Typero::Type::OPTS_KEYS
98
- raise ArgumentError.new('Unallowed Type params found: "%s", allowed: %s' % [result.join(' and '), Typero::Type::OPTS_KEYS.sort]) if result.length > 0
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
- # get field value
34
- value = @object[field]
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)
@@ -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
@@ -1,4 +1,7 @@
1
1
  class Typero::DateType < Typero::Type
2
+ opts :min, 'Smallest date-time allowed'
3
+ opts :max, 'Maximal date-time allowed'
4
+
2
5
  error :en, :min_date, 'Minimal allowed date is %s'
3
6
  error :en, :max_date, 'Maximal allowed date is %s'
4
7
 
@@ -1,6 +1,9 @@
1
1
  require_relative 'date_type'
2
2
 
3
3
  class Typero::DatetimeType < Typero::DateType
4
+ opts :min, 'Smallest date allowed'
5
+ opts :max, 'Maximal date allowed'
6
+
4
7
  def set
5
8
  unless [Time, DateTime].include?(value.class)
6
9
  value { |data| DateTime.parse(data) }
@@ -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.set(:label, 'Foo bar') -> "foo-bar"
26
- def set type, value, opts = {}, &block
27
- check = Typero::Type.load(type).new value, opts
28
- check.get
29
- rescue TypeError => error
30
- if block
31
- block.call error
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
- raise error
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.3
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-01-06 00:00:00.000000000 Z
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.0.6
79
+ rubygems_version: 3.2.22
80
80
  signing_key:
81
81
  specification_version: 4
82
82
  summary: Ruby type system