simple_validate 0.1.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d1b5d4bc074cc25068e6f100d2cd522743f06285
4
- data.tar.gz: 5751a6868737977c6a4c53a494a3a0e22e38e7d9
3
+ metadata.gz: 3f47a10de1d9a4052b119189c72c76b87dadbc58
4
+ data.tar.gz: be46f20b574db68989edf69b23c6d73a599be285
5
5
  SHA512:
6
- metadata.gz: ed48e5e9575c3c229a3fbc4af5c162b0f4d7ae7e21d2aaa3fe506d1e837f3f0085ddea8298592e55e00f9cab840372d0715930e84fc5fa7f4ae7b9a41fd10466
7
- data.tar.gz: 42486260a3ed6d152e56c63cfd9f50808377182756c86f040acdc6d33b8556f04cd4c2cc713084234c1fd1d0bf9f83b34ede218dd6cbd22e1e4d833fb5dcc1bd
6
+ metadata.gz: b1e5761c25c9f5e1f4de99243f8e3c512891546de63a3bb42c89ed7b9a00dd7fa735ef275f0ecd605f73ddc80ddd17f383196ec3f5b7be185174ca1dd60a5ee5
7
+ data.tar.gz: 4cf5a016ec9b15c684814c13cf118eb87ae1cde303e638bb9632a6fcff7f34669d884191f8cbbf8a949d4bd116c678efd1d296dff821d1a24857a85d72408854
data/README.md CHANGED
@@ -20,6 +20,8 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
+ ### Example
24
+
23
25
  ```ruby
24
26
  require 'simple_validate'
25
27
 
@@ -42,6 +44,36 @@ end
42
44
  :name=>["can't be empty"]}>
43
45
  ```
44
46
 
47
+ ### Presence
48
+
49
+ * It is possible to pass a different message to any validation.
50
+
51
+ ```ruby
52
+ validates_presence_of :attribute, message: 'NOT HERE'
53
+ ```
54
+
55
+ ### Numericality
56
+
57
+ ```ruby
58
+ validates_numericality_of :attribute
59
+ ```
60
+
61
+ ### Format
62
+
63
+ ```ruby
64
+ validates_format_of :attribute, with: /.*/
65
+ ```
66
+
67
+ ### Length
68
+
69
+ * Possible length options include: `maximum`, `minimum`, `in`, `is`.
70
+
71
+ * `maximum`, `minimum` and `is` take a single integer and `in` takes a range.
72
+
73
+ ```ruby
74
+ validates_length_of :attribute, in: 6..9
75
+ ```
76
+
45
77
  ## Development
46
78
 
47
79
  `$ rake` to run the specs
@@ -0,0 +1,11 @@
1
+ module SimpleValidate
2
+ class ValidatesBase
3
+ attr_reader :message
4
+ attr_accessor :attribute
5
+
6
+ def initialize(attribute, message)
7
+ @message = message
8
+ @attribute = attribute
9
+ end
10
+ end
11
+ end
@@ -1,12 +1,9 @@
1
1
  module SimpleValidate
2
- class ValidatesFormatOf
3
- attr_reader :message
4
- attr_accessor :attribute
2
+ class ValidatesFormatOf < ValidatesBase
5
3
 
6
4
  def initialize(attribute, options)
7
- @regex = options[:with]
8
- @message = options[:message] || 'is incorrect format'
9
- @attribute = attribute
5
+ @regex = options[:with]
6
+ super(attribute, options[:message] || "is incorrect format")
10
7
  end
11
8
 
12
9
  def valid?(instance)
@@ -0,0 +1,58 @@
1
+ module SimpleValidate
2
+ class ValidatesLengthOf
3
+ attr_reader :attribute
4
+ class InvalidLengthOption < ArgumentError; end
5
+
6
+ VALID_LENGTH_OPTIONS = %i(maximum minimum in is)
7
+
8
+ def initialize(attribute, options)
9
+ @message = options.delete(:message)
10
+ @attribute = attribute
11
+ @options = options
12
+ check_options(@options)
13
+ @length_validator = @options.select { |k, _| VALID_LENGTH_OPTIONS.include?(k) }
14
+ end
15
+
16
+ def message
17
+ @message ||= begin
18
+ case @length_validator.keys.first
19
+ when :minimum
20
+ 'is too short'
21
+ when :maximum
22
+ 'is too long'
23
+ else
24
+ 'is not the correct length'
25
+ end
26
+ end
27
+ end
28
+
29
+ def validator
30
+ @options.entries.first
31
+ end
32
+
33
+ def check_options(options)
34
+ if options.keys.size > 1 || !VALID_LENGTH_OPTIONS.include?(options.keys.first)
35
+ raise InvalidLengthOption, "Invalid length option given #{@options.keys}"
36
+ end
37
+ end
38
+
39
+ def valid_length?(actual_length)
40
+ validator_key, valid_length = @length_validator.entries.first
41
+ case validator_key
42
+ when :minimum
43
+ actual_length >= valid_length ? true : false
44
+ when :maximum
45
+ actual_length <= valid_length ? true : false
46
+ when :in
47
+ valid_length.member?(actual_length) ? true : false
48
+ when :is
49
+ actual_length == valid_length ? true : false
50
+ end
51
+ end
52
+
53
+ def valid?(instance)
54
+ actual_length = instance.send(attribute).length
55
+ valid_length?(actual_length)
56
+ end
57
+ end
58
+ end
@@ -1,11 +1,8 @@
1
1
  module SimpleValidate
