datory 1.0.0.rc14 → 1.0.0.rc15

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: 7c1b9b4a2fbe194c484a57960111c07b2285db29c889406a9e6e29571866b4f0
4
- data.tar.gz: ecafad6097d0ba8867b2aa5b332c4c7d91e959728cf24393eb9356131c5fe437
3
+ metadata.gz: 7cf1723be06fbf81937c9e8e9b20cf1e37f3d70765dbfde943fc0e3640ab3c13
4
+ data.tar.gz: 90f48cba076d54721d8c03858016326a87c4b6c3dea2bcbabdff17ac7c69677e
5
5
  SHA512:
6
- metadata.gz: 97dfe2487f3fb9859d4cbde74f4473d5c2c993ac91f6804afc2bf7d1441d871b90c5400c0911691231587e258db6047cd880289c7449feafaf93af8a0fd82a00
7
- data.tar.gz: 9bf01b58328bbe3c6c5822523c34d8383aacf8aa21736a64da8435730f553a8dfa3bf8a147a4e07bb5b86c2b872ab23973f013058802be28f2d443e7678ef185
6
+ metadata.gz: '0794e8bf0e3bbf898c595199207ecb6f833ab84b43450a493339c7a27f87577c3a4fdb43d48e2ab21f8a93c7cf49fdd55ca0e4182d18c89ad0a66bb613a2d0a5'
7
+ data.tar.gz: 8571cd2fbff8d5afb5b81fc68a8ae37c5d8ecb3f387037afdaddd61b0bad8974d0198d3c85750f028b9fd7a57e7b66d9a92bc3e0fd19849fc7bd6ab547c45237
@@ -3,13 +3,49 @@
3
3
  module Datory
4
4
  module Attributes
5
5
  class Form
6
+ attr_reader :model
7
+
6
8
  def initialize(context, model)
7
9
  @context = context
8
10
  @model = model
9
11
  end
10
12
 
13
+ def target
14
+ @context
15
+ end
16
+
17
+ def update(**attributes)
18
+ if model_collection?
19
+ raise_misuse "The `update` method cannot be used with a collection. Instead, use the `update_by` method."
20
+ end
21
+
22
+ found_keys = model.keys & attributes.keys
23
+
24
+ reset!
25
+
26
+ model.merge!(attributes.slice(*found_keys))
27
+ end
28
+
29
+ def update_by(index, **attributes) # rubocop:disable Metrics/MethodLength
30
+ unless model_collection?
31
+ raise_misuse "The `update_by` method cannot be used without a collection. Instead, use the `update` method."
32
+ end
33
+
34
+ reset!
35
+
36
+ model.map!.with_index do |model_item, model_index|
37
+ if model_index == index
38
+ found_keys = model_item.keys & attributes.keys
39
+
40
+ model_item.merge(attributes.slice(*found_keys))
41
+ else
42
+ model_item
43
+ end
44
+ end
45
+ end
46
+
11
47
  def serialize
12
- @serialize ||= @context.serialize(@model)
48
+ @serialize ||= @context.serialize(model)
13
49
  end
14
50
 
15
51
  def valid?
@@ -23,6 +59,20 @@ module Datory
23
59
  def invalid?
24
60
  !valid?
25
61
  end
62
+
63
+ private
64
+
65
+ def raise_misuse(message)
66
+ raise Datory::Exceptions::MisuseError, message
67
+ end
68
+
69
+ def model_collection?
70
+ [Set, Array].include?(model.class)
71
+ end
72
+
73
+ def reset!
74
+ @serialize = nil
75
+ end
26
76
  end
27
77
  end
28
78
  end
@@ -2,7 +2,9 @@
2
2
 
3
3
  module Datory
4
4
  module Exceptions
5
+ class Base < StandardError; end
5
6
  class SerializationError < Datory::Service::Exceptions::Input; end
6
7
  class DeserializationError < Datory::Service::Exceptions::Input; end
8
+ class MisuseError < Base; end
7
9
  end
8
10
  end
@@ -5,7 +5,7 @@ module Datory
5
5
  MAJOR = 1
6
6
  MINOR = 0
7
7
  PATCH = 0
8
- PRE = "rc14"
8
+ PRE = "rc15"
9
9
 
10
10
  STRING = [MAJOR, MINOR, PATCH, PRE].compact.join(".")
11
11
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: datory
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.rc14
4
+ version: 1.0.0.rc15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anton Sokolov