composed_validations 0.0.3 → 0.0.4

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: bab2327be4ff6b26f3e3551f0169068b367cf705
4
- data.tar.gz: 16fca3cd1bfc62f58cd5d6cd3ab7a89fbf23d1ad
3
+ metadata.gz: 8d89729666fefa1d939fab5404db545fb38d54c4
4
+ data.tar.gz: 3840e021b0bca686a7f60671d4e011a6cc945992
5
5
  SHA512:
6
- metadata.gz: 27107fce38883fd31397d2cd05e121520152a8ab8ed48c5906c7a102a7dfb4e0eda3e8129f30ec7823b122cdb9dc9e9fde4c61a5730f563e10c0fbdefa7a1713
7
- data.tar.gz: 4bd68f57c44a602aa3806fb795aae0dcea9e77d572e873bde44dafb3ed2b86a0fe2316e7522dc429d53f8e889b370bb95ef88170360ff0a3b1507c5a0bffc273
6
+ metadata.gz: 20aab6508544be7422acec7289458517b69cdccb15327f1f19b6bbe7f4d9ff88c2eccd9549871fabb43f0eeec7f4a6ecd1c244b14b8b6e4887eea952735aec86
7
+ data.tar.gz: 50be9b4b2d8caa5384de8c30a11b8e472a7f3cb350374bf5e62b09c6dcc080ae003a8920acebcd207359e304d36b7b389fc6e0f97c5d4aef7d2cf01aa3536e3e
@@ -0,0 +1,23 @@
1
+ module ComposedValidations
2
+ class AndStringJoiner
3
+ attr_reader :strings, :last_two
4
+ def initialize(*strings)
5
+ @strings = strings
6
+ @last_two = @strings.pop(2)
7
+ end
8
+
9
+ def to_s
10
+ [first_elements_string, last_two_elements_string].join(", ").gsub(/^, /,'')
11
+ end
12
+
13
+ private
14
+
15
+ def first_elements_string
16
+ strings.join(", ")
17
+ end
18
+
19
+ def last_two_elements_string
20
+ last_two.join(" and ")
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,20 @@
1
+ module ComposedValidations
2
+ class AndValidator
3
+ attr_reader :validators
4
+ def initialize(validators)
5
+ @validators = Array(validators)
6
+ end
7
+
8
+ def valid?(record)
9
+ validators.all? do |validator|
10
+ validator.valid?(record)
11
+ end
12
+ end
13
+
14
+ def message
15
+ @message ||= AndStringJoiner.new(validators.map(&:message)).to_s
16
+ end
17
+
18
+ end
19
+
20
+ end
@@ -15,12 +15,19 @@ module ComposedValidations
15
15
  end
16
16
 
17
17
  class Decorator < SimpleDelegator
18
- attr_reader :validators
19
18
  def initialize(resource, validators)
20
19
  @validators = validators
21
20
  super(resource)
22
21
  end
23
22
 
23
+ def validators
24
+ @validator_return ||= begin
25
+ Hash[@validators.map do |property, validators|
26
+ [property.to_sym, Array(validators)]
27
+ end]
28
+ end
29
+ end
30
+
24
31
  def class
25
32
  __getobj__.class
26
33
  end
@@ -1,5 +1,8 @@
1
1
  module ComposedValidations
2
2
  class ValidatedProperty
3
3
  vattr_initialize :validated_property, :property_accessor
4
+ def to_sym
5
+ validated_property
6
+ end
4
7
  end
5
8
  end
@@ -1,3 +1,3 @@
1
1
  module ComposedValidations
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -7,7 +7,9 @@ module ComposedValidations
7
7
  autoload :DecorateProperties, "composed_validations/decorate_properties"
8
8
  autoload :PropertyValidator, "composed_validations/property_validator"
9
9
  autoload :OrValidator, "composed_validations/or_validator"
10
+ autoload :AndValidator, "composed_validations/and_validator"
10
11
  autoload :OrStringJoiner, "composed_validations/or_string_joiner"
12
+ autoload :AndStringJoiner, "composed_validations/and_string_joiner"
11
13
  autoload :ValidatedProperty, "composed_validations/validated_property"
12
14
 
