rulix 0.2.0 → 0.3.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: da82c189efb7e63ea03bb2363ce177d6810c9e0e
4
- data.tar.gz: 7a5135b9f16b1b80e3045c71d7314a77f2fdf07d
3
+ metadata.gz: d3a105f632d0c729e93bc8ed9cb1389f8a3386f5
4
+ data.tar.gz: 1b83a610f1aed3fe46d65ff75a459e155fb3827b
5
5
  SHA512:
6
- metadata.gz: acd7f8cee74adde2b1ed17b9dde5a54b0d1db6c8ac91af94eeff46ab6ce5bbeca79800f35d8fa9cca358171d44edff8f4df83aeb3b635d4f9c6c591c8f3f9049
7
- data.tar.gz: 7c060eea1f3828ee908f06019f9a0c806820b9de2e03cf47eb67745f51f332162f3ec6854734a6f697ad12380057b2dc88e631a046cad8289e6c7d65c0b8db30
6
+ metadata.gz: b9e44f9296c40ccd8cfbcc5e822f30ba90b378a8e603791f3e786caedc3e0e2299c45b172913374fcffde163f7723cbc4b7694194279304d04d9d9a583c3e5b9
7
+ data.tar.gz: 3b7f4e2de34f8a6a8008f12cd9ede722a79f4f08af9b516237ca373eab509cbd25007ff121f6f59191bf95acfb7df4f0a8b758ca669ce6b944befdd0485f9ffb
@@ -0,0 +1,35 @@
1
+ module Rulix
2
+ class ActiveRecordValidator
3
+ attr_accessor :ruleset
4
+
5
+ def initialize *args
6
+ ruleset = args.first[:ruleset]
7
+
8
+ self.ruleset = ruleset
9
+ end
10
+
11
+ def validate record
12
+ unless Rulix::Validator.valid? record, ruleset
13
+ errors = Rulix::Validator.errors record, ruleset
14
+
15
+ reduce_errors_into record, errors
16
+ end
17
+ end
18
+
19
+ def reduce_errors_into record, error_hash
20
+ error_hash.reduce(record) do |subject, error|
21
+ key, val = error
22
+
23
+ if val.is_a? Hash
24
+ reduce_errors_into record.send(key), val
25
+ else
26
+ val.flatten.map do |err|
27
+ subject.errors.add key, err
28
+ end
29
+ end
30
+
31
+ subject
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,23 @@
1
+ module Rulix
2
+ module Mutators
3
+ class Replace
4
+ attr_accessor :args
5
+
6
+ def initialize args
7
+ self.args = args
8
+ end
9
+
10
+ def to_proc
11
+ method(:call)
12
+ end
13
+
14
+ def call string
15
+ raise ArgumentError, "argument is not a string" unless string.is_a? String
16
+
17
+ string.gsub *args
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ Rulix::Mutator.register :replace, Rulix::Mutators::Replace
@@ -0,0 +1,17 @@
1
+ module Rulix
2
+ module Mutators
3
+ class SqueezeSpaces
4
+ def self.to_proc
5
+ new.method(:call)
6
+ end
7
+
8
+ def call string
9
+ raise ArgumentError, "argument is not a string" unless string.is_a? String
10
+
11
+ string.squeeze ' '
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ Rulix::Mutator.register :squeeze_spaces, Rulix::Mutators::SqueezeSpaces
@@ -0,0 +1,29 @@
1
+ module Rulix
2
+ module Mutators
3
+ class Strip
4
+ attr_accessor :pattern
5
+
6
+ def initialize pattern = nil
7
+ self.pattern = pattern
8
+ end
9
+
10
+ def to_proc
11
+ method(:call)
12
+ end
13
+
14
+ def call string
15
+ raise ArgumentError, "argument is not a string" unless string.is_a? String
16
+
17
+ if pattern
18
+ string.gsub pattern, ''
19
+ else
20
+ # Just in case someone actually meant the string method :strip
21
+ # If they didn't provide any other arguments, we can assume this
22
+ string.strip
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ Rulix::Mutator.register :strip, Rulix::Mutators::Strip
@@ -0,0 +1,23 @@
1
+ module Rulix
2
+ module Mutators
3
+ class Truncate
4
+ attr_accessor :length
5
+
6
+ def initialize length
7
+ self.length = length
8
+ end
9
+
10
+ def to_proc
11
+ method(:call)
12
+ end
13
+
14
+ def call string
15
+ raise ArgumentError, "argument is not a string" unless string.is_a? String
16
+
17
+ string.slice(0, length)
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ Rulix::Mutator.register :truncate, Rulix::Mutators::Truncate
@@ -8,7 +8,7 @@ module Rulix
8
8
  return register_block symbol, &block if block_given?
9
9
 
10
10
  if !procable.respond_to?(:to_proc)
11
- unless (procable.respond_to?(:new) && procable.new.respond_to?(:to_proc))
11
+ unless (procable.respond_to?(:new) && procable.instance_methods.include?(:to_proc))
12
12
  raise ArgumentError, "You attempted to register :#{symbol}, but the argument you passed can't be coerced into a proc!"
13
13
  end
14
14
  end
@@ -1,19 +1,8 @@
1
- module Rulix
2
- module Validators
3
- class AlphaNumericValidator
4
- def self.to_proc
5
- new.method(:call)
6
- end
1
+ require_relative './format_validator'
7
2
 
