rasti-form 1.0.3 → 1.1.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
  SHA1:
3
- metadata.gz: 4741522b947fe4e41362f7350a8334b93fb949c1
4
- data.tar.gz: e778bd35d8f3a8f48e095482076b403ce222541a
3
+ metadata.gz: d70488681e6a487f474d2988f6e2344795bcca7d
4
+ data.tar.gz: 79334807aac54d47c80092be57ee122fc828321b
5
5
  SHA512:
6
- metadata.gz: c371e0e8f3966238846156a150aa99fff8a83dca10315f7a4ff819b9f1ecadad52a5dfb710f3685ec9d724f34b39f6afa8e384d2e0882337553a2e02215b955a
7
- data.tar.gz: c13b74e182bd6fb8d6c9f9a55ea31cd6db1defe8b01a8605a8a9fe85aa7300be247a1ced2e203fe0d0d2d978aa40e71dcbb87e1aee20f33ffd577d98cc0f7cf0
6
+ metadata.gz: 5f393c30ca974a0204f6fbe3c86e92fb7df865171fbd5969fba5d3783b45214de20bbd714ff57e99750a0de484546fe748d313a80b45679ca3061c321db8e44b
7
+ data.tar.gz: 417a854f878b0ec9007b36529f93358f406019e41143a104bdf804a60528d3a9b05bf38c4ac2824341fbb44a0c025c1f00bbf7ced06790b7bf6d77e0b443a9e1
@@ -1,5 +1,5 @@
1
1
  module Rasti
2
2
  class Form
3
- VERSION = '1.0.3'
3
+ VERSION = '1.1.0'
4
4
  end
5
5
  end
data/lib/rasti/form.rb CHANGED
@@ -56,12 +56,16 @@ module Rasti
56
56
  end
57
57
  alias_method :inspect, :to_s
58
58
 
59
- def attributes
60
- assigned_attribute_names.each_with_object({}) do |name, hash|
59
+ def attributes(options={})
60
+ attributes_filter = {only: assigned_attribute_names, except: []}.merge(options)
61
+ (attributes_filter[:only] - attributes_filter[:except]).each_with_object({}) do |name, hash|
61
62
  hash[name] = serialize(read_attribute(name))
62
63
  end
63
64
  end
64
- alias_method :to_h, :attributes
65
+
66
+ def to_h
67
+ attributes
68
+ end
65
69
 
66
70
  def assigned?(name)
67
71
  assigned_attribute_names.include? name
@@ -112,7 +116,7 @@ module Rasti
112
116
  end
113
117
 
114
118
  def assigned_attribute_names
115
- self.class.attribute_names & instance_variables.map { |v| v.to_s[1..-1].to_sym }
119
+ @assigned_attribute_names ||= self.class.attribute_names & instance_variables.map { |v| v.to_s[1..-1].to_sym }
116
120
  end
117
121
 
118
122
  def serialize(value)
data/spec/form_spec.rb CHANGED
@@ -262,33 +262,61 @@ describe Rasti::Form do
262
262
 
263
263
  end
264
264
 
265
- it 'Attributes (to_h)' do
266
- t = Rasti::Form::Types
267
- address_class = Rasti::Form[street: t::String, number: t::Integer]
268
- contact_class = Rasti::Form[
269
- name: t::String,
270
- age: t::Integer,
271
- phones: t::Hash[t::Symbol, t::Integer],
272
- addresses: t::Array[t::Form[address_class]]
273
- ]
274
-
275
- attributes = {
276
- name: 'John',
277
- age: 24,
278
- phones: {
279
- office: 1234567890,
280
- house: 456456456
281
- },
282
- addresses: [
283
- {street: 'Lexington Avenue', number: 123},
284
- {street: 'Park Avenue', number: 456}
265
+ describe 'Attributes' do
266
+
267
+ let :address_class do
268
+ Rasti::Form[
269
+ street: Rasti::Form::Types::String,
270
+ number: Rasti::Form::Types::Integer
271
+ ]
272
+ end
273
+
274
+ let :contact_class do
275
+ Rasti::Form[
276
+ name: Rasti::Form::Types::String,
277
+ age: Rasti::Form::Types::Integer,
278
+ phones: Rasti::Form::Types::Hash[Rasti::Form::Types::Symbol, Rasti::Form::Types::Integer],
279
+ addresses: Rasti::Form::Types::Array[Rasti::Form::Types::Form[address_class]],
280
+ hobbies: Rasti::Form::Types::Array[Rasti::Form::Types::String]
285
281
  ]
286
- }
282
+ end
283
+
284
+ let :attributes do
285
+ {
286
+ name: 'John',
287
+ age: 24,
288
+ phones: {
289
+ office: 1234567890,
290
+ house: 456456456
291
+ },
292
+ addresses: [
293
+ {street: 'Lexington Avenue', number: 123},
294
+ {street: 'Park Avenue', number: 456}
295
+ ]
296
+ }
297
+ end
298
+
299
+ it 'All (to_h)' do
300
+ contact = contact_class.new attributes
287
301
 
288
- contact = contact_class.new attributes
302
+ contact.attributes.must_equal attributes
303
+ contact.to_h.must_equal attributes
304
+ end
305
+
306
+ it 'Only' do
307
+ contact = contact_class.new attributes
308
+
309
+ contact.attributes(only: [:name, :age]).must_equal name: attributes[:name],
310
+ age: attributes[:age]
311
+ end
312
+
313
+ it 'Except' do
314
+ contact = contact_class.new attributes
315
+
316
+ contact.attributes(except: [:age, :addresses]).must_equal name: attributes[:name],
317
+ phones: attributes[:phones]
318
+ end
289
319
 
290
- contact.attributes.must_equal attributes
291
- contact.attributes.must_equal contact.to_h
292
320
  end
293
321
 
294
322
  it 'to_s' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rasti-form
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriel Naiman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-12-01 00:00:00.000000000 Z
11
+ date: 2017-12-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_require