partitional 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1fd2f509c71ca5e46903b81b24ac78a5bae441130453c5b8dce4208a3a9e3b3a
4
- data.tar.gz: b20b06d9749591270b536cd0121ccb642878d3cd40f76f812649681793a945bf
3
+ metadata.gz: c74db8079513aa01fad882503d4b72a9ea92e144b39c7c5f1f482455a101d73a
4
+ data.tar.gz: 78dbf9d752bf0fdd1acccf5b06669ecef4ce1233c584de3b70a1c698b742e75f
5
5
  SHA512:
6
- metadata.gz: c597de88ee4d116423b411d0c82460116bb2d69007d55d0e19cfcfe9d16299c9b5b336467b09a37818f5f06e48f04477a0fd8deea7082fb71427551d70116e51
7
- data.tar.gz: d8c0369ac52fc5d8cb6a909929e2df2424fa8e58c636bc6ee1467ffd0016a1054bbdd3889f9fbcc9f41cec191a838af29481cc0269343ce724111c2b00aa5b46
6
+ metadata.gz: e122df258ba7f0773b3888f87060e54b7fc267d40e6cd7aa9c30a0de9ccbd553f7b468dff57efe3d454df3adaf4418303616540f5312b5cbf2494ecf8b3ae969
7
+ data.tar.gz: c193c62458ba3b65534793b2ba09bd7332c2ce2008e11b6f0a5ceefec08812cb4ca396d5ef247dd708b4ce24d9c6e924c75dcf292924954145e947a2c2694caf
@@ -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
- validate do
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
@@ -4,7 +4,8 @@ module Partitional
4
4
  class Model
5
5
  include ActiveModel::Model
6
6
 
7
- attr_accessor :record, :mapping
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 = (mapping || {}).fetch(attr) { attr }
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 = (mapping || {}).fetch(attr) { attr }
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
@@ -1,3 +1,3 @@
1
1
  module Partitional
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
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.1.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-03-25 00:00:00.000000000 Z
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