avalon 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Ryo NAKAMURA
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,47 @@
1
+ # Avalon
2
+
3
+ A validator implementation for Ruby.
4
+ Avalon provides simple validation methods and validator class.
5
+
6
+ ## Requirements
7
+ Avalon is implemented in pure ruby without any gems.
8
+ Tested in following environments:
9
+ * MRI 1.8.7
10
+ * MRI 1.9.2
11
+ * MRI 1.9.3
12
+
13
+ ## Usage
14
+
15
+ ```
16
+ $ gem install avalon
17
+ ```
18
+
19
+ ```ruby
20
+ require "avalon"
21
+
22
+ Avalon.valid?("abc", /d/) #=> false
23
+ Avalon.invalid?("abc", /d/) #=> true
24
+ Avalon.validate("abc", /d/) #=> "abc" must match /d/ (Avalon::ValidationError)
25
+
26
+ Avalon.valid?(:foo => "bar") { |target| target.has_key?(:foo) } #=> true
27
+ Avalon.valid?(:foo => "bar") { has_key?(:foo) } #=> true
28
+
29
+ class Calculator
30
+ include Avalon
31
+
32
+ def square(number)
33
+ validate(number, Fixnum, /^\d+$/)
34
+ number.to_i ** 2
35
+ end
36
+ end
37
+
38
+ calculator = Calculator.new
39
+ calculator.square(3) #=> 9
40
+ calculator.square("3") #=> 9
41
+ calculator.square("three") #=> "three" must match Fixnum or /^\d+$/ (Avalon::ValidationError)
42
+
43
+ validator = Avalon::Validator.new("1", lambda {|x| x.respond_to?(:to_i) })
44
+ validator.valid? #=> true
45
+ validator.invalid? #=> false
46
+ validator.validate #=> true
47
+ ```
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/avalon.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/avalon/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Ryo NAKAMURA"]
6
+ gem.email = ["r7kamura@gmail.com"]
7
+ gem.description = "Provide a simple validation method and validator class for Ruby"
8
+ gem.summary = "A validator implementation for Ruby"
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "avalon"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Avalon::VERSION
17
+
18
+ gem.add_development_dependency "rspec"
19
+ end
@@ -0,0 +1,44 @@
1
+ module Avalon
2
+ class Validator
3
+ def initialize(target, *patterns, &block)
4
+ @target = target
5
+ @patterns = patterns
6
+ @patterns << block if block_given?
7
+ end
8
+
9
+ def validate
10
+ raise_error if invalid?
11
+ true
12
+ end
13
+
14
+ def valid?
15
+ @patterns.any? {|pattern| check(pattern) }
16
+ end
17
+
18
+ def invalid?
19
+ !valid?
20
+ end
21
+
22
+ private
23
+
24
+ def check(pattern)
25
+ if pattern.respond_to?(:call)
26
+ @target.instance_eval(&pattern)
27
+ else
28
+ pattern === @target
29
+ end
30
+ end
31
+
32
+ def raise_error
33
+ raise ValidationError, error_message
34
+ end
35
+
36
+ def error_message
37
+ target = @target.inspect
38
+ patterns = @patterns.map(&:inspect).join(" or ")
39
+ "#{target} must match #{patterns}"
40
+ end
41
+ end
42
+
43
+ class ValidationError < StandardError; end
44
+ end
@@ -0,0 +1,3 @@
1
+ module Avalon
2
+ VERSION = "0.0.2"
3
+ end
data/lib/avalon.rb ADDED
@@ -0,0 +1,18 @@
1
+ require "avalon/version"
2
+ require "avalon/validator"
3
+
4
+ module Avalon
5
+ extend self
6
+
7
+ def validate(*args, &block)
8
+ Validator.new(*args, &block).validate
9
+ end
10
+
11
+ def valid?(*args, &block)
12
+ Validator.new(*args, &block).valid?
13
+ end
14
+
15
+ def invalid?(*args, &block)
16
+ Validator.new(*args, &block).invalid?
17
+ end
18
+ end
@@ -0,0 +1,121 @@
1
+ require "spec_helper"
2
+
3
+ describe Avalon::Validator do
4
+ describe "#valid?" do
5
+ context "given String pattern" do
6
+ context "when matched" do
7
+ it do
8
+ Avalon::Validator.new("target", "target").should be_valid
9
+ end
10
+ end
11
+
12
+ context "not matched" do
13
+ it do
14
+ Avalon::Validator.new("target", "not matched").should_not be_valid
15
+ end
16
+ end
17
+ end
18
+
19
+ context "given Regexp pattern" do
20
+ context "when matched" do
21
+ it do
22
+ Avalon::Validator.new("target", /target/).should be_valid
23
+ end
24
+ end
25
+
26
+ context "when not matched" do
27
+ it do
28
+ Avalon::Validator.new("target", /not matched/).should_not be_valid
29
+ end
30
+ end
31
+ end
32
+
33
+ context "given Class pattern" do
34
+ context "when matched" do
35
+ it do
36
+ Avalon::Validator.new("target", String).should be_valid
37
+ end
38
+ end
39
+
40
+ context "when not matched" do
41
+ it do
42
+ Avalon::Validator.new("target", Fixnum).should_not be_valid
43
+ end
44
+ end
45
+ end
46
+
47
+ context "given Proc pattern" do
48
+ it "should evaluated in target's context" do
49
+ Avalon::Validator.new("target") { match(/target/) }.should be_valid
50
+ end
51
+
52
+ context "when matched" do
53
+ it do
54
+ Avalon::Validator.new("target", proc {|x| x =~ /target/ }).should be_valid
55
+ end
56
+ end
57
+
58
+ context "when not matched" do
59
+ it do
60
+ Avalon::Validator.new("target", proc {|x| x =~ /not match/ }).should_not be_valid
61
+ end
62
+ end
63
+ end
64
+
65
+ context "given Block pattern" do
66
+ context "when matched" do
67
+ it do
68
+ Avalon::Validator.new("target") {|x| x =~ /target/ }.should be_valid
69
+ end
70
+ end
71
+
72
+ context "when not matched" do
73
+ it do
74
+ Avalon::Validator.new("target") {|x| x =~ /not match/ }.should_not be_valid
75
+ end
76
+ end
77
+
78
+ it "should evaluated in target's context" do
79
+ Avalon::Validator.new("target") { match(/target/) }.should be_valid
80
+ end
81
+ end
82
+
83
+ context "given multiple OR-patterns" do
84
+ context "when matched" do
85
+ it do
86
+ Avalon::Validator.new("target", Fixnum, "target").should be_valid
87
+ end
88
+ end
89
+
90
+ context "when not matched" do
91
+ it do
92
+ Avalon::Validator.new("target", Fixnum, /not matched/).should_not be_valid
93
+ end
94
+ end
95
+ end
96
+ end
97
+
98
+ describe "#invalid?" do
99
+ it "should behave like a counter method of #valid?" do
100
+ Avalon::Validator.new("target", "not matched").should be_invalid
101
+ end
102
+ end
103
+
104
+ describe "#validate" do
105
+ context "in valid case" do
106
+ it do
107
+ expect do
108
+ Avalon::Validator.new("target", "target").validate
109
+ end.to_not raise_error(Avalon::ValidationError)
110
+ end
111
+ end
112
+
113
+ context "in invalid case" do
114
+ it "should raise Avalon::ValidationError with message that includes target and patterns" do
115
+ expect do
116
+ Avalon::Validator.new("target", Fixnum, /not matched/).validate
117
+ end.to raise_error(Avalon::ValidationError, '"target" must match Fixnum or /not matched/')
118
+ end
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,24 @@
1
+ require "spec_helper"
2
+
3
+ describe Avalon do
4
+ describe ".#validate" do
5
+ it "should call Validator#validate" do
6
+ Avalon::Validator.any_instance.should_receive(:validate)
7
+ Avalon.validate("target", "pattern")
8
+ end
9
+ end
10
+
11
+ describe ".#valid?" do
12
+ it "should call Validator#valid?" do
13
+ Avalon::Validator.any_instance.should_receive(:valid?)
14
+ Avalon.valid?("target", "pattern")
15
+ end
16
+ end
17
+
18
+ describe ".#valid?" do
19
+ it "should call Validator#invalid?" do
20
+ Avalon::Validator.any_instance.should_receive(:invalid?)
21
+ Avalon.invalid?("target", "pattern")
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
2
+ require "avalon"
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: avalon
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ryo NAKAMURA
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70209852418340 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70209852418340
25
+ description: Provide a simple validation method and validator class for Ruby
26
+ email:
27
+ - r7kamura@gmail.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ - avalon.gemspec
38
+ - lib/avalon.rb
39
+ - lib/avalon/validator.rb
40
+ - lib/avalon/version.rb
41
+ - spec/avalon/validator_spec.rb
42
+ - spec/avalon_spec.rb
43
+ - spec/spec_helper.rb
44
+ homepage: ''
45
+ licenses: []
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project:
64
+ rubygems_version: 1.8.15
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: A validator implementation for Ruby
68
+ test_files:
69
+ - spec/avalon/validator_spec.rb
70
+ - spec/avalon_spec.rb
71
+ - spec/spec_helper.rb
72
+ has_rdoc: