typero 0.9.5 → 0.9.6

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: 0bcd151d7d7b875c6db7aec28c85c3abcf10596a4f596622050d7e18cdb81e9e
4
- data.tar.gz: d54b9c76d6587e5872f19deba20febd7430e19022fe869a1360339c8156205d9
3
+ metadata.gz: 54d18d3e66cf73fd0d6f6c42f319cb744fb3cc3f5e11e5e4a24b961c36792a34
4
+ data.tar.gz: 65f87c041f1a498c82d707d897d93eab7294a0d373a4d9aca5ec9aefe5da27b7
5
5
  SHA512:
6
- metadata.gz: 5b639372e86d329ef150809f52f964a459c3faf369a86d219292a2af6b8364ed5c2745079d409944472ec59316cae690e0f1d554cecdeacb625bb5ccf4b39f29
7
- data.tar.gz: 0e887a10b853325c7b27f3c90ab57b3c1987d38576e3f1128fb27eb4982d2aaefdbf6e5ded9e80a75c7085c5674686196b13653eed62db853d6d50fe0c15ad39
6
+ metadata.gz: 1a63e1a470f0e21162f3c11e40fe4977b4432bb30d3f154330840f344b1dc91c49dc453213139b8e5299f4afe587ae3bf6427fa0aabedd0cc0910188e6463ca5
7
+ data.tar.gz: 0dd2e24597fa25a0543b851fa3218fc69e129fa671c3ed52edf6110994876c14e3ceb88002ebfec130782722b51dee184cf30dbb0012384da9eba7654d8db24c
data/.version CHANGED
@@ -1 +1 @@
1
- 0.9.5
1
+ 0.9.6
data/lib/typero/params.rb CHANGED
@@ -1,4 +1,5 @@
1
- # Base class for schema validation
1
+ # Base class for schema validation.
2
+ # Accepts set of params and returns hash of porsed rules
2
3
 
3
4
  module Typero
4
5
  class Params
@@ -114,7 +115,7 @@ module Typero
114
115
  # db :timestamps
115
116
  # db :add_index, :code -> t.add_index :code
116
117
  def db *args
117
- @db_rules.push args
118
+ @db_rules.push args.unshift(:db_rule!)
118
119
  end
119
120
 
120
121
  # set :age, type: :integer -> integer :age
@@ -126,4 +127,4 @@ module Typero
126
127
  set field, *args, &block
127
128
  end
128
129
  end
129
- end
130
+ end
data/lib/typero/schema.rb CHANGED
@@ -1,14 +1,51 @@
1
+ # Typero.schema :user, type: :model do
2
+
3
+ # Typero.schema :some_name, type: :model, db: DB_LOG do
4
+ # set :name, String, req: true
5
+ # set :email, :email, req: true
6
+ # set :emails, [:email], min: 2
7
+ # end
8
+ #
9
+ # rules = Typero.schema :some_name
10
+ #
11
+ # or
12
+ #
13
+ # rules = Typero.schema do
14
+ # string :name, req: true # generic email string
15
+ # email :email, req: true # string of type email
16
+ # emails [:skills], min: 2 # list of emails in filed named "emails"
17
+ # end
18
+ #
19
+ # errors = rules.validate (@object || @hash)
20
+ # rules.valid? (@object)
21
+ # rules.validate(@object) {|errors| ... }
22
+
1
23
  module Typero
2
24
  class Schema
25
+ SCHEMA_STORE ||= {}
26
+
27
+ attr_reader :klass
28
+ attr_reader :schema
29
+ attr_reader :opts
30
+
3
31
  # accepts dsl block to
4
- def initialize &block
5
- raise "Params not defined" unless block_given?
6
- @schema = Params.new &block
32
+ def initialize name, opts = nil, &block
33
+ if block
34
+ @opts = opts || {}
35
+ @schema = Params.new &block
36
+
37
+ if name
38
+ @klass = name
39
+ SCHEMA_STORE[name] = self
40
+ end
41
+ else
42
+ raise "Use Typero.schema(:name) to load stored schema"
43
+ end
7
44
  end
