typero 0.9.4 → 0.9.5

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: 0f8d03e15d714e38774a8f817aaf02c710e3ddca8b65f9d413aefb01678bd31b
4
- data.tar.gz: ebd4cb0d01d43f7a93c3e2575daa832ba960dd8ece2a284676c763f4c43ef30c
3
+ metadata.gz: 0bcd151d7d7b875c6db7aec28c85c3abcf10596a4f596622050d7e18cdb81e9e
4
+ data.tar.gz: d54b9c76d6587e5872f19deba20febd7430e19022fe869a1360339c8156205d9
5
5
  SHA512:
6
- metadata.gz: 7a8dc1fdac410f59695f486860f078359a8344492335759b2df69f2fa382e3366000d70d994c4c05bdfa613c6c9dc207b66fefd2e4731f97dca97f94eda94b85
7
- data.tar.gz: e7c6581d6fc6558687b023505564c669f59ce8e5dc0a298392f83fab92e1f47b27704a0b7644143161c6a264d601c2ac2a382c8cc0bdfb54f97ea52789846891
6
+ metadata.gz: 5b639372e86d329ef150809f52f964a459c3faf369a86d219292a2af6b8364ed5c2745079d409944472ec59316cae690e0f1d554cecdeacb625bb5ccf4b39f29
7
+ data.tar.gz: 0e887a10b853325c7b27f3c90ab57b3c1987d38576e3f1128fb27eb4982d2aaefdbf6e5ded9e80a75c7085c5674686196b13653eed62db853d6d50fe0c15ad39
data/.version CHANGED
@@ -1 +1 @@
1
- 0.9.4
1
+ 0.9.5
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Sequel::Plugins::TyperoAttributes
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 :typero_attributes
51
+ # Sequel::Model.plugin :typero
52
52
 
data/lib/typero/schema.rb CHANGED
@@ -1,8 +1,5 @@
1
1
  module Typero
2
2
  class Schema
3
- SCHEMAS = {}
4
- TYPES = {}
5
-
6
3
  # accepts dsl block to
7
4
  def initialize &block
8
5
  raise "Params not defined" unless block_given?
@@ -67,11 +64,12 @@ module Typero
67
64
 
68
65
  # if value is not list of allowed values, raise error
69
66
  allowed = opts[:allow] || opts[:allowed] || opts[:values]
70
- if value && allowed && !allowed.include?(value)
67
+ if value && allowed && !allowed.map(&:to_s).include?(value.to_s)
71
68
  add_error field, 'Value "%s" is not allowed' % value, opts
72
69
  end
73
70
 
74
71
  value = check_filed_value field, value, opts
72
+
75
73
  add_required_error field, value, opts
76
74
  end
77
75
 
@@ -98,7 +98,11 @@ module Typero
98
98
  opts[:default].nil? ? default : opts[:default]
99
99
  else
100
100
  set
101
- error_for(:not_in_range, opts[:values].join(', ')) if opts[:values] && !opts[:values].include?(@value)
101
+
102
+ if opts[:values] && !opts[:values].map(&:to_s).include?(@value.to_s)
103
+ error_for(:not_in_range, opts[:values].join(', '))
104
+ end
105
+
102
106
  value
103
107
  end
104
108
  end
@@ -7,6 +7,7 @@ class Typero::HashType < Typero::Type
7
7
  end
8
8
 
9
9
  @value ||= {}
10
+ @value.delete_if {|_, v| v.empty? }
10
11
 
11
12
  error_for(:not_hash_type_error) unless @value.respond_to?(:keys) && @value.respond_to?(:values)
12
13
 
@@ -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(/,\s*/, ' ')
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
@@ -19,7 +19,8 @@
19
19
  module Typero
20
20
  extend self
21
21
 
22
- VERSION = File.read File.expand_path "../../.version", File.dirname(__FILE__)
22
+ VERSION ||= File.read File.expand_path "../../.version", File.dirname(__FILE__)
23
+ SCHEMAS ||= {}
23
24
 
24
25
  # check and coerce value
25
26
  # Typero.type(:label) -> Typero::LabelType
@@ -45,37 +46,52 @@ module Typero
45
46
  end
46
47
  alias :set :type
47
48
 
48
- # load or set type schema
49
+ # type schema
49
50
  # Typero.schema(:blog) { ... }
51
+
52
+ # type schema with option
50
53
  # Typero.schema(:blog, type: :model) { ... }
54
+
55
+ # get schema
51
56
  # Typero.schema(:blog)
57
+
58
+ # get schema with options (as array)
59
+ # Typero.schema(:blog, :with_schema)
60
+
61
+ # get all schema names with type: model
52
62
  # Typero.schema(type: :model)
53
- def schema name=nil, opts=nil, &block
63
+ def schema name = nil, opts = nil, &block
54
64
  klass = name.to_s.classify if name && !name.is_a?(Hash)
55
65
 
56
66
  if block_given?
57
67
  Typero::Schema.new(&block).tap do |schema|
58
68
  if klass
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
69
+ SCHEMAS[klass] = [schema, opts || {}]
65
70
  end
66
71
  end
67
72
  else
68
73
  # Schema not given, get schema
69
74
  if name.is_a?(Hash)
70
75
  # Typero.schema type: :model
71
- if type = name[:type]
72
- Typero::Schema::TYPES[type]
76
+ out = []
77
+
78
+ for key, _ in SCHEMAS
79
+ schema, opts = _
80
+ next unless opts[name.keys.first] == name.values.first
81
+ out.push key.classify
73
82
  end
83
+
84
+ out
74
85
  elsif klass
75
86
  # Typero.schema :user
76
- schema = Typero::Schema::SCHEMAS[klass]
87
+ schema = SCHEMAS[klass]
77
88
  schema ||= class_finder klass, :schema
78
- schema || nil
89
+
90
+ if opts
91
+ schema
92
+ else
93
+ schema.respond_to?(:[]) ? schema[0] : schema
94
+ end
79
95
  else
80
96
  raise ArgumentError, 'Schema type not defined.'
81
97
  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
4
+ version: 0.9.5
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-07-31 00:00:00.000000000 Z
11
+ date: 2022-08-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.22
80
+ rubygems_version: 3.2.3
80
81
  signing_key:
81
82
  specification_version: 4
82
83
  summary: Ruby type system