kharon 0.3.0 → 0.3.1

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.
@@ -0,0 +1,32 @@
1
+ require "./lib/validator.rb"
2
+
3
+ module Kharon
4
+ # This validator is really cooler than the other one. It will help you validate datas without raising exception, if you don't want to.
5
+ # @author Vincent Courtois <vincent.courtois@mycar-innovations.com>
6
+ class CoolValidator < Kharon::Validator
7
+
8
+ # @!attribute [r] errors
9
+ # @return an array of strings, each string representing an error occured while validating the hash.
10
+ attr_reader :errors
11
+
12
+ # Constructor of the classe, receiving the datas to validate and filter.
13
+ # @param [Hash] datas the datas to validate in the validator.
14
+ # @example create a new instance of validator.
15
+ # @validator = Kharon::CoolValidator.new({key: "value"})
16
+ def initialize(datas)
17
+ super(datas)
18
+ @errors = Array.new
19
+ end
20
+
21
+ # Fancy method to see if the validator have seen an error in the given hash.
22
+ def has_errors?
23
+ return (@errors.empty? ? false : true)
24
+ end
25
+
26
+ protected
27
+
28
+ def raise_error(message)
29
+ errors.push(message)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,34 @@
1
+ require "./lib/validator.rb"
2
+ require "./lib/cool_validator.rb"
3
+
4
+ module Kharon
5
+ module Factory
6
+
7
+ # @!attribute [r|w] use_exceptions
8
+ # @return TRUE if Kharon currently use exceptions, FALSE if not.
9
+ @@use_exceptions = true
10
+
11
+ # Acts as a creation method for validators, considering the options you transmitted.
12
+ # @param [Hash] datas the datas to validate in the validator.
13
+ def self.validator(datas)
14
+ @@use_exceptions ? Kharon::Validator.new(datas) : Kharon::CoolValidator.new(datas)
15
+ end
16
+
17
+ # Allows you to pass a whole block to configure the Kharon module.
18
+ # @param [Hash] block a block of configuration instructions to pass to the module. See documentation for further informations.
19
+ def self.configure(&block)
20
+ self.instance_eval(&block)
21
+ end
22
+
23
+ # Sets the use of exceptions in the whole process.
24
+ # @param [Boolean] status TRUE if you want to use the eceptions, FALSE if not.
25
+ def self.use_exceptions(status)
26
+ @@use_exceptions = status
27
+ end
28
+
29
+ # Fancy method to know if the module uses the exceptions or not.
30
+ def self.uses_exceptions?
31
+ return @@use_exceptions
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe "CoolValidator" do
4
+ context "handling errors" do
5
+
6
+ before do
7
+ @validator = Kharon::CoolValidator.new({key: "value"})
8
+ end
9
+
10
+ it "doesn't raise an exception when an error occurs" do
11
+ expect(->{@validator.any(:another, required: true)}).to_not raise_error
12
+ end
13
+
14
+ it "has an error when an error has occured" do
15
+ @validator.any(:another, required: true)
16
+ expect(@validator.has_errors?).to be(true)
17
+ end
18
+
19
+ it "doesn't have an error when none has occured" do
20
+ @validator.any(:key, required: true)
21
+ expect(@validator.has_errors?).to be(false)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Kharon::Factory" do
4
+
5
+ context "validator" do
6
+ it "produces a Kharon::Validator when exceptions are wanted" do
7
+ Kharon::Factory.use_exceptions(true)
8
+ expect(Kharon::Factory.validator({key: "value"})).to be_a(Kharon::Validator)
9
+ end
10
+
11
+ it "produces a Kharon::CoolValidator when exceptions are not wanted" do
12
+ Kharon::Factory.use_exceptions(false)
13
+ expect(Kharon::Factory.validator({key: "value"})).to be_a(Kharon::CoolValidator)
14
+ end
15
+ end
16
+
17
+ context "configure" do
18
+ it "correctly configures the module to use exceptions" do
19
+ Kharon::Factory.configure do |configuration|
20
+ configuration.use_exceptions(true)
21
+ end
22
+ expect(Kharon::Factory.uses_exceptions?).to be(true)
23
+ end
24
+
25
+ it "correctly configures the module to not use exceptions" do
26
+ Kharon::Factory.configure do |configuration|
27
+ configuration.use_exceptions(false)
28
+ end
29
+ expect(Kharon::Factory.uses_exceptions?).to be(false)
30
+ end
31
+ end
32
+
33
+ end
@@ -1,6 +1,4 @@
1
- require 'rack/test'
2
- require 'moped'
3
- require './lib/validator.rb'
1
+ require 'spec_helper'
4
2
 
5
3
  shared_examples "options" do |process|
6
4
  context ":rename" do
@@ -0,0 +1,9 @@
1
+ # Sets the environment variable to test before loading all the files.
2
+ ENV['RACK_ENV'] = 'test'
3
+
4
+ require 'rack/test'
5
+ require 'bson'
6
+
7
+ require './lib/cool_validator.rb'
8
+ require './lib/validator_factory.rb'
9
+ require './lib/validator.rb'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kharon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,23 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
  date: 2014-04-02 00:00:00.000000000 Z
13
- dependencies: []
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bson
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - '='
20
+ - !ruby/object:Gem::Version
21
+ version: 2.2.2
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - '='
28
+ - !ruby/object:Gem::Version
29
+ version: 2.2.2
14
30
  description: Kharon is a ruby hash validator that helps you fix the structure of a
15
31
  hash (type of the keys, dependencies, ...).
16
32
  email: vincent.courtois@mycar-innovations.com
@@ -20,7 +36,12 @@ extra_rdoc_files: []
20
36
  files:
21
37
  - lib/validator.rb
22
38
  - lib/validate.rb
39
+ - lib/validator_factory.rb
40
+ - lib/cool_validator.rb
41
+ - spec/spec_helper.rb
23
42
  - spec/lib/validator_spec.rb
43
+ - spec/lib/cool_validator_spec.rb
44
+ - spec/lib/validator_factory_spec.rb
24
45
  homepage: https://rubygems.org/gems/kharon
25
46
  licenses:
26
47
  - Apache License 2
@@ -47,4 +68,7 @@ signing_key:
47
68
  specification_version: 3
48
69
  summary: Ruby Hash validator
49
70
  test_files:
71
+ - spec/spec_helper.rb
50
72
  - spec/lib/validator_spec.rb
73
+ - spec/lib/cool_validator_spec.rb
74
+ - spec/lib/validator_factory_spec.rb