duck_record 0.0.22 → 0.0.23

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: e4921c51fca978e6910b1c8a0e9390b01f0d8408
4
- data.tar.gz: 77d9a49aecd1011ca0225536bcc420cc24ac9d03
3
+ metadata.gz: bc4554980a2453870fa71616cd56279ce83b5454
4
+ data.tar.gz: 0e62dba8f62514499fc95fe75a9f24548e93f6ed
5
5
  SHA512:
6
- metadata.gz: a8e66fdc4569a943dd53f0ba8d0d49fb9c776ea3e062b5ba1657340925f707a4689ee8c3ed0e3aead7d1c00ca9e8d16e46955a238c1b1f5a23de2d6088b752a2
7
- data.tar.gz: 3d2bb86b4dbdc92a30596a3cafc1cb85483ad317bab5375f7e4746957173354ba542e1996af724a8f6c9ef9a74fd5bc4ba1e7ca9d4af3c0978f6be71a72a25fc
6
+ metadata.gz: 3c4bdf7e498269309f56201a3f605d5939202561bafdbd106efebbd4c5f8d8c86a6bf2607c5678d04ef0beba5654b805c53ee75dcfb930dcb2cbeb8cf22f4930
7
+ data.tar.gz: d1f951ff91b996436acb2a244b3f8bf3cee8bdd58f742c31e968465f1c531818ec4a4f97b8dfc0fb1865751c39a8235967ae445ebbdeaf50e74b37b929c69d4c
@@ -9,6 +9,7 @@ en:
9
9
  messages:
10
10
  required: "must exist"
11
11
  taken: "has already been taken"
12
+ subset: "is not included in the list"
12
13
 
13
14
  # Active Record models configuration
14
15
  duck_record:
@@ -1,4 +1,5 @@
1
1
  require "duck_record/validations/uniqueness_on_real_record"
2
+ require "duck_record/validations/subset"
2
3
 
3
4
  module DuckRecord
4
5
  class RecordInvalid < DuckRecordError
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_model/validations/clusivity"
4
+
5
+ module DuckRecord
6
+ module Validations
7
+ class SubsetValidator < ActiveModel::EachValidator # :nodoc:
8
+ include ActiveModel::Validations::Clusivity
9
+
10
+ def validate_each(record, attribute, value)
11
+ unless subset?(record, value)
12
+ record.errors.add(attribute, :subset, options.except(:in, :within).merge!(value: value))
13
+ end
14
+ end
15
+
16
+ private
17
+
18
+ def subset?(record, value)
19
+ return false unless value.respond_to?(:to_a)
20
+
21
+ enumerable = value.to_a
22
+ members =
23
+ if delimiter.respond_to?(:call)
24
+ delimiter.call(record)
25
+ elsif delimiter.respond_to?(:to_sym)
26
+ record.send(delimiter)
27
+ else
28
+ delimiter
29
+ end
30
+
31
+ (members & enumerable).size == enumerable.size
32
+ end
33
+ end
34
+
35
+ module HelperMethods
36
+ # Validates whether the value of the specified attribute is available in a
37
+ # particular enumerable object.
38
+ #
39
+ # class Person < ActiveRecord::Base
40
+ # validates_inclusion_of :gender, in: %w( m f )
41
+ # validates_inclusion_of :age, in: 0..99
42
+ # validates_inclusion_of :format, in: %w( jpg gif png ), message: "extension %{value} is not included in the list"
43
+ # validates_inclusion_of :states, in: ->(person) { STATES[person.country] }
44
+ # validates_inclusion_of :karma, in: :available_karmas
45
+ # end
46
+ #
47
+ # Configuration options:
48
+ # * <tt>:in</tt> - An enumerable object of available items. This can be
49
+ # supplied as a proc, lambda or symbol which returns an enumerable. If the
50
+ # enumerable is a numerical, time or datetime range the test is performed
51
+ # with <tt>Range#cover?</tt>, otherwise with <tt>include?</tt>. When using
52
+ # a proc or lambda the instance under validation is passed as an argument.
53
+ # * <tt>:within</tt> - A synonym(or alias) for <tt>:in</tt>
54
+ # * <tt>:message</tt> - Specifies a custom error message (default is: "is
55
+ # not included in the list").
56
+ #
57
+ # There is also a list of default options supported by every validator:
58
+ # +:if+, +:unless+, +:on+, +:allow_nil+, +:allow_blank+, and +:strict+.
59
+ # See <tt>ActiveModel::Validations#validates</tt> for more information
60
+ def validates_subset_of(*attr_names)
61
+ validates_with SubsetValidator, _merge_attributes(attr_names)
62
+ end
63
+ end
64
+ end
65
+ end
@@ -1,3 +1,3 @@
1
1
  module DuckRecord
2
- VERSION = "0.0.22"
2
+ VERSION = "0.0.23"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: duck_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.22
4
+ version: 0.0.23
5
5
  platform: ruby
6
6
  authors:
7
7
  - jasl
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-02-24 00:00:00.000000000 Z
11
+ date: 2018-03-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -150,6 +150,7 @@ files:
150
150
  - lib/duck_record/type/time.rb
151
151
  - lib/duck_record/type/unsigned_integer.rb
152
152
  - lib/duck_record/validations.rb
153
+ - lib/duck_record/validations/subset.rb
153
154
  - lib/duck_record/validations/uniqueness_on_real_record.rb
154
155
  - lib/duck_record/version.rb
155
156
  - lib/tasks/acts_as_record_tasks.rake
@@ -173,7 +174,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
173
174
  version: '0'
174
175
  requirements: []
175
176
  rubyforge_project:
176
- rubygems_version: 2.6.12
177
+ rubygems_version: 2.6.13
177
178
  signing_key:
178
179
  specification_version: 4
179
180
  summary: Used for creating virtual models like ActiveType or ModelAttribute does