va 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 842b924ccdb0e783a6452a0112a8a151e0f0905e
4
+ data.tar.gz: adaff6c5ef19f94720f44d8052a4d67cd9a6c5cf
5
+ SHA512:
6
+ metadata.gz: 0890f8a6c210a77e31402f664e2923cb12da3eb511c05a512692d71f6473ef39af0801c06172d8def6d1a5c86b8f811cbb20dc1dbfa3376322f067a9f171930f
7
+ data.tar.gz: 68c368b22efcfecbedc1f4dd82f9155dd43bb90157ccd4dffe73ea866d3044a839bc43aa89fa09b15a188e7799d80f9a6b8e2e036ee7bd07a65ed053b7e8792e
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+ gemspec
3
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Federico Iachetti
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # Va
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'va'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install va
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/va/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/lib/va/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ module Va
2
+ VERSION = "0.0.3"
3
+ end
data/lib/va.rb ADDED
@@ -0,0 +1,54 @@
1
+ require "va/version"
2
+
3
+ module Va
4
+ class Model
5
+ attr_reader :attributes
6
+
7
+ def initialize(**args)
8
+ @attributes = {}
9
+ args.each do |k, v|
10
+ key = k.to_sym
11
+ @attributes[key] = v if self.class.keys.include?(key)
12
+ end
13
+ end
14
+
15
+ def valid?
16
+ self.class.validations.all? { |attrs, validation|
17
+ validation.call(attrs.map { |attr| self.send(attr)})
18
+ }
19
+ end
20
+
21
+ private
22
+ def self.validations
23
+ @validations ||= []
24
+ end
25
+
26
+ def self.validate(*attrs, &block)
27
+ attrs.each do |attr|
28
+ raise UnknownAttribute unless keys.include?(attr)
29
+ end
30
+ validations << [attrs, block]
31
+ end
32
+
33
+ def self.keys
34
+ @keys ||= []
35
+ end
36
+
37
+ def self.attribute(attr_name)
38
+ name = attr_name.to_sym
39
+
40
+ self.keys << name
41
+
42
+ define_method "#{name}=" do |value|
43
+ attributes[name] = value
44
+ end
45
+
46
+ define_method "#{name}" do
47
+ attributes[name]
48
+ end
49
+ end
50
+ end
51
+ class UnknownAttribute < Exception; end
52
+ end
53
+
54
+
@@ -0,0 +1 @@
1
+ require File.expand_path("../../lib/va", __FILE__)
data/spec/va_spec.rb ADDED
@@ -0,0 +1,92 @@
1
+ require "./spec/spec_helper"
2
+
3
+ scope do
4
+ class Login < Va::Model
5
+ attribute :email
6
+ attribute :pass
7
+ end
8
+
9
+ test "init" do
10
+ va = Login.new(email: "fede@example.com", pass: "123456")
11
+ assert_equal va.email, "fede@example.com"
12
+ assert_equal va.pass, "123456"
13
+ end
14
+
15
+ scope "#attributes" do
16
+ test "all" do
17
+ va = Login.new(email: "fede@example.com", pass: "123456")
18
+ assert_equal va.attributes, { email: "fede@example.com", pass: "123456" }
19
+ end
20
+
21
+ test "some" do
22
+ va = Login.new(email: "fede@example.com")
23
+ assert_equal va.attributes, { email: "fede@example.com" }
24
+ end
25
+
26
+ test "spureous" do
27
+ va = Login.new(email: "fede@example.com", i_dont_belong_here: "HELLO!")
28
+ assert_equal va.attributes, { email: "fede@example.com" }
29
+ end
30
+ end
31
+ end
32
+ scope "validations" do
33
+ test "basic passing validation" do
34
+ class VeryValid < Va::Model
35
+ validate do
36
+ true
37
+ end
38
+ end
39
+
40
+ va = VeryValid.new
41
+ assert_equal va.valid?, true
42
+ end
43
+
44
+ test "basic passing validation" do
45
+ class VeryInvalid < Va::Model
46
+ validate do
47
+ false
48
+ end
49
+ end
50
+
51
+ va = VeryInvalid.new
52
+ assert_equal va.valid?, false
53
+ end
54
+
55
+ scope "A range validation" do
56
+ class MyRange < Va::Model
57
+ attribute :from
58
+ attribute :to
59
+
60
+ validate(:from, :to) do |from, to|
61
+ from < to
62
+ end
63
+ end
64
+
65
+ test "valid" do
66
+ r = MyRange.new(from: 1, to: 5)
67
+ assert_equal r.valid?, true
68
+ end
69
+
70
+ test do
71
+ r = MyRange.new(from: 10, to: 5)
72
+ assert_equal r.valid?, false
73
+ end
74
+ end
75
+
76
+ scope "can't validate" do
77
+ test "invalid arguments" do
78
+ begin
79
+ class FaceValidator < Va::Model
80
+ attribute :face
81
+
82
+ validate(:leg) do |leg|
83
+ true
84
+ end
85
+ end
86
+ assert nil
87
+ rescue Object => e
88
+ assert_equal e.class, Va::UnknownAttribute
89
+ end
90
+ end
91
+ end
92
+ end
data/va.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'va/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "va"
8
+ spec.version = Va::VERSION
9
+ spec.authors = ["Federico Iachetti"]
10
+ spec.email = ["iachetti.federico@gmail.com"]
11
+ spec.summary = %q{Minimalistic framework agnostic Validator.}
12
+ spec.description = %q{Minimalistic framework agnostic Validator.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(spec)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+
24
+ spec.add_development_dependency "cutest", "~> 1.2.2"
25
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: va
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Federico Iachetti
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-25 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
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
+ - !ruby/object:Gem::Dependency
42
+ name: cutest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.2.2
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 1.2.2
55
+ description: Minimalistic framework agnostic Validator.
56
+ email:
57
+ - iachetti.federico@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/va.rb
69
+ - lib/va/version.rb
70
+ - spec/spec_helper.rb
71
+ - spec/va_spec.rb
72
+ - va.gemspec
73
+ homepage: ''
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.4.3
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Minimalistic framework agnostic Validator.
97
+ test_files:
98
+ - spec/spec_helper.rb
99
+ - spec/va_spec.rb