rasti-model 1.0.0 → 1.0.1

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
  SHA256:
3
- metadata.gz: 69fc7d6502deb7e66837ef4fdd561b1ac56b9125c3420612e0574b28122dcac0
4
- data.tar.gz: 83a974c72689d66b70ed7cbe7d50e0dc0ae93ec4acabc471333124b21d3d0d2e
3
+ metadata.gz: 5d85d0034827688a0aea67f255e2ac0ea95a273da7a38dc66c823f032cf95830
4
+ data.tar.gz: 63fed2869a50ba059f73f878c1eace99b00068df719a1d373bcbe627edac7e16
5
5
  SHA512:
6
- metadata.gz: e9dc23d735dee6c67b37a789cd2146b65dcfd2eaa3bc811cd937d7e80d3361bca87760eb2a49a23d3b94941386eab1cc34e4fdffc06fdd9deba4b86730b93af1
7
- data.tar.gz: dd9c2d798a13065f2825a9b3a988b6c2e7de4333e1a198a052fe24848d2dd96c54e55c187ecd7da2b60dc181adc81f2fbc97434af6f61cb2a5e0a1d2c1e07a84
6
+ metadata.gz: 8934c038b6dabbf06f6c6405751fe9691a75495089ba3c2c177f82dd5c486dbd14954a39a757232774fe6e36b6a631529286db3a9427d79bc857ddddd8852596
7
+ data.tar.gz: 4b967d4b0cfae3cd0b1cf4c405a8422230f01836554108bd4f3e27756d2d50c7bcea46ead161b7066c570296fc3aa2799faafba636d1c8f54ccdc48a7655ebb4
data/lib/rasti/model.rb CHANGED
@@ -175,22 +175,28 @@ module Rasti
175
175
  @serialized_attributes ||= begin
176
176
  cast_attributes!
177
177
 
178
- __cache__.each_with_object({}) do |(attr_name, value), hash|
179
- hash[attr_name] = serialize_value value
178
+ self.class.attributes.each_with_object({}) do |attribute, hash|
179
+ if __cache__.key? attribute.name
180
+ hash[attribute.name] = serialize_value __cache__[attribute.name], attribute.type
181
+ end
180
182
  end
181
183
  end
182
184
  end
183
185
 
184
- def serialize_value(value)
186
+ def serialize_value(value, type=nil)
185
187
  case value
186
188
  when Model
187
189
  value.to_h
188
190
  when Array
189
- value.map { |v| serialize_value v }
191
+ t = type.is_a?(Rasti::Types::Array) ? type.type : nil
192
+ value.map { |v| serialize_value v, t }
190
193
  when Hash
194
+ t = type.is_a?(Rasti::Types::Hash) ? type.value_type : nil
191
195
  value.each_with_object({}) do |(k,v), h|
192
- h[k.to_sym] = serialize_value v
196
+ h[k.to_sym] = serialize_value v, t
193
197
  end
198
+ when Time
199
+ type.is_a?(Rasti::Types::Time) ? value.strftime(type.format) : value
194
200
  else
195
201
  value
196
202
  end
@@ -1,5 +1,5 @@
1
1
  module Rasti
2
2
  class Model
3
- VERSION = '1.0.0'
3
+ VERSION = '1.0.1'
4
4
  end
5
5
  end
data/spec/model_spec.rb CHANGED
@@ -164,12 +164,14 @@ describe Rasti::Model do
164
164
 
165
165
  let :contact_class do
166
166
  Rasti::Model[
167
- id: T::Integer,
168
- name: T::String,
169
- birthday: T::Model[birthday_class],
170
- phones: T::Hash[T::Symbol, T::Integer],
171
- addresses: T::Array[T::Model[address_class]],
172
- labels: T::Array[T::String]
167
+ id: T::Integer,
168
+ name: T::String,
169
+ birthday: T::Model[birthday_class],
170
+ phones: T::Hash[T::Symbol, T::Integer],
171
+ addresses: T::Array[T::Model[address_class]],
172
+ labels: T::Array[T::String],
173
+ created_at: T::Time['%d/%m/%Y %H:%M:%S %z'],
174
+ updated_at: nil
173
175
  ]
174
176
  end
175
177
 
@@ -190,7 +192,9 @@ describe Rasti::Model do
190
192
  {street: 'Lexington Avenue', number: 123},
191
193
  {street: 'Park Avenue', number: 456}
192
194
  ],
193
- labels: ['Friend', 'Work']
195
+ labels: ['Friend', 'Work'],
196
+ created_at: '16/03/2021 09:30:10 -0300',
197
+ updated_at: Time.parse('2021-03-16T11:45:20-03:00')
194
198
  }
195
199
  end
196
200
 
@@ -209,11 +213,13 @@ describe Rasti::Model do
209
213
  it 'Except' do
210
214
  contact = contact_class.new attributes
211
215
 
212
- contact.to_h(except: [:age, :addresses]).must_equal id: attributes[:id],
213
- name: attributes[:name],
214
- birthday: attributes[:birthday],
215
- phones: attributes[:phones],
216
- labels: attributes[:labels]
216
+ excluded_attributes = [:age, :addresses, :created_at, :updated_at]
217
+
218
+ contact.to_h(except: excluded_attributes).must_equal id: attributes[:id],
219
+ name: attributes[:name],
220
+ birthday: attributes[:birthday],
221
+ phones: attributes[:phones],
222
+ labels: attributes[:labels]
217
223
  end
218
224
 
219
225
  it 'Ignore not assigned attributes' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rasti-model
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriel Naiman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-11 00:00:00.000000000 Z
11
+ date: 2021-03-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_require