typero 0.4.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ class Typero::LabelType < Typero::Type
2
+ def set
3
+ value do |data|
4
+ data
5
+ .to_s
6
+ .gsub(/\s+/,'-')
7
+ .gsub(/[^\w\-]/,'')
8
+ .gsub(/\-+/, '-')[0,30]
9
+ .downcase
10
+ end
11
+
12
+ error_for(:unallowed_characters_error) unless value =~ /^[\w\-]+$/
13
+ end
14
+
15
+ def db_schema
16
+ [:string, {
17
+ limit: 30
18
+ }]
19
+ end
20
+ end
@@ -0,0 +1,21 @@
1
+ class Typero::ModelType < Typero::Type
2
+ def set
3
+ value(&:to_h)
4
+
5
+ errors = []
6
+
7
+ schema = Typero.schema(opts[:model])
8
+ schema.validate(value) do |field, error|
9
+ errors.push '%s (%s)' % [error, field]
10
+ end
11
+
12
+ raise TypeError.new errors.join(', ') if errors.first
13
+ end
14
+
15
+ def db_schema
16
+ [:jsonb, {
17
+ null: false
18
+ }]
19
+ end
20
+ end
21
+
@@ -1,4 +1,20 @@
1
1
  class Typero::OibType < Typero::Type
2
+ error :en, :not_an_oib_error, 'not in an OIB format'
3
+
4
+ def set
5
+ value do |data|
6
+ check?(data) ? data.to_i : error_for(:not_an_oib_error)
7
+ end
8
+ end
9
+
10
+ def db_schema
11
+ [:string, {
12
+ limit: 11
13
+ }]
14
+ end
15
+
16
+ private
17
+
2
18
  # http://domagoj.eu/oib/
3
19
  def check? oib
4
20
  oib = oib.to_s
@@ -19,16 +35,5 @@ class Typero::OibType < Typero::Type
19
35
  return control_sum == oib.at(10).to_i
20
36
  end
21
37
 
22
- def set
23
- @value = check?(@value) ? @value.to_i : nil
24
- end
25
-
26
- def validate
27
- raise TypeError.new(error_for(:not_an_oib_error)) unless check?(@value)
28
- end
29
-
30
- def not_an_oib_error
31
- 'not in an OIB format'
32
- end
33
38
  end
34
39
 
@@ -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,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,90 @@
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 type schema
39
+ def schema name=nil, &block
40
+ # :user -> 'User'
41
+ name = name.to_s.classify if name
42
+
43
+ if block_given?
44
+ Typero::Schema.new(&block).tap do |schema|
45
+ Typero::Schema::SCHEMAS[name] = schema if name
46
+ end
47
+ else
48
+ raise ArgumentErorr.new('Schema name not given') unless name
49
+
50
+ schema = Typero::Schema::SCHEMAS[name]
51
+ schema ||= class_finder name, :schema
52
+ schema || raise('Typero schema "%s" not defined' % name)
53
+ end
54
+ end
55
+
56
+ def export model, opts={}, &block
57
+ if block_given?
58
+ Exporter::EXPORTERS[model.to_s.classify] = block
59
+ else
60
+ Exporter.new(model, opts).render
61
+ end
62
+ end
63
+
64
+ def defined? name
65
+ Typero::Type.load name
66
+ true
67
+ rescue ArgumentError
68
+ false
69
+ end
70
+
71
+ private
72
+
73
+ # class_finder :user, :exporter, :representer
74
+ # find first UserExporter, User::Exporter, User::Representer, UserRepresenter
75
+ def class_finder *args
76
+ name = args.shift.to_s.classify
77
+
78
+ for el in args
79
+ for separator in ['_','/']
80
+ klass = [name, el].join(separator).classify
81
+
82
+ begin
83
+ return klass.constantize
84
+ rescue NameError => e
85
+ nil
86
+ end
87
+ end
88
+ end
89
+ end
90
+ 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.4.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dino Reic
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-07-11 00:00:00.000000000 Z
11
+ date: 2020-06-02 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
@@ -32,18 +32,29 @@ extensions: []
32
32
  extra_rdoc_files: []
