relaxo-model 0.17.3 → 0.18.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: 2f9a147ad023213a3a98ec44faf9893f09e8fd1cabbebeaa13f6d499dcd0f73a
4
- data.tar.gz: a92a1cd906a27b04556d326a74305a1c94f8b1f04fa30216bcc63d97230685b6
3
+ metadata.gz: 43646c8e87ba1baad72dd21037a91817468427e6c990f82ed1f6201d319961f7
4
+ data.tar.gz: 3b7e4d1c13908a5fc015e2160e037b3492cf5c41f7ce1b17773de8aaa9bbea6b
5
5
  SHA512:
6
- metadata.gz: bfc0059b36c22d21c497816ceff1eec5d7cb8ed3e0b8f2c046188ead5a5e9e4a307332699290b69476c08f757d17ac8b8578e5eb740c6f6cfdbf2b4196e7329b
7
- data.tar.gz: 39c4da5c519955e5e5187dc358a648c57a1438ae3e2fb7841572c96c2afded168b460b206f94badb0198cef6f7fee0c389ec5a622a1ff8c7ab86635cca7e253f
6
+ metadata.gz: 7a34f73d1f6931122401aa1b3c678adf6894ed9c275ee03420f624d732eafeaac28e854a8f192e34ad3fb15eb3bc7bd2b77e72283620bc4fb165d31bc6924c91
7
+ data.tar.gz: 0dde3caeb52c3cb6ee779986db3568a7acef6fb1786be0b302505ab0c3ca8788be515c632cab06262ec634d4becf5a3f551168b0b3c0a3ae19593e5d65a2b1eb
@@ -120,9 +120,9 @@ module Relaxo
120
120
  @properties[name] = klass
121
121
 
122
122
  self.send(:define_method, name) do
123
- if @changed.include? name
123
+ if @changed.include?(name)
124
124
  return @changed[name]
125
- elsif @attributes.include? name
125
+ elsif @attributes.include?(name)
126
126
  if klass
127
127
  value = @attributes[name]
128
128
 
@@ -68,32 +68,32 @@ module Relaxo
68
68
  @changed.delete(key)
69
69
  @attributes.delete(key)
70
70
  end
71
-
72
- def assign(primative_attributes, only = :all)
73
- enumerator = primative_attributes
74
-
75
- if only == :all
76
- enumerator = enumerator.select{|key, value| self.class.properties.include? key.to_sym}
77
- elsif only.respond_to? :include?
78
- enumerator = enumerator.select{|key, value| only.include? key.to_sym}
79
- end
80
-
81
- enumerator.each do |key, value|
71
+
72
+ ALL = Hash.new(true)
73
+
74
+ def assign(primative_attributes, only = ALL)
75
+ primative_attributes.each do |key, value|
82
76
  key = key.to_sym
83
-
84
- klass = self.class.properties[key]
85
-
86
- if klass
87
- # This might raise a validation error
88
- value = klass.convert_from_primative(@dataset, value)
77
+ case mapping = only[key]
78
+ when true
79
+ # Assign from primitive value:
80
+ if klass = self.class.properties[key]
81
+ value = klass.convert_from_primative(@dataset, value)
82
+ end
83
+
84
+ self[key] = value
85
+ when false, nil
86
+ # Ignore:
87
+ next
88
+ else
89
+ # Nested assignment:
90
+ self[key].assign(value, mapping)
89
91
  end
90
-
91
- self[key] = value
92
92
  end
93
93
 
94
94
  return self
95
95
  end
96
-
96
+
97
97
  def [] name
98
98
  if self.class.properties.include? name
99
99
  self.send(name)
@@ -101,7 +101,7 @@ module Relaxo
101
101
  raise KeyError.new(name)
102
102
  end
103
103
  end
104
-
104
+
105
105
  def []= name, value
106
106
  if self.class.properties.include? name
107
107
  self.send("#{name}=", value)
@@ -109,7 +109,7 @@ module Relaxo
109
109
  raise KeyError.new(name)
110
110
  end
111
111
  end
112
-
112
+
113
113
  def validate(changeset)
114
114
  # Do nothing :)
115
115
  end
@@ -109,7 +109,7 @@ module Relaxo
109
109
  end
110
110
 
111
111
  # Update any calculations:
112
- def before_save
112
+ def before_save(changeset)
113
113
  end
114
114
 
115
115
  def after_save
@@ -148,34 +148,34 @@ module Relaxo
148
148
  end
149
149
 
150
150
  # Save the model object.
151
- def save(dataset)
152
- return if persisted? and @changed.empty?
151
+ def save(changeset)
152
+ before_save(changeset)
153
153
 
154
- before_save
154
+ return if persisted? and @changed.empty?
155
155
 
156
- if errors = self.validate(dataset)
156
+ if errors = self.validate(changeset)
157
157
  return errors
158
158
  end
159
159
 
160
160
  existing_paths = persisted? ? paths.to_a : []
161
161
 
162
162
  # Write data, check if any actual changes made:
163
- object = dataset.append(self.dump)
163
+ object = changeset.append(self.dump)
164
164
  return if object == @object
165
165
 
166
166
  existing_paths.each do |path|
167
- dataset.delete(path)
167
+ changeset.delete(path)
168
168
  end
169
169
 
170
170
  paths do |path|
171
- if dataset.exist?(path)
171
+ if changeset.exist?(path)
172
172
  raise KeyError, "Dataset already contains path: #{path}, when inserting #{@attributes.inspect}"
173
173
  end
174
174
 
175
- dataset.write(path, object)
175
+ changeset.write(path, object)
176
176
  end
177
177
 
178
- @dataset = dataset
178
+ @dataset = changeset
179
179
  @object = object
180
180
 
181
181
  after_save
@@ -183,8 +183,8 @@ module Relaxo
183
183
  return true
184
184
  end
185
185
 
186
- def save!(dataset)
187
- result = self.save(dataset)
186
+ def save!(changeset)
187
+ result = self.save(changeset)
188
188
 
189
189
  if result != true
190
190
  raise ValidationFailure.new(self, result)
@@ -199,13 +199,13 @@ module Relaxo
199
199
  def after_delete
200
200
  end
201
201
 
202
- def delete(dataset)
202
+ def delete(changeset)
203
203
  before_delete
204
204
 
205
205
  @changed.clear
206
206
 
207
207
  paths.each do |path|
208
- dataset.delete(path)
208
+ changeset.delete(path)
209
209
  end
210
210
 
211
211
  after_delete
@@ -20,6 +20,6 @@
20
20
 
21
21
  module Relaxo
22
22
  module Model
23
- VERSION = "0.17.3"
23
+ VERSION = "0.18.0"
24
24
  end
25
25
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: relaxo-model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.17.3
4
+ version: 0.18.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-29 00:00:00.000000000 Z
11
+ date: 2020-06-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: msgpack