typero 0.5.2 → 0.9.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/.version +1 -1
  3. data/lib/adapters/sequel.rb +11 -33
  4. data/lib/typero.rb +8 -189
  5. data/lib/typero/params.rb +126 -0
  6. data/lib/typero/schema.rb +154 -0
  7. data/lib/typero/type/type.rb +125 -0
  8. data/lib/typero/type/types/boolean_type.rb +26 -0
  9. data/lib/typero/type/types/currency_type.rb +21 -0
  10. data/lib/typero/type/types/date_type.rb +35 -0
  11. data/lib/typero/type/types/datetime_type.rb +16 -0
  12. data/lib/typero/type/types/email_type.rb +20 -0
  13. data/lib/typero/type/types/float_type.rb +24 -0
  14. data/lib/typero/type/types/hash_type.rb +31 -0
  15. data/lib/typero/type/types/image_type.rb +22 -0
  16. data/lib/typero/type/types/integer_type.rb +16 -0
  17. data/lib/typero/type/types/label_type.rb +20 -0
  18. data/lib/typero/type/types/locale_type.rb +13 -0
  19. data/lib/typero/type/types/model_type.rb +23 -0
  20. data/lib/typero/type/{oib.rb → types/oib_type.rb} +16 -18
  21. data/lib/typero/type/types/point_type.rb +25 -0
  22. data/lib/typero/type/types/string_type.rb +20 -0
  23. data/lib/typero/type/types/text_type.rb +10 -0
  24. data/lib/typero/type/types/time_type.rb +5 -0
  25. data/lib/typero/type/types/timezone_type.rb +15 -0
  26. data/lib/typero/type/types/url_type.rb +13 -0
  27. data/lib/typero/typero.rb +98 -0
  28. metadata +33 -27
  29. data/lib/typero/type.rb +0 -32
  30. data/lib/typero/type/array.rb +0 -55
  31. data/lib/typero/type/boolean.rb +0 -16
  32. data/lib/typero/type/currency.rb +0 -17
  33. data/lib/typero/type/date.rb +0 -6
  34. data/lib/typero/type/datetime.rb +0 -6
  35. data/lib/typero/type/email.rb +0 -27
  36. data/lib/typero/type/float.rb +0 -27
  37. data/lib/typero/type/geography.rb +0 -13
  38. data/lib/typero/type/hash.rb +0 -18
  39. data/lib/typero/type/integer.rb +0 -25
  40. data/lib/typero/type/label.rb +0 -20
  41. data/lib/typero/type/point.rb +0 -25
  42. data/lib/typero/type/string.rb +0 -29
  43. data/lib/typero/type/text.rb +0 -7
  44. data/lib/typero/type/url.rb +0 -20
