valcro 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in valcro.gemspec
4
+ gemspec
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,42 @@
1
+ require 'valcro/error'
2
+ require 'valcro/error_list'
3
+ require 'valcro/runner'
4
+
5
+ module Valcro
6
+ def self.included(base)
7
+ base.extend ClassMethods
8
+ end
9
+
10
+ def errors
11
+ @error_list ||= Valcro::ErrorList.new
12
+ end
13
+
14
+ def valid?
15
+ !errors.any?
16
+ end
17
+
18
+ def error_messages
19
+ errors.to_s
20
+ end
21
+
22
+ def validate
23
+ self.class.validators.each do |validator_class|
24
+ validation_runner.add_validator validator_class.new(self)
25
+ end
26
+ validation_runner.validate
27
+ end
28
+
29
+ def validation_runner
30
+ @validation_runner ||= Valcro::Runner.new(errors)
31
+ end
32
+
33
+ module ClassMethods
34
+ def validates_with(validator_class)
35
+ validators << validator_class
36
+ end
37
+
38
+ def validators
39
+ @validators ||= []
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,18 @@
1
+ module Valcro
2
+ class Error
3
+ attr_accessor :property, :message
4
+
5
+ def initialize(property, message)
6
+ @property = property
7
+ @message = message
8
+ end
9
+
10
+ def to_s
11
+ if @property == :base
12
+ message
13
+ else
14
+ "#{property.to_s} #{message}"
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,32 @@
1
+ module Valcro
2
+ class ErrorList
3
+ attr_accessor :errors
4
+ def initialize
5
+ @errors = []
6
+ end
7
+
8
+ def <<(error)
9
+ @errors << error
10
+ end
11
+
12
+ def add(prop, message)
13
+ @errors << Valcro::Error.new(prop, message)
14
+ end
15
+
16
+ def [](prop)
17
+ @errors.select { |error| error.property == prop }.map(&:message) || []
18
+ end
19
+
20
+ def full_messages
21
+ @errors.map(&:to_s)
22
+ end
23
+
24
+ def to_s
25
+ full_messages.join(' ')
26
+ end
27
+
28
+ def any?
29
+ @errors.any?
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,19 @@
1
+ module Valcro
2
+ class Runner
3
+ attr_accessor :validators, :error_list
4
+ def initialize(error_list = ErrorList.new)
5
+ @validators = []
6
+ @error_list = error_list
7
+ end
8
+
9
+ def add_validator(validator)
10
+ @validators << validator
11
+ end
12
+
13
+ def validate
14
+ @validators.each do |validator|
15
+ validator.call error_list
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module Valcro
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+
3
+ describe Valcro::ErrorList, 'adding errors' do
4
+ it 'supports adding via <<' do
5
+ list = Valcro::ErrorList.new
6
+ list << Valcro::Error.new(:prop, 'message')
7
+
8
+ list.should have(1).errors
9
+ end
10
+ end
11
+
12
+ describe Valcro::ErrorList, '#add' do
13
+ it 'finds and agregates error messages' do
14
+ list = Valcro::ErrorList.new
15
+
16
+ list[:one].should == []
17
+
18
+ list.add(:one, 'message one')
19
+ list.add(:two, 'message two')
20
+ list.add(:one, 'another message one')
21
+
22
+ list[:one].should == ['message one', 'another message one']
23
+ end
24
+ end
25
+
26
+ describe Valcro::ErrorList, '#full_messages' do
27
+ it 'gives a collection of messages' do
28
+ list = Valcro::ErrorList.new
29
+
30
+ list.add :prop, 'one'
31
+ list.add :prop, 'two'
32
+ list.add :prop,'three'
33
+
34
+ list.full_messages.should == ["prop one", "prop two", "prop three"]
35
+ end
36
+ end
37
+
38
+ describe Valcro::ErrorList, '#to_s' do
39
+ it 'gives messages as one string' do
40
+ list = Valcro::ErrorList.new
41
+
42
+ list.add :prop, 'one'
43
+ list.add :prop, 'two'
44
+ list.add :prop, 'three'
45
+
46
+ list.to_s.should == "prop one prop two prop three"
47
+ end
48
+ end
49
+
50
+ describe Valcro::ErrorList, '#any?' do
51
+ it 'is true when there are errors 'do
52
+ list = Valcro::ErrorList.new
53
+
54
+ list.any?.should be_false
55
+ list.add :prop, 'some error'
56
+
57
+ list.any?.should be_true
58
+ end
59
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe Valcro::Error do
4
+ it 'has a property and a message' do
5
+ error = create_error(:prop, 'message')
6
+
7
+ error.property.should == :prop
8
+ error.message.should == 'message'
9
+ end
10
+
11
+ it 'can coerce to a string' do
12
+ error = create_error
13
+ error.to_s.should == 'prop message'
14
+ end
15
+
16
+ it 'does not include property if it is base' do
17
+ error = create_error(:base)
18
+ error.to_s.should == 'message'
19
+ end
20
+
21
+ def create_error(prop = :prop, message = 'message')
22
+ Valcro::Error.new(prop, message)
23
+ end
24
+ end
25
+
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe Valcro::Runner do
4
+ it 'can store validators' do
5
+ runner = Valcro::Runner.new
6
+ runner.add_validator :some_validator
7
+ runner.validators.should have(1).proc
8
+ end
9
+
10
+ it 'runs validators' do
11
+ error_list = Valcro::ErrorList.new
12
+ runner = Valcro::Runner.new(error_list)
13
+ runner.add_validator lambda { |errors| errors.add :foo, 'Huge mistake' }
14
+
15
+ runner.validate
16
+
17
+ error_list.any?.should be_true
18
+ end
19
+ end
@@ -0,0 +1,2 @@
1
+ require 'rspec'
2
+ require 'valcro'
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+
3
+ describe Valcro do
4
+ class TestClass
5
+ include Valcro
6
+ end
7
+
8
+ subject { TestClass.new }
9
+ it { should be_valid }
10
+ end
11
+
12
+ describe Valcro, 'adding some errors' do
13
+ class TestClass
14
+ include Valcro
15
+ end
16
+
17
+ it 'gives access to the error list' do
18
+ test_instance = TestClass.new
19
+ test_instance.should be_valid
20
+
21
+ test_instance.errors.add(:foo, 'too foo for my taste')
22
+
23
+ test_instance.errors[:foo].should have(1).error
24
+
25
+ test_instance.should_not be_valid
26
+ end
27
+ end
28
+
29
+ describe Valcro, 'validators' do
30
+ class StatusFail
31
+ def initialize(context)
32
+ @context = context
33
+ end
34
+ def call(errors)
35
+ errors.add(:status, 'big mistake') if @context.status == 'fail'
36
+ end
37
+ end
38
+
39
+ class TestClass
40
+ include Valcro
41
+ def status
42
+ "fail"
43
+ end
44
+ validates_with StatusFail
45
+ end
46
+
47
+ it 'can be added validators' do
48
+ test_instance = TestClass.new
49
+
50
+ test_instance.validate
51
+ test_instance.should_not be_valid
52
+ end
53
+ end
54
+
55
+ describe Valcro, '#error_messages' do
56
+ class TestClass
57
+ include Valcro
58
+ end
59
+
60
+ it 'delegates to errors' do
61
+ test_instance = TestClass.new
62
+ test_instance.stub!(errors: double(to_s: 'some errors'))
63
+
64
+ test_instance.error_messages.should == 'some errors'
65
+ end
66
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "valcro/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "valcro"
7
+ s.version = Valcro::VERSION
8
+ s.authors = ["Harold Giménez"]
9
+ s.email = ["harold.gimenez@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Validation so simple you probably don't need this}
12
+ s.description = %q{Validation so simple you probably don't need this}
13
+
14
+ s.rubyforge_project = "valcro"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency "rspec"
22
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: valcro
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Harold Giménez
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-08 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70333795201320 !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: *70333795201320
25
+ description: Validation so simple you probably don't need this
26
+ email:
27
+ - harold.gimenez@gmail.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - Rakefile
35
+ - lib/valcro.rb
36
+ - lib/valcro/error.rb
37
+ - lib/valcro/error_list.rb
38
+ - lib/valcro/runner.rb
39
+ - lib/valcro/version.rb
40
+ - spec/error_list_spec.rb
41
+ - spec/error_spec.rb
42
+ - spec/runner_spec.rb
43
+ - spec/spec_helper.rb
44
+ - spec/valcro_spec.rb
45
+ - valcro.gemspec
46
+ homepage: ''
47
+ licenses: []
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project: valcro
66
+ rubygems_version: 1.8.10
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Validation so simple you probably don't need this
70
+ test_files:
71
+ - spec/error_list_spec.rb
72
+ - spec/error_spec.rb
73
+ - spec/runner_spec.rb
74
+ - spec/spec_helper.rb
75
+ - spec/valcro_spec.rb