2
- class ValidatesNumericalityOf
3
- attr_reader :message
4
- attr_accessor :attribute
2
+ class ValidatesNumericalityOf < ValidatesBase
5
3
 
6
4
  def initialize(attribute, options)
7
- @message = options[:message] || 'must be a number'
8
- @attribute = attribute
5
+ super(attribute, options[:message] || "must be a number")
9
6
  end
10
7
 
11
8
  def valid?(instance)
@@ -1,11 +1,8 @@
1
1
  module SimpleValidate
2
- class ValidatesPresenceOf
3
- attr_reader :message
4
- attr_accessor :attribute
2
+ class ValidatesPresenceOf < ValidatesBase
5
3
 
6
4
  def initialize(attribute, options)
7
- @message = options[:message] || "can't be empty"
8
- @attribute = attribute
5
+ super(attribute, options[:message] || "can't be empty")
9
6
  end
10
7
 
11
8
  def valid?(instance)
@@ -1,3 +1,3 @@
1
1
  module SimpleValidate
2
- VERSION = "0.1.0"
2
+ VERSION = '1.0.0'
3
3
  end
@@ -1,7 +1,9 @@
1
1
  require 'simple_validate/version'
2
+ require 'simple_validate/validates_base'
2
3
  require 'simple_validate/validates_presence_of'
3
4
  require 'simple_validate/validates_format_of'
4
5
  require 'simple_validate/validates_numericality_of'
6
+ require 'simple_validate/validates_length_of'
5
7
  require 'simple_validate/errors'
6
8
  require 'active_support/all'
7
9
 
@@ -24,7 +26,12 @@ module SimpleValidate
24
26
 
25
27
  module ClassMethods
26
28
  def method_missing(method, *args, &block)
27
- if "#{method}" =~ /(validates_(format_of|presence_of|numericality_of))/
29
+ if "#{method}" =~ /(validates_
30
+ (format|
31
+ presence|
32
+ numericality|
33
+ length)_of)
34
+ /x
28
35
  add_validations(args, const_get($1.classify))
29
36
  else
30
37
  super
@@ -32,7 +39,12 @@ module SimpleValidate
32
39
  end
33
40
 
34
41
  def respond_to_missing?(method, include_private = false)
35
- "#{method}" =~ /validates_(format_of|presence_of|numericality_of)/ || super
42
+ "#{method}" =~ /(validates_
43
+ (format|
44
+ presence|
45
+ numericality|
46
+ length)_of)
47
+ /x || super
36
48
  end
37
49
 
38
50
  def add_validations(args, klass)
@@ -4,22 +4,22 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'simple_validate/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "simple_validate"
7
+ spec.name = 'simple_validate'
8
8
  spec.version = SimpleValidate::VERSION
9
- spec.authors = ["Nick Palaniuk"]
10
- spec.email = ["npalaniuk@gmail.com"]
9
+ spec.authors = ['Nick Palaniuk']
10
+ spec.email = ['npalaniuk@gmail.com']
11
11
 
12
12
  spec.summary = %q(Validations for any plain old ruby object)
13
13
  spec.description = %q(Validations for any ruby object)
14
- spec.homepage = "https://github.com/nikkypx/simple_validate"
15
- spec.license = "MIT"
14
+ spec.homepage = 'https://github.com/nikkypx/simple_validate'
15
+ spec.license = 'MIT'
16
16
 
17
17
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
- spec.bindir = "exe"
18
+ spec.bindir = 'exe'
19
19
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
- spec.require_paths = ["lib"]
20
+ spec.require_paths = ['lib']
21
21
 
22
- spec.add_development_dependency "bundler", "~> 1.11"
23
- spec.add_development_dependency "rake", "~> 10.0"
24
- spec.add_development_dependency "rspec", "~> 3.0"
22
+ spec.add_development_dependency 'bundler', '~> 1.11'
23
+ spec.add_development_dependency 'rake', '~> 10.0'
24
+ spec.add_development_dependency 'rspec', '~> 3.0'
25
25
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_validate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Palaniuk
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-05-01 00:00:00.000000000 Z
11
+ date: 2016-05-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -68,7 +68,9 @@ files:
68
68
  - Rakefile
69
69
  - lib/simple_validate.rb
70
70
  - lib/simple_validate/errors.rb
71
+ - lib/simple_validate/validates_base.rb
71
72
  - lib/simple_validate/validates_format_of.rb
73
+ - lib/simple_validate/validates_length_of.rb
72
74
  - lib/simple_validate/validates_numericality_of.rb
73
75
  - lib/simple_validate/validates_presence_of.rb
74
76
  - lib/simple_validate/version.rb