partitional 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 +4 -4
- data/lib/partitional.rb +2 -8
- data/lib/partitional/model.rb +8 -3
- data/lib/partitional/validator.rb +40 -0
- data/lib/partitional/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c74db8079513aa01fad882503d4b72a9ea92e144b39c7c5f1f482455a101d73a
|
4
|
+
data.tar.gz: 78dbf9d752bf0fdd1acccf5b06669ecef4ce1233c584de3b70a1c698b742e75f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e122df258ba7f0773b3888f87060e54b7fc267d40e6cd7aa9c30a0de9ccbd553f7b468dff57efe3d454df3adaf4418303616540f5312b5cbf2494ecf8b3ae969
|
7
|
+
data.tar.gz: c193c62458ba3b65534793b2ba09bd7332c2ce2008e11b6f0a5ceefec08812cb4ca396d5ef247dd708b4ce24d9c6e924c75dcf292924954145e947a2c2694caf
|
data/lib/partitional.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'active_support/concern'
|
2
2
|
require 'partitional/version'
|
3
|
+
require 'partitional/validator'
|
3
4
|
|
4
5
|
module Partitional
|
5
6
|
extend ActiveSupport::Concern
|
@@ -49,14 +50,7 @@ module Partitional
|
|
49
50
|
end
|
50
51
|
|
51
52
|
def define_partial_validator(name, mapping)
|
52
|
-
|
53
|
-
partial = send(name)
|
54
|
-
partial.validate
|
55
|
-
|
56
|
-
partial.errors.each do |attr, message|
|
57
|
-
errors.add(mapping[attr], message)
|
58
|
-
end
|
59
|
-
end
|
53
|
+
validates name, partitional: true
|
60
54
|
end
|
61
55
|
end
|
62
56
|
end
|
data/lib/partitional/model.rb
CHANGED
@@ -4,7 +4,8 @@ module Partitional
|
|
4
4
|
class Model
|
5
5
|
include ActiveModel::Model
|
6
6
|
|
7
|
-
attr_accessor :record
|
7
|
+
attr_accessor :record
|
8
|
+
attr_writer :mapping
|
8
9
|
|
9
10
|
def self.attributes
|
10
11
|
@attributes ||= []
|
@@ -42,13 +43,13 @@ module Partitional
|
|
42
43
|
|
43
44
|
def [](attr)
|
44
45
|
attr = attr.to_s.to_sym
|
45
|
-
reader =
|
46
|
+
reader = mapping.fetch(attr) { attr }
|
46
47
|
record ? record_send(reader) : instance_variable_get(:"@#{attr}")
|
47
48
|
end
|
48
49
|
|
49
50
|
def []=(attr, val)
|
50
51
|
attr = attr.to_s.to_sym
|
51
|
-
writer =
|
52
|
+
writer = mapping.fetch(attr) { attr }
|
52
53
|
record ? record_send(:"#{writer}=", val) : instance_variable_set(:"@#{attr}", val)
|
53
54
|
end
|
54
55
|
|
@@ -60,6 +61,10 @@ module Partitional
|
|
60
61
|
Hash[attrs]
|
61
62
|
end
|
62
63
|
|
64
|
+
def mapping
|
65
|
+
@mapping || {}
|
66
|
+
end
|
67
|
+
|
63
68
|
protected
|
64
69
|
|
65
70
|
def record_send(method, *args)
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
|
3
|
+
class PartitionalValidator < ActiveModel::EachValidator
|
4
|
+
ALLOWED_OPTIONS = %i[
|
5
|
+
allow_nil
|
6
|
+
allow_blank
|
7
|
+
if
|
8
|
+
unless
|
9
|
+
message
|
10
|
+
attributes
|
11
|
+
]
|
12
|
+
|
13
|
+
def check_validity!
|
14
|
+
invalid_keys = []
|
15
|
+
options.except(*ALLOWED_OPTIONS).each_pair do |key, value|
|
16
|
+
invalid_keys.push("#{key}: #{value}")
|
17
|
+
end
|
18
|
+
raise ArgumentError, "#{invalid_keys.join(', ')} is invalid options." unless invalid_keys.empty?
|
19
|
+
end
|
20
|
+
|
21
|
+
def validate_each(record, attribute, value)
|
22
|
+
return if options[:if].present? && !to_value(record, options[:if])
|
23
|
+
return if options[:unless].present? && to_value(record, options[:unless])
|
24
|
+
|
25
|
+
value.validate
|
26
|
+
mapping = value.mapping
|
27
|
+
|
28
|
+
value.errors.each do |attr, message|
|
29
|
+
record.errors.add(mapping[attr], message)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
protected
|
34
|
+
|
35
|
+
def to_value(record, value)
|
36
|
+
return record.send(value) if value.is_a?(Symbol)
|
37
|
+
return value.call(record) if value.is_a?(Proc)
|
38
|
+
value
|
39
|
+
end
|
40
|
+
end
|
data/lib/partitional/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: partitional
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sho Kusano
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-12-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -112,6 +112,7 @@ files:
|
|
112
112
|
- bin/setup
|
113
113
|
- lib/partitional.rb
|
114
114
|
- lib/partitional/model.rb
|
115
|
+
- lib/partitional/validator.rb
|
115
116
|
- lib/partitional/version.rb
|
116
117
|
- partitional.gemspec
|
117
118
|
homepage: https://github.com/rosylilly/partitional
|