vmodel 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/Gemfile +4 -0
- data/README.md +36 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/vmodel/validator.rb +7 -0
- data/lib/vmodel/validators/integer_validator.rb +28 -0
- data/lib/vmodel/version.rb +3 -0
- data/lib/vmodel.rb +123 -0
- data/vmodel.gemspec +29 -0
- metadata +82 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f87c0c505239ef6b95ab97b16f179d44b1627141
|
4
|
+
data.tar.gz: c83b727f3cf2424999ca5cba3ad377187248dfda
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0560d475d6e4a02876249c7896577b9bf4f9cba5eb386d921ad65e821dc3ef3b582342da3b257911eaef4538a84eac7b4b96c6f71a6e4bdff96b42e6b8c2d0ef
|
7
|
+
data.tar.gz: 7406b69828f903fcb5da5ae36b33c54e72603a895ff8fc1b8c57fc0ef08ed029aa881ce2f310f129281f20dd0411399744aca487c82ccc25df588bea89a1b3fd
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# Vmodel
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/vmodel`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'vmodel'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install vmodel
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/vmodel.
|
36
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "vmodel"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
module Vmodel
|
2
|
+
class IntegerValidator < Validator
|
3
|
+
def validate(attr, options)
|
4
|
+
if attr.nil? && options[:required]
|
5
|
+
return [false, "is required"]
|
6
|
+
end
|
7
|
+
if attr.nil? && options[:default]
|
8
|
+
attr = options[:default]
|
9
|
+
end
|
10
|
+
if options[:cast]
|
11
|
+
attr = Integer(attr)
|
12
|
+
end
|
13
|
+
return [false, "is not an integer"] if !attr.is_a?(Integer)
|
14
|
+
options.each do |k,v|
|
15
|
+
case k
|
16
|
+
when :gte
|
17
|
+
return [false, "should be great than #{v} or equal to #{v}"] if attr < v
|
18
|
+
when :lte
|
19
|
+
return [false, "should be less than #{v} or equal to #{v}"] if attr > v
|
20
|
+
when :gt
|
21
|
+
return [false, "should be great than #{v}"] if attr <= v
|
22
|
+
when :lt
|
23
|
+
return [false, "should be less than #{v}"] if attr >= v
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/vmodel.rb
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
require "vmodel/version"
|
2
|
+
require "vmodel/validator"
|
3
|
+
require "vmodel/validators/integer_validator"
|
4
|
+
module Vmodel
|
5
|
+
|
6
|
+
def self.included(base)
|
7
|
+
base.extend ClassMethods
|
8
|
+
base.include InstanceMethods
|
9
|
+
base.instance_variable_set(:@_config, {})
|
10
|
+
base.instance_variable_set(:@_allow_attributes, [])
|
11
|
+
base.instance_variable_set(:@_validations, {})
|
12
|
+
end
|
13
|
+
|
14
|
+
module ClassMethods
|
15
|
+
def wild(flag)
|
16
|
+
@_config[:wild_mod] = flag
|
17
|
+
end
|
18
|
+
|
19
|
+
def wild_mod
|
20
|
+
@_config[:wild_mod]
|
21
|
+
end
|
22
|
+
|
23
|
+
def allow_attributes(*argv)
|
24
|
+
if !wild_mod
|
25
|
+
@_allow_attributes = argv.map(&:to_s)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def allow_attributes
|
30
|
+
@_allow_attributes
|
31
|
+
end
|
32
|
+
|
33
|
+
def validates(attribute, type, options={})
|
34
|
+
if !@_allow_attributes.include?(attribute.to_s)
|
35
|
+
@_allow_attributes << attribute.to_s
|
36
|
+
end
|
37
|
+
@_validations[attribute.to_s]={type: type, options: options}
|
38
|
+
end
|
39
|
+
|
40
|
+
def validations
|
41
|
+
@_validations
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
module InstanceMethods
|
46
|
+
|
47
|
+
def initialize(attributes = {})
|
48
|
+
@_attributes = {}
|
49
|
+
@_errors = []
|
50
|
+
|
51
|
+
if self.class.wild_mod
|
52
|
+
attributes.each do |k,v|
|
53
|
+
@_attributes[k.to_s] = v
|
54
|
+
end
|
55
|
+
else
|
56
|
+
self.class.allow_attributes.each do |item|
|
57
|
+
@_attributes[item] = attributes[item] || attributes[item.to_sym]
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def validate!
|
63
|
+
self.class.validations.each do |k,v|
|
64
|
+
validate(k, @_attributes[k], v)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def valid?
|
69
|
+
errors.size == 0
|
70
|
+
end
|
71
|
+
|
72
|
+
def errors
|
73
|
+
validate!
|
74
|
+
@_errors
|
75
|
+
end
|
76
|
+
|
77
|
+
def attributes
|
78
|
+
@_attributes
|
79
|
+
end
|
80
|
+
|
81
|
+
def method_missing(name, *argv, &block)
|
82
|
+
attr = name.to_s.gsub("=","")
|
83
|
+
if name.to_s.end_with?("=")
|
84
|
+
if @_attributes.include?(attr)
|
85
|
+
@_attributes[attr] = argv[0]
|
86
|
+
return argv[0]
|
87
|
+
elsif self.class.wild_mod
|
88
|
+
return @_attributes[attr] = argv[0]
|
89
|
+
end
|
90
|
+
else
|
91
|
+
if @_attributes.include?(attr)
|
92
|
+
return @_attributes[attr]
|
93
|
+
end
|
94
|
+
return self.class.wild_mod ? nil : super
|
95
|
+
end
|
96
|
+
super
|
97
|
+
end
|
98
|
+
|
99
|
+
def respond_to?(method)
|
100
|
+
if wild_mod
|
101
|
+
return true
|
102
|
+
end
|
103
|
+
attr = method.to_s.gsub("=","")
|
104
|
+
@_attributes.include?(attr)
|
105
|
+
end
|
106
|
+
private
|
107
|
+
def validate(attr, value, rule)
|
108
|
+
if rule[:type].is_a? Vmodel::Validator
|
109
|
+
result, message = rule[:type].new.validate(value, rule[:options])
|
110
|
+
if !result
|
111
|
+
@_errors << {attr => message}
|
112
|
+
end
|
113
|
+
else
|
114
|
+
validator_name = rule[:type].to_s+"Validator"
|
115
|
+
validator = Vmodel.const_get validator_name
|
116
|
+
result, message = validator.new.validate(value, rule[:options])
|
117
|
+
if !result
|
118
|
+
@_errors << {attr => message}
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
data/vmodel.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'vmodel/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "vmodel"
|
8
|
+
spec.version = Vmodel::VERSION
|
9
|
+
spec.authors = ["Jerry"]
|
10
|
+
spec.email = ["taojay315@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Validate Models.}
|
13
|
+
spec.description = %q{Only used for validate models.}
|
14
|
+
spec.homepage = "http://blog.jerry-tao.com"
|
15
|
+
|
16
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
17
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
18
|
+
|
19
|
+
|
20
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
21
|
+
f.match(%r{^(test|spec|features)/})
|
22
|
+
end
|
23
|
+
spec.bindir = "exe"
|
24
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
25
|
+
spec.require_paths = ["lib"]
|
26
|
+
|
27
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
28
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: vmodel
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jerry
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-06-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.14'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.14'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: Only used for validate models.
|
42
|
+
email:
|
43
|
+
- taojay315@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- README.md
|
51
|
+
- Rakefile
|
52
|
+
- bin/console
|
53
|
+
- bin/setup
|
54
|
+
- lib/vmodel.rb
|
55
|
+
- lib/vmodel/validator.rb
|
56
|
+
- lib/vmodel/validators/integer_validator.rb
|
57
|
+
- lib/vmodel/version.rb
|
58
|
+
- vmodel.gemspec
|
59
|
+
homepage: http://blog.jerry-tao.com
|
60
|
+
licenses: []
|
61
|
+
metadata: {}
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
requirements: []
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 2.5.1
|
79
|
+
signing_key:
|
80
|
+
specification_version: 4
|
81
|
+
summary: Validate Models.
|
82
|
+
test_files: []
|