typero 0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/typero.rb +89 -0
  3. metadata +45 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 92cf598bbfb6f6b241b3f9d54e74ff8354f1a31c
4
+ data.tar.gz: 7d87b59b9fdee781f18ffebee6bfc161b81791a1
5
+ SHA512:
6
+ metadata.gz: 56d648431f62c1a64e47ac39f70e0cb305bef0ff261fae06d476b5d8abb20a3e03e59dccb217f19b265469738b0097f343ff908c35a60f307e47608f9bb838f8
7
+ data.tar.gz: 0339f991c56ca2690c74041e2559ac9501fedf3b5d85b5e88d11406fe3029e97ffbb697f1e1198c7e3df669c7928f81704f7c5b56d8d8446d1ff74bacec6098b
@@ -0,0 +1,89 @@
1
+ # attribute :url, :url
2
+ # attribute :name, req:'Link name is required'
3
+ # attribute :tags, Array[:label]
4
+ # attribute :bucket_id, Integer, req:'Bucket is not assigned'
5
+ # attribute :kind, String, allow:[:doc, :img, :vid, :lnk, :art], default:'lnk'
6
+ # attribute :email, :email, req:true, uniq:"Email is allready registred", protected:"You are not allowed to change the email"
7
+
8
+ # Typero.validate('dux@dux.net', :email)
9
+
10
+ module Typero
11
+ extend self
12
+
13
+ @opts ||= {}
14
+
15
+ def opts
16
+ @opts
17
+ end
18
+
19
+ # called to define instance variable in class
20
+ # Typero.define_instance(self, :email, { :type=>:email })
21
+ def define_instance(klass, name, opts={})
22
+ type = opts[:type]
23
+ opts[:name] = name
24
+
25
+ type_klass = "Typero::#{type.to_s.classify}Type".constantize
26
+ @opts[klass.to_s] ||= {}
27
+ @opts[klass.to_s][name] = type_klass.new(opts).freeze
28
+
29
+ klass.class_eval %[
30
+ def #{name}
31
+ Typero.opts['#{klass}'][:#{name}].get_value(self)
32
+ end
33
+
34
+ def #{name}=(value=nil)
35
+ Typero.opts['#{klass}'][:#{name}].set_value(self, value)
36
+ end
37
+ ]
38
+ end
39
+
40
+ def last_error
41
+ Thread.current[:validate_last_error]
42
+ end
43
+
44
+ # Typero.validate('duxdux.net', :email) # raise erorr
45
+ # Typero.validate('duxdux.net', :email) { |msg| error(msg) } # capture
46
+ def validate!(value, type, opts={}, &block)
47
+ type_klass = "Typero::#{type.to_s.classify}Type".constantize
48
+ type_klass.new(opts).validate(value)
49
+ true
50
+ rescue TypeError
51
+ if block
52
+ block.call($!.message)
53
+ return false
54
+ end
55
+ raise(TypeError, $!.message)
56
+ end
57
+
58
+ # Typero.validate('duxdux.net', :email) -> false
59
+ def validate(*args)
60
+ validate!(*args)
61
+ true
62
+ rescue
63
+ false
64
+ end
65
+
66
+ # Typero.quick_set('[invalid-to-fix]', :label)
67
+ def quick_set(value, type, opts={})
68
+ type_klass = "Typero::#{type.to_s.classify}Type".constantize
69
+ type_inst = type_klass.new(opts)
70
+ type_inst.respond_to?(:get_set) ? type_inst.get_set(value) : set(value)
71
+ end
72
+
73
+
74
+ # Typero.validate_opts params, :ttl, :password
75
+ # only :ttl, :password and allowed in hash
76
+ def validate_opts(hash, *args)
77
+ for key in (hash || {}).keys
78
+ raise TypeError, "Key :#{key} is not allowed in hash" unless args.index(key)
79
+ end
80
+ hash
81
+ end
82
+ end
83
+
84
+ require './lib/typero/instance'
85
+ require './lib/typero/schema'
86
+ require './lib/typero/type'
87
+ require './lib/typero/hash'
88
+
89
+ Dir['lib/typero/type/*.rb'].each{ |f| require "./#{f.sub('.rb','')}" }
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: typero
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.2'
5
+ platform: ruby
6
+ authors:
7
+ - Dino Reic
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-10-09 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Simple and fast ruby type system. Enforce types as Array, Email, Boolean
14
+ for ruby class instances
15
+ email: reic.din@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/typero.rb
21
+ homepage: http://rubygems.org/dux/typero
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.5.1
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Ruby type system
45
+ test_files: []