33
33
  files:
34
34
  - "./.version"
35
+ - "./lib/adapters/sequel.rb"
35
36
  - "./lib/typero.rb"
36
- - "./lib/typero/type.rb"
37
- - "./lib/typero/type/array.rb"
38
- - "./lib/typero/type/boolean.rb"
39
- - "./lib/typero/type/email.rb"
40
- - "./lib/typero/type/float.rb"
41
- - "./lib/typero/type/hash.rb"
42
- - "./lib/typero/type/integer.rb"
43
- - "./lib/typero/type/label.rb"
44
- - "./lib/typero/type/oib.rb"
45
- - "./lib/typero/type/string.rb"
46
- - "./lib/typero/type/url.rb"
37
+ - "./lib/typero/exporter.rb"
38
+ - "./lib/typero/params.rb"
39
+ - "./lib/typero/schema.rb"
40
+ - "./lib/typero/type/type.rb"
41
+ - "./lib/typero/type/types/boolean_type.rb"
42
+ - "./lib/typero/type/types/currency_type.rb"
43
+ - "./lib/typero/type/types/date_type.rb"
44
+ - "./lib/typero/type/types/datetime_type.rb"
45
+ - "./lib/typero/type/types/email_type.rb"
46
+ - "./lib/typero/type/types/float_type.rb"
47
+ - "./lib/typero/type/types/hash_type.rb"
48
+ - "./lib/typero/type/types/image_type.rb"
49
+ - "./lib/typero/type/types/integer_type.rb"
50
+ - "./lib/typero/type/types/label_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/url_type.rb"
57
+ - "./lib/typero/typero.rb"
47
58
  homepage: https://github.com/dux/typero
48
59
  licenses:
49
60
  - MIT
@@ -63,8 +74,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
63
74
  - !ruby/object:Gem::Version
64
75
  version: '0'
65
76
  requirements: []
66
- rubyforge_project:
67
- rubygems_version: 2.7.5
77
+ rubygems_version: 3.0.6
68
78
  signing_key:
69
79
  specification_version: 4
70
80
  summary: Ruby type system
@@ -1,39 +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(what)
19
- true
20
- end
21
-
22
- def error_for name
23
- @opts[name] || send(name)
24
- end
25
-
26
- def get
27
- @value
28
- end
29
-
30
- def set
31
- @value
32
- end
33
-
34
- def default
35
- nil
36
- end
37
- end
38
-
39
-
@@ -1,44 +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(/\}$/,'').split(/\s*,\s*/)
9
- end
10
-
11
- @value.uniq!
12
- @value.compact!
13
-
14
- if type = @opts[:array_type]
15
- @value.map! { |el|
16
- Typero.validate(type, el) { |msg|
17
- raise TypeError.new "'%s' %s (%s)" % [el, msg, value_in_list_error]
18
- }
19
- }
20
- end
21
-
22
- # this converts Sequel::Postgres::PGArray to Array and fixes many problems
23
- @value = @value.to_a if @value.class != Array
24
- end
25
-
26
- def validate
27
- raise TypeError, error_for(:min_length) % @opts[:min] if @opts[:min] && @value.length < @opts[:min]
28
- raise TypeError, error_for(:max_length) % @opts[:max] if @opts[:max] && @value.length > @opts[:max]
29
- true
30
- end
31
-
32
- def min_error
33
- 'min array lenght is %s elements'
34
- end
35
-
36
- def max_error
37
- 'max array lenght is %s elements'
38
- end
39
-
40
- def value_in_list_error
41
- 'value in list'
42
- end
43
- end
44
-
@@ -1,10 +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
- end
10
-
@@ -1,21 +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
- end
21
-