rulix 0.1.0 → 0.2.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: 4c4b442c751d4de70d8ae4dc25d2433da4820fa3
4
- data.tar.gz: d4d3c2d5928c563975905a70534e89117cb758b4
3
+ metadata.gz: da82c189efb7e63ea03bb2363ce177d6810c9e0e
4
+ data.tar.gz: 7a5135b9f16b1b80e3045c71d7314a77f2fdf07d
5
5
  SHA512:
6
- metadata.gz: 503fe431b92c2906eb9cf1dc4c0cdd4718458609c9bb7937fb9cfa7ea9919da5c07c0dae54fe1e8d5def127373fc1aba2deed4357039c41fcc6dc826a7f25064
7
- data.tar.gz: 0c0d83b8f3831d08f7d7ae78f6368f7f0d044a20b428d36805bc7fa9f1b3a67e97deed61c8b709cacbd375625629f3bce911f45c5ec4d66c2f25b69f22d16e89
6
+ metadata.gz: acd7f8cee74adde2b1ed17b9dde5a54b0d1db6c8ac91af94eeff46ab6ce5bbeca79800f35d8fa9cca358171d44edff8f4df83aeb3b635d4f9c6c591c8f3f9049
7
+ data.tar.gz: 7c060eea1f3828ee908f06019f9a0c806820b9de2e03cf47eb67745f51f332162f3ec6854734a6f697ad12380057b2dc88e631a046cad8289e6c7d65c0b8db30
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ *.gem
@@ -31,9 +31,15 @@ module Rulix
31
31
  def self.get_operation operation
32
32
  case operation
33
33
  when Symbol
34
- return @registry[operation].to_proc if @registry[operation] && @registry[operation].respond_to?(:to_proc)
35
-
36
- operation.to_proc
34
+ registered_op = @registry[operation]
35
+
36
+ if registered_op
37
+ return registered_op.to_proc if registered_op.respond_to?(:to_proc)
38
+
39
+ registered_op.new.to_proc
40
+ else
41
+ operation.to_proc
42
+ end
37
43
  when Hash
38
44
  # If you're passing a hash as a rule argument, we assume that it's been registered
39
45
  # The registered rule must be instantiatable, and we assume the args passed
@@ -47,7 +53,7 @@ module Rulix
47
53
 
48
54
  case registered_procable
49
55
  when Proc
50
- registered_procable.curry[arguments ]
56
+ registered_procable.curry[arguments]
51
57
  else
52
58
  registered_procable.new(arguments).to_proc
53
59
  end
@@ -1,5 +1,3 @@
1
- require_relative './registry'
2
-
3
1
  module Rulix
4
2
  class Validator
5
3
  include Rulix::Registry
@@ -46,15 +44,16 @@ module Rulix
46
44
 
47
45
  def self.reduce_into_hash hash, dataset, ruleset
48
46
  ruleset.reduce(hash) do |data_hash, values|
49
- key, val = values
47
+ key, rule_val = values
48
+
49
+ nested_value = value_from_dataset dataset, key
50
50
 
51
- if val.is_a? Hash
51
+ if rule_val.is_a?(Hash) && (nested_value.is_a?(Hash) || rule_val.keys.all? { |k| nested_value.respond_to?(k) })
52
52
  seed = {}
53
- nested_value = value_from_dataset dataset, key
54
53
 
55
- data_hash[key] = reduce_into_hash seed, nested_value, val
54
+ data_hash[key] = reduce_into_hash seed, nested_value, rule_val
56
55
  else
57
- data_hash[key] = value_from_dataset dataset, key
56
+ data_hash[key] = nested_value
58
57
  end
59
58
 
60
59
  data_hash
@@ -62,7 +61,12 @@ module Rulix
62
61
  end
63
62
 
64
63
  def self.value_from_dataset dataset, key
65
- if dataset.respond_to? :[]
64
+ # Not a fan of type-checking here, would rather do a :respond_to? :[]
65
+ # However, that scenario breaks when validating sensitive data
66
+ # on an ActiveRecord model (like password) or any other attribute
67
+ # that is manipulated in the biz logic layer before being set on
68
+ # the db (e.g.: Model(:ssn) -> Database(:encrypted_ssn))
69
+ if dataset.is_a? Hash
66
70
  dataset[key]
67
71
  else
68
72
  dataset.public_send(key)
@@ -0,0 +1,19 @@
1
+ module Rulix
2
+ module Validators
3
+ class AlphaNumericValidator
4
+ def self.to_proc
5
+ new.method(:call)
6
+ end
7
+
8
+ def call string
9
+ /^[a-zA-Z0-9\s?]*$/ === string || [false, error_message(string)]
10
+ end
11
+
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
@@ -16,4 +16,4 @@ module Rulix
16
16
  end
17
17
  end
18
18
 
19
- Rulix::Validator.register :alpha_only, Rulix::Validators::AlphaValidator
19
+ Rulix::Validator.register :alpha, Rulix::Validators::AlphaValidator
@@ -0,0 +1,19 @@
1
+ module Rulix
2
+ module Validators
3
+ class EmailValidator
4
+ def self.to_proc
5
+ new.method(:call)
6
+ end
7
+
8
+ def call string
9
+ /.*@.*/ === string || [false, error_message(string)]
10
+ end
11
+
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
@@ -1,17 +1,24 @@
1
1
  module Rulix
2
2
  module Validators