13
15
  def ValidatedProperty(value)
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe ComposedValidations::AndStringJoiner do
4
+ subject { described_class.new(*strings) }
5
+ context "when there's two strings" do
6
+ let(:strings) { ["one", "two"] }
7
+ it "should join with and" do
8
+ expect(subject.to_s).to eq "one and two"
9
+ end
10
+ end
11
+ context "when there's three strings" do
12
+ let(:strings) { ["one", "two", "three"] }
13
+ it "should join with a comma and and" do
14
+ expect(subject.to_s).to eq "one, two and three"
15
+ end
16
+ end
17
+ context "when there's four strings" do
18
+ let(:strings) { ["one", "two", "three", "four"] }
19
+ it "should join nicely" do
20
+ expect(subject.to_s).to eq "one, two, three and four"
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe ComposedValidations::AndValidator do
4
+ subject { described_class.new(validators) }
5
+ let(:validators) { [ validator_1, validator_2 ] }
6
+ let(:validator_1) { mock_validator(valid: true) }
7
+ let(:validator_2) { mock_validator(valid: true) }
8
+
9
+ describe "#valid?" do
10
+ let(:value) { double("value") }
11
+ let(:result) { subject.valid?(value) }
12
+ context "when both validators are valid" do
13
+ it "should return true" do
14
+ expect(result).to eq true
15
+ end
16
+ end
17
+ context "when one is valid" do
18
+ let(:validator_2) { mock_validator(valid: false) }
19
+ it "should return false" do
20
+ expect(result).to eq false
21
+ end
22
+ end
23
+ context "when both are invalid" do
24
+ let(:validator_1) { mock_validator(valid: false) }
25
+ let(:validator_2) { mock_validator(valid: false) }
26
+ it "should return false" do
27
+ expect(result).to eq false
28
+ end
29
+ context "with one valid validator" do
30
+ let(:validators) { validator_1 }
31
+ let(:validator_1) { mock_validator(valid: true) }
32
+ it "should return true" do
33
+ expect(result).to eq true
34
+ end
35
+ end
36
+ end
37
+ describe "#message" do
38
+ it "should combine the two validators' messages" do
39
+ expect(subject.message).to eq "message and message"
40
+ end
41
+ context "with one validator" do
42
+ let(:validators) { validator_1 }
43
+ it "should just return one message when there's one" do
44
+ expect(subject.message).to eq "message"
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ def mock_validator(valid:)
51
+ i = double("validator")
52
+ allow(i).to receive(:valid?).and_return(valid)
53
+ allow(i).to receive(:message).and_return("message")
54
+ i
55
+ end
56
+ end
@@ -24,10 +24,15 @@ RSpec.describe ComposedValidations::DecorateProperties do
24
24
  expect(ComposedValidations::WithValidatedProperty).to have_received(:new).with(resource, :title, validator)
25
25
  expect(ComposedValidations::WithValidatedProperty).to have_received(:new).with(result.__getobj__, :title, validator2)
26
26
  end
27
- it "should respond to #validators" do
28
- result = subject.run
27
+ end
29
28
 
30
- expect(result.validators).to eq property_mapper
29
+ describe "#validators" do
30
+ it "should give back the properties" do
31
+ expect(subject.run.validators).to eq property_mapper
32
+ end
33
+ it "should give back accessors as keys if given a ValidatedProperty" do
34
+ result = described_class.new(resource, {ComposedValidations::ValidatedProperty.new(:lcsubject, :lcsubject_ids) => validator}).run
35
+ expect(result.validators).to eq ({:lcsubject => [validator]})
31
36
  end
32
37
  end
33
38
 
@@ -13,6 +13,12 @@ RSpec.describe ComposedValidations::ValidatedProperty do
13
13
  end
14
14
  end
15
15
 
16
+ describe "#to_sym" do
17
+ it "should return the validated property" do
18
+ expect(subject.to_sym).to eq :validated_property
19
+ end
20
+ end
21
+
16
22
  describe "Caster" do
17
23
  subject { ComposedValidations::ValidatedProperty(value) }
18
24
  context "when given a string" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: composed_validations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Trey Terrell
@@ -124,6 +124,8 @@ files:
124
124
  - circle.yml
125
125
  - composed_validations.gemspec
126
126
  - lib/composed_validations.rb
127
+ - lib/composed_validations/and_string_joiner.rb
128
+ - lib/composed_validations/and_validator.rb
127
129
  - lib/composed_validations/decorate_properties.rb
128
130
  - lib/composed_validations/or_string_joiner.rb
129
131
  - lib/composed_validations/or_validator.rb
@@ -131,6 +133,8 @@ files:
131
133
  - lib/composed_validations/validated_property.rb
132
134
  - lib/composed_validations/version.rb
133
135
  - lib/composed_validations/with_validated_property.rb
136
+ - spec/composed_validations/and_string_joiner_spec.rb
137
+ - spec/composed_validations/and_validator_spec.rb
134
138
  - spec/composed_validations/decorate_properties_spec.rb
135
139
  - spec/composed_validations/or_string_joiner_spec.rb
136
140
  - spec/composed_validations/or_validator_spec.rb
@@ -164,6 +168,8 @@ signing_key:
164
168
  specification_version: 4
165
169
  summary: Composes validations onto properties of an object.
166
170
  test_files:
171
+ - spec/composed_validations/and_string_joiner_spec.rb
172
+ - spec/composed_validations/and_validator_spec.rb
167
173
  - spec/composed_validations/decorate_properties_spec.rb
168
174
  - spec/composed_validations/or_string_joiner_spec.rb
169
175
  - spec/composed_validations/or_validator_spec.rb