8
- def call string
9
- /^[a-zA-Z0-9\s?]*$/ === string || [false, error_message(string)]
10
- end
3
+ validator = Rulix::Validators::FormatValidator.new(
4
+ pattern: /^[a-zA-Z0-9\s?]*$/,
5
+ message: "contains non-alpha-numeric characters"
6
+ )
11
7
 
12
- def error_message string
13
- "contains non-alpha-numeric characters"
14
- end
15
- end
16
- end
17
- end
18
-
19
- Rulix::Validator.register :alphanum, Rulix::Validators::AlphaNumericValidator
8
+ Rulix::Validator.register :alphanum, validator
@@ -1,19 +1,8 @@
1
- module Rulix
2
- module Validators
3
- class AlphaValidator
4
- def self.to_proc
5
- new.method(:call)
6
- end
1
+ require_relative './format_validator'
7
2
 
8
- def call string
9
- /^[a-zA-Z\s?]*$/ === string || [false, error_message(string)]
10
- end
3
+ validator = Rulix::Validators::FormatValidator.new(
4
+ pattern: /^[a-zA-Z\s?]*$/,
5
+ message: "contains non-alpha characters"
6
+ )
11
7
 
12
- def error_message string
13
- "contains non-alpha characters"
14
- end
15
- end
16
- end
17
- end
18
-
19
- Rulix::Validator.register :alpha, Rulix::Validators::AlphaValidator
8
+ Rulix::Validator.register :alpha, validator
@@ -1,19 +1,8 @@
1
- module Rulix
2
- module Validators
3
- class EmailValidator
4
- def self.to_proc
5
- new.method(:call)
6
- end
1
+ require_relative './format_validator'
7
2
 
8
- def call string
9
- /.*@.*/ === string || [false, error_message(string)]
10
- end
3
+ validator = Rulix::Validators::FormatValidator.new(
4
+ pattern: /.*@.*/,
5
+ message: 'is not an email address'
6
+ )
11
7
 
12
- def error_message string
13
- "is not an email address"
14
- end
15
- end
16
- end
17
- end
18
-
19
- Rulix::Validator.register :email, Rulix::Validators::EmailValidator
8
+ Rulix::Validator.register :email, validator
@@ -18,7 +18,7 @@ module Rulix
18
18
  end
19
19
 
20
20
  def call string
21
- pattern === string || [false, message]
21
+ (string && pattern === string) || [false, message]
22
22
  end
23
23
 
24
24
  def to_proc
@@ -14,6 +14,8 @@ module Rulix
14
14
  end
15
15
 
16
16
  def call string
17
+ return [false, "can't be nil"] unless string
18
+
17
19
  if exactly.nil?
18
20
  (min..max).cover?(string.length) || [false, error_message(string)]
19
21
  else
@@ -9,7 +9,7 @@ module Rulix
9
9
  end
10
10
 
11
11
  def call value
12
- !options.include?(value) || [false, "cannot be one of #{options}"]
12
+ (value && !options.include?(value)) || [false, "cannot be one of #{options}"]
13
13
  end
14
14
 
15
15
  def to_proc
@@ -6,6 +6,8 @@ module Rulix
6
6
  end
7
7
 
8
8
  def call value
9
+ return [false, error_message] unless value
10
+
9
11
  case value
10
12
  when Integer, Fixnum
11
13
  true
@@ -16,7 +18,7 @@ module Rulix
16
18
  end
17
19
  end
18
20
 
19
- def error_message value
21
+ def error_message value = nil
20
22
  "is not a number"
21
23
  end
22
24
  end
@@ -9,7 +9,7 @@ module Rulix
9
9
  end
10
10
 
11
11
  def call value
12
- options.include?(value) || [false, "is not one of #{options}"]
12
+ (value && options.include?(value)) || [false, "is not one of #{options}"]
13
13
  end
14
14
 
15
15
  def to_proc
@@ -2,7 +2,7 @@ module Rulix
2
2
  module Validators
3
3
  class RequiredValidator
4
4
  def call value
5
- !value.nil? || [false, 'is required']
5
+ (value && !value.nil?) || [false, 'is required']
6
6
  end
7
7
 
8
8
  def to_proc
data/lib/rulix/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Rulix
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/rulix.rb CHANGED
@@ -2,8 +2,10 @@ require_relative './rulix/version'
2
2
  require_relative './rulix/registry'
3
3
  require_relative './rulix/mutator'
4
4
  require_relative './rulix/validator'
5
+ require_relative './rulix/active_record_validator'
5
6
  require_relative './rulix/core_ext/hash'
6
7
 
8
+ Dir[File.dirname(__FILE__) + '/rulix/mutators/*.rb'].each { |f| require f }
7
9
  Dir[File.dirname(__FILE__) + '/rulix/validators/*.rb'].each { |f| require f }
8
10
 
9
11
  module Rulix; end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rulix
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mitch Monsen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-22 00:00:00.000000000 Z
11
+ date: 2016-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -68,8 +68,13 @@ files:
68
68
  - bin/console
69
69
  - bin/setup
70
70
  - lib/rulix.rb
71
+ - lib/rulix/active_record_validator.rb
71
72
  - lib/rulix/core_ext/hash.rb
72
73
  - lib/rulix/mutator.rb
74
+ - lib/rulix/mutators/replace.rb
75
+ - lib/rulix/mutators/squeeze_spaces.rb
76
+ - lib/rulix/mutators/strip.rb
77
+ - lib/rulix/mutators/truncate.rb
73
78
  - lib/rulix/registry.rb
74
79
  - lib/rulix/validator.rb
75
80
  - lib/rulix/validators/alpha_numeric_validator.rb