3
3
  class FormatValidator
4
- attr_accessor :format, :message
4
+ attr_accessor :pattern, :message
5
5
 
6
6
  def initialize options = nil
7
- options ||= {}
7
+ case options
8
+ when Regexp
9
+ self.pattern = options
10
+ when Hash
11
+ self.pattern = options[:pattern]
12
+ self.message = options[:message]
13
+ else
14
+ options ||= {}
15
+ end
8
16
 
9
- self.format = options[:format]
10
- self.message = options.fetch :message, 'does not match format'
17
+ self.message ||= 'does not match format'
11
18
  end
12
19
 
13
20
  def call string
14
- format === string || [false, message]
21
+ pattern === string || [false, message]
15
22
  end
16
23
 
17
24
  def to_proc
@@ -1,26 +1,35 @@
1
1
  module Rulix
2
2
  module Validators
3
3
  class LengthValidator
4
- attr_accessor :min, :max
4
+ attr_accessor :min, :max, :exactly
5
5
 
6
6
  def initialize options = nil
7
7
  options ||= {}
8
- min = options[:min] || options[:exactly] || 0
9
- max = options[:max] || options[:exactly] || min + 1
8
+ min = options[:min] || 0
9
+ max = options[:max] || min + 1
10
10
 
11
+ self.exactly = options[:exactly]
11
12
  self.min = min
12
13
  self.max = max
13
14
  end
14
15
 
15
16
  def call string
16
- (min..max).cover?(string.length) || [false, error_message(string)]
17
+ if exactly.nil?
18
+ (min..max).cover?(string.length) || [false, error_message(string)]
19
+ else
20
+ exactly == string.length || [false, exact_error_message]
21
+ end
22
+ end
23
+
24
+ def exact_error_message
25
+ "must be exactly #{exactly} characters long"
17
26
  end
18
27
 
19
28
  def error_message string
20
29
  if string.length < min
21
- "is too short"
30
+ "must be at least #{min} characters long"
22
31
  elsif string.length > max
23
- "is too long"
32
+ "cannot be longer than #{max} characters"
24
33
  end
25
34
  end
26
35
 
@@ -0,0 +1,22 @@
1
+ module Rulix
2
+ module Validators
3
+ class NotOneOfValidator
4
+ attr_accessor :options
5
+
6
+ def initialize options = nil
7
+ options ||= []
8
+ self.options = options
9
+ end
10
+
11
+ def call value
12
+ !options.include?(value) || [false, "cannot be one of #{options}"]
13
+ end
14
+
15
+ def to_proc
16
+ method(:call)
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ Rulix::Validator.register :not_one_of, Rulix::Validators::NotOneOfValidator
@@ -0,0 +1,26 @@
1
+ module Rulix
2
+ module Validators
3
+ class NumberValidator
4
+ def self.to_proc
5
+ new.method(:call)
6
+ end
7
+
8
+ def call value
9
+ case value
10
+ when Integer, Fixnum
11
+ true
12
+ when String
13
+ /^[\d]*$/ === value || [false, error_message(value)]
14
+ else
15
+ [false, error_message(value)]
16
+ end
17
+ end
18
+
19
+ def error_message value
20
+ "is not a number"
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ Rulix::Validator.register :number, Rulix::Validators::NumberValidator
@@ -0,0 +1,22 @@
1
+ module Rulix
2
+ module Validators
3
+ class OneOfValidator
4
+ attr_accessor :options
5
+
6
+ def initialize options = nil
7
+ options ||= []
8
+ self.options = options
9
+ end
10
+
11
+ def call value
12
+ options.include?(value) || [false, "is not one of #{options}"]
13
+ end
14
+
15
+ def to_proc
16
+ method(:call)
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ Rulix::Validator.register :one_of, Rulix::Validators::OneOfValidator
@@ -0,0 +1,15 @@
1
+ module Rulix
2
+ module Validators
3
+ class RequiredValidator
4
+ def call value
5
+ !value.nil? || [false, 'is required']
6
+ end
7
+
8
+ def to_proc
9
+ method(:call)
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+ Rulix::Validator.register :required, Rulix::Validators::RequiredValidator
data/lib/rulix/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Rulix
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/rulix.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require_relative './rulix/version'
2
+ require_relative './rulix/registry'
2
3
  require_relative './rulix/mutator'
3
4
  require_relative './rulix/validator'
4
5
  require_relative './rulix/core_ext/hash'
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.1.0
4
+ version: 0.2.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-17 00:00:00.000000000 Z
11
+ date: 2016-06-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -72,9 +72,15 @@ files:
72
72
  - lib/rulix/mutator.rb
73
73
  - lib/rulix/registry.rb
74
74
  - lib/rulix/validator.rb
75
+ - lib/rulix/validators/alpha_numeric_validator.rb
75
76
  - lib/rulix/validators/alpha_validator.rb
77
+ - lib/rulix/validators/email_validator.rb
76
78
  - lib/rulix/validators/format_validator.rb
77
79
  - lib/rulix/validators/length_validator.rb
80
+ - lib/rulix/validators/not_one_of_validator.rb
81
+ - lib/rulix/validators/number_validator.rb
82
+ - lib/rulix/validators/one_of_validator.rb
83
+ - lib/rulix/validators/required_validator.rb
78
84
  - lib/rulix/version.rb
79
85
  - rulix.gemspec
80
86
  homepage: https://github.com/blarshk/rulix