8
45
 
9
46
  # validates any instance object with hash variable interface
10
47
  # it also coarces values
11
- def validate object, options=nil
48
+ def validate object, options = nil
12
49
  @options = options || {}
13
50
  @object = object
14
51
  @errors = {}
@@ -99,7 +136,7 @@ module Typero
99
136
  schema_opts = @schema.rules[field]
100
137
  opts[:array] = true if schema_opts[:array]
101
138
 
102
- total << [type, field, opts]
139
+ total << [field, type, opts]
103
140
  end
104
141
 
105
142
  out += @schema.db_rules
@@ -71,6 +71,10 @@ module Typero
71
71
 
72
72
  false
73
73
  end
74
+
75
+ def db_schema
76
+ new(nil).db_schema
77
+ end
74
78
  end
75
79
 
76
80
  ###
data/lib/typero/typero.rb CHANGED
@@ -1,26 +1,7 @@
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
1
  module Typero
20
2
  extend self
21
3
 
22
4
  VERSION ||= File.read File.expand_path "../../.version", File.dirname(__FILE__)
23
- SCHEMAS ||= {}
24
5
 
25
6
  # check and coerce value
26
7
  # Typero.type(:label) -> Typero::LabelType
@@ -55,45 +36,30 @@ module Typero
55
36
  # get schema
56
37
  # Typero.schema(:blog)
57
38
 
58
- # get schema with options (as array)
59
- # Typero.schema(:blog, :with_schema)
60
-
61
- # get all schema names with type: model
39
+ # get all schema that matches any option
62
40
  # Typero.schema(type: :model)
63
41
  def schema name = nil, opts = nil, &block
64
42
  klass = name.to_s.classify if name && !name.is_a?(Hash)
65
43
 
66
44
  if block_given?
67
- Typero::Schema.new(&block).tap do |schema|
68
- if klass
69
- SCHEMAS[klass] = [schema, opts || {}]
70
- end
71
- end
45
+ # Typero.schema type: :model, db: DB_LOG do
46
+ Typero::Schema.new(klass, opts, &block)
72
47
  else
73
- # Schema not given, get schema
74
48
  if name.is_a?(Hash)
75
- # Typero.schema type: :model
49
+ # Schema not given, get schema
50
+ # Typero.schema type: :model
76
51
  out = []
77
52
 
78
- for key, _ in SCHEMAS
79
- schema, opts = _
80
- next unless opts[name.keys.first] == name.values.first
81
- out.push key.classify
53
+ for schema in Schema::SCHEMA_STORE.values
54
+ if schema.opts[name.keys.first] == name.values.first
55
+ out.push schema.klass
56
+ end
82
57
  end
83
58
 
84
59
  out
85
- elsif klass
86
- # Typero.schema :user
87
- schema = SCHEMAS[klass]
88
- schema ||= class_finder klass, :schema
89
-
90
- if opts
91
- schema
92
- else
93
- schema.respond_to?(:[]) ? schema[0] : schema
94
- end
95
60
  else
96
- raise ArgumentError, 'Schema type not defined.'
61
+ # Typero.schema :user
62
+ Typero::Schema::SCHEMA_STORE[klass] || raise('Schema "%s" not found' % klass)
97
63
  end
98
64
  end
99
65
  end
@@ -104,21 +70,4 @@ module Typero
104
70
  rescue ArgumentError
105
71
  false
106
72
  end
107
-
108
- private
109
-
110
- # class_finder :user, :exporter, :representer
111
- # find first UserExporter, User::Exporter, User::Representer, UserRepresenter
112
- def class_finder *args
113
- name = args.shift.to_s.classify
114
-
115
- for el in args
116
- for separator in ['_','/']
117
- klass = [name, el].join(separator).classify
118
- return klass.constantize if const_defined? klass
119
- end
120
- end
121
-
122
- nil
123
- end
124
73
  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.5
4
+ version: 0.9.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dino Reic
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-08-20 00:00:00.000000000 Z
11
+ date: 2022-12-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fast_blank