@@ -0,0 +1,25 @@
1
+ # for postgres - select st_asewkt(lon_lat) || st_astext(lon_lat)
2
+ # point = @object.class.xselect("ST_AsText(#{field}) as #{field}").where(id: @object.id).first[field.to_sym]
3
+
4
+ class Typero::PointType < Typero::Type
5
+ def set
6
+ if value.include?('/@')
7
+ point = value.split('/@', 2).last.split(',')
8
+ value { [point[0], point[1]].join(',') }
9
+ end
10
+
11
+ if !value.include?('POINT') && value.include?(',')
12
+ point = value.sub(/,\s*/, ' ')
13
+ value { 'SRID=4326;POINT(%s)' % point }
14
+ end
15
+
16
+ if value && value.include?(',') && !value =~ /^SRID=4326;POINT\(/
17
+ error_for(:unallowed_characters_error)
18
+ end
19
+ end
20
+
21
+ def db_schema
22
+ [:geography, {}]
23
+ end
24
+ end
25
+
@@ -0,0 +1,20 @@
1
+ class Typero::StringType < Typero::Type
2
+ opts :min, 'Minimun string length'
3
+ opts :max, 'Maximun string length'
4
+ opts :downcase, 'is the string in downcase?'
5
+
6
+ def set
7
+ value(&:to_s)
8
+ value(&:downcase) if opts[:downcase]
9
+
10
+ error_for(:min_length_error, opts[:min], value.length) if opts[:min] && value.length < opts[:min]
11
+ error_for(:max_length_error, opts[:max], value.length) if opts[:max] && value.length > opts[:max]
12
+ end
13
+
14
+ def db_schema
15
+ [:string, {
16
+ limit: @opts[:max] || 255
17
+ }]
18
+ end
19
+ end
20
+
@@ -0,0 +1,10 @@
1
+ require_relative 'string_type'
2
+
3
+ class Typero::TextType < Typero::StringType
4
+ opts :min, 'Minimun string length'
5
+ opts :max, 'Maximun string length'
6
+
7
+ def db_schema
8
+ [:text, {}]
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ require_relative 'datetime_type'
2
+
3
+ class Typero::TimeType < Typero::DatetimeType
4
+ end
5
+
@@ -0,0 +1,15 @@
1
+ class Typero::TimezoneType < Typero::Type
2
+ error :en, :invalid_time_zone, 'Invalid time zone'
3
+
4
+ def set
5
+ TZInfo::Timezone.get(value)
6
+ rescue TZInfo::InvalidTimezoneIdentifier
7
+ error_for :invalid_time_zone
8
+ end
9
+
10
+ def db_schema
11
+ [:string, { length: 50 }]
12
+ end
13
+
14
+ end
15
+
@@ -0,0 +1,13 @@
1
+ class Typero::UrlType < Typero::Type
2
+ error :en, :url_not_starting_error, 'URL is not starting with http or https'
3
+
4
+ def set
5
+ parts = value.split('://')
6
+ error_for(:url_not_starting_error) unless parts[1]
7
+ end
8
+
9
+ def db_schema
10
+ [:string, {}]
11
+ end
12
+ end
13
+
@@ -0,0 +1,98 @@
1
+ # rules = Typero.schema do
2
+ # set :name, String, req: true
3
+ # set :email, :email, req: true
4
+ # set :skills, [:email], min: 2
5
+ # end
6
+ #
7
+ # or
8
+ #
9
+ # rules = Typero.schema do
10
+ # string :name, req: true
11
+ # email :email, req: true
12
+ # email [:skills], min: 2
13
+ # end
14
+ #
15
+ # errors = rules.validate @object
16
+ # rules.valid?
17
+ # rules.validate(@object) { |errors| ... }
18
+
19
+ module Typero
20
+ extend self
21
+
22
+ VERSION = File.read File.expand_path "../../.version", File.dirname(__FILE__)
23
+
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
33
+ else
34
+ raise error
35
+ end
36
+ end
37
+
38
+ # load or set type schema
39
+ # Typero.schema(:blog) { ... }
40
+ # Typero.schema(:blog, type: :model) { ... }
41
+ # Typero.schema(:blog)
42
+ # Typero.schema(type: :model)
43
+ def schema name=nil, opts=nil, &block
44
+ klass = name.to_s.classify if name && !name.is_a?(Hash)
45
+
46
+ if block_given?
47
+ Typero::Schema.new(&block).tap do |schema|
48
+ if klass
49
+ Typero::Schema::SCHEMAS[klass] = schema
50
+
51
+ if opts && opts[:type]
52
+ Typero::Schema::TYPES[opts[:type]] ||= []
53
+ Typero::Schema::TYPES[opts[:type]].push klass unless Typero::Schema::TYPES[opts[:type]].include?(klass)
54
+ end
55
+ end
56
+ end
57
+ else
58
+ # Schema not given, get schema
59
+ if name.is_a?(Hash)
60
+ # Typero.schema type: :model
61
+ if type = name[:type]
62
+ Typero::Schema::TYPES[type]
63
+ end
64
+ elsif klass
65
+ # Typero.schema :user
66
+ schema = Typero::Schema::SCHEMAS[klass]
67
+ schema ||= class_finder klass, :schema
68
+ schema || nil
69
+ else
70
+ raise ArgumentError, 'Schema type not defined.'
71
+ end
72
+ end
73
+ end
74
+
75
+ def defined? name
76
+ Typero::Type.load name
77
+ true
78
+ rescue ArgumentError
79
+ false
80
+ end
81
+
82
+ private
83
+
84
+ # class_finder :user, :exporter, :representer
85
+ # find first UserExporter, User::Exporter, User::Representer, UserRepresenter
86
+ def class_finder *args
87
+ name = args.shift.to_s.classify
88
+
89
+ for el in args
90
+ for separator in ['_','/']
91
+ klass = [name, el].join(separator).classify
92
+ return klass.constantize if const_defined? klass
93
+ end
94
+ end
95
+
96
+ nil
97
+ end
98
+ end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: typero
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.9.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dino Reic
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-07-30 00:00:00.000000000 Z
11
+ date: 2021-01-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fast_blank
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1'
19
+ version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1'
26
+ version: '0'
27
27
  description: Simple and fast ruby type system. Enforce types as Array, Email, Boolean
28
28
  for ruby class instances
29
29
  email: reic.dino@gmail.com
@@ -34,28 +34,34 @@ files:
34
34
  - "./.version"
35
35
  - "./lib/adapters/sequel.rb"
36
36
  - "./lib/typero.rb"
37
- - "./lib/typero/type.rb"
38
- - "./lib/typero/type/array.rb"
39
- - "./lib/typero/type/boolean.rb"
40
- - "./lib/typero/type/currency.rb"
41
- - "./lib/typero/type/date.rb"
42
- - "./lib/typero/type/datetime.rb"
43
- - "./lib/typero/type/email.rb"
44
- - "./lib/typero/type/float.rb"
45
- - "./lib/typero/type/geography.rb"
46
- - "./lib/typero/type/hash.rb"
47
- - "./lib/typero/type/integer.rb"
48
- - "./lib/typero/type/label.rb"
49
- - "./lib/typero/type/oib.rb"
50
- - "./lib/typero/type/point.rb"
51
- - "./lib/typero/type/string.rb"
52
- - "./lib/typero/type/text.rb"
53
- - "./lib/typero/type/url.rb"
37
+ - "./lib/typero/params.rb"
38
+ - "./lib/typero/schema.rb"
39
+ - "./lib/typero/type/type.rb"
40
+ - "./lib/typero/type/types/boolean_type.rb"
41
+ - "./lib/typero/type/types/currency_type.rb"
42
+ - "./lib/typero/type/types/date_type.rb"
43
+ - "./lib/typero/type/types/datetime_type.rb"
44
+ - "./lib/typero/type/types/email_type.rb"
45
+ - "./lib/typero/type/types/float_type.rb"
46
+ - "./lib/typero/type/types/hash_type.rb"
47
+ - "./lib/typero/type/types/image_type.rb"
48
+ - "./lib/typero/type/types/integer_type.rb"
49
+ - "./lib/typero/type/types/label_type.rb"
50
+ - "./lib/typero/type/types/locale_type.rb"
51
+ - "./lib/typero/type/types/model_type.rb"
52
+ - "./lib/typero/type/types/oib_type.rb"
53
+ - "./lib/typero/type/types/point_type.rb"
54
+ - "./lib/typero/type/types/string_type.rb"
55
+ - "./lib/typero/type/types/text_type.rb"
56
+ - "./lib/typero/type/types/time_type.rb"
57
+ - "./lib/typero/type/types/timezone_type.rb"
58
+ - "./lib/typero/type/types/url_type.rb"
59
+ - "./lib/typero/typero.rb"
54
60
  homepage: https://github.com/dux/typero
55
61
  licenses:
56
62
  - MIT
57
63
  metadata: {}
58
- post_install_message:
64
+ post_install_message:
59
65
  rdoc_options: []
60
66
  require_paths:
61
67
  - lib
@@ -70,8 +76,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
70
76
  - !ruby/object:Gem::Version
71
77
  version: '0'
72
78
  requirements: []
73
- rubygems_version: 3.0.3
74
- signing_key:
79
+ rubygems_version: 3.0.6
80
+ signing_key:
75
81
  specification_version: 4
76
82
  summary: Ruby type system
77
83
  test_files: []
@@ -1,32 +0,0 @@
1
- class Typero::Type
2
- attr_accessor :opts
3
- attr_accessor :value
4
-
5
- def self.load name
6
- klass = 'Typero::%sType' % name.to_s.gsub(/[^\w]/,'').classify
7
- klass.constantize
8
- end
9
-
10
- ###
11
-
12
- def initialize value, opts={}
13
- @value = value
14
- @opts = opts
15
- end
16
-
17
- # default validation for any type
18
- def validate
19
- true
20
- end
21
-
22
- # get error from option or the default one
23
- def error_for name
24
- @opts[name] || send(name)
25
- end
26
-
27
- def default
28
- nil
29
- end
30
- end
31
-
32
-
@@ -1,55 +0,0 @@
1
- class Typero::ArrayType < Typero::Type
2
- def default
3
- []
4
- end
5
-
6
- def set
7
- unless @value.class.to_s.index('Array')
8
- @value = @value.to_s.sub(/^\{/,'').sub(/\}$/,'')
9
-
10
- # split on new line and comma by default
11
- @value = @value.split(/\s*[,\n]\s*/)
12
- end
13
-
14
- @value.uniq!
15
- @value.compact!
16
-
17
- if type = @opts[:array_type]
18
- @value.map! { |el|
19
- Typero.validate(type, el) { |msg|
20
- raise TypeError.new "'%s' %s (%s)" % [el, msg, value_in_list_error]
21
- }
22
- }
23
- end
24
-
25
- # this converts Sequel::Postgres::PGArray to Array and fixes many problems
26
- @value = @value.to_a if @value.class != Array
27
- end
28
-
29
- def validate
30
- raise TypeError, error_for(:min_length) % @opts[:min] if @opts[:min] && @value.length < @opts[:min]
31
- raise TypeError, error_for(:max_length) % @opts[:max] if @opts[:max] && @value.length > @opts[:max]
32
- true
33
- end
34
-
35
- def min_error
36
- 'min array lenght is %s elements'
37
- end
38
-
39
- def max_error
40
- 'max array lenght is %s elements'
41
- end
42
-
43
- def value_in_list_error
44
- 'value in list'
45
- end
46
-
47
- def db_field
48
- check = Typero::Type.load(@opts[:array_type]).new nil, {}
49
- schema = check.db_field
50
- schema[1] ||= {}
51
- schema[1].merge! array: true
52
- schema
53
- end
54
- end
55
-
@@ -1,16 +0,0 @@
1
- class Typero::BooleanType < Typero::Type
2
- def default
3
- false
4
- end
5
-
6
- def set
7
- @value = [true, 1, '1', 'true', 'on'].include?(@value) ? true : false
8
- end
9
-
10
- def db_field
11
- opts = {}
12
- opts[:default] = @opts[:default] || false
13
- [:boolean, opts]
14
- end
15
- end
16
-
@@ -1,17 +0,0 @@
1
- require_relative './float'
2
-
3
- class Typero::CurrencyType < Typero::FloatType
4
-
5
- def set
6
- @value = @value.to_f.round(2)
7
- end
8
-
9
- def db_field
10
- opts = {}
11
- opts[:precision] = 8
12
- opts[:scale] = 2
13
- [:decimal, opts]
14
- end
15
-
16
- end
17
-
@@ -1,6 +0,0 @@
1
- class Typero::DateType < Typero::Type
2
- def set
3
- @value = @value.to_s.downcase.gsub(/[^\d\-\.\s]/, '')
4
- end
5
- end
6
-
@@ -1,6 +0,0 @@
1
- class Typero::DatetimeType < Typero::Type
2
- def set
3
- @value = @value.to_s.downcase.gsub(/[^\d\-\.\s\:]/, '')
4
- end
5
- end
6
-
@@ -1,27 +0,0 @@
1
- class Typero::EmailType < Typero::Type
2
-
3
- def set
4
- @value = @value.downcase.gsub(/\s+/,'+')
5
- end
6
-
7
- def validate
8
- raise TypeError, error_for(:not_8_chars_error) unless @value.to_s.length > 7
9
- raise TypeError, error_for(:missing_monkey_error) unless @value.include?('@')
10
- end
11
-
12
- def not_8_chars_error
13
- 'is not having at least 8 characters'
14
- end
15
-
16
- def missing_monkey_error
17
- 'is missing @'
18
- end
19
-
20
- def db_field
21
- opts = {}
22
- opts[:limit] = @opts[:max] || 120
23
- opts[:null] = false if @opts[:req]
24
- [:string, opts]
25
- end
26
- end
27
-