mini_model 0.0.1 → 0.0.2

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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -3
  3. data/lib/mini_model.rb +7 -4
  4. data/test/all.rb +4 -4
  5. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9e8708424bd24b1a49650936429addbe7a258728
4
- data.tar.gz: 677ed6b7a4b871dcff0f9c1eaee2e44c9c06cce4
3
+ metadata.gz: b80bb2389a67ac22d77537e366e2de43491ad63c
4
+ data.tar.gz: 989d69035efd19a70fb42e32377894a86b079469
5
5
  SHA512:
6
- metadata.gz: c57dfc9f23486c77ea40014113240c8f5e847a38b0dff3331ce161e383ec0849dc2432b31d118940a4730356d43b890d8a3ca6b4aef960d8ee0889b7aa9a784a
7
- data.tar.gz: 3fc9fc50f95689f908bf098fba7d44071a4c42c416d703503fe6eecd8c67f99b02ffd28dd1f2b1cc01e3620e2b76c56dcbb4abdbc7309b97aebbe258bcf028ce
6
+ metadata.gz: daf447cd0e17c3ae3f290363d6ae915c412672fbc2ea465a61f0b2bd7a1216c776ec652f5fe1bc8667c75248d3a60ed4007fc9d008beef8de793611fd2883135
7
+ data.tar.gz: 66f1c3d97813966cfdac883001720d9f1d800c62bcb5c75fdb6b9c05e692ce0ce66c3fd18914f430f1d29d8667dd68a0b58f7b3bc847dae08ce32081b75e8106
data/README.md CHANGED
@@ -78,9 +78,8 @@ We don't want to be assigning nil all over our associations and what have you.
78
78
 
79
79
  `#attributes` Gets the attributes hash.
80
80
 
81
- `#attributes=` For each key/value in the given hash, sends the writer to self.
82
- This means it'll be a missing method if you have not created the writer via the
83
- `::attribute` macro, or manually. This is not safe to use with user input.
81
+ `#attributes=` For each key/value in the given hash, sends the writer (from key)
82
+ to self if the method exists.
84
83
 
85
84
  `#==` Compares two models to see if they are equals. It ensures that the class,
86
85
  id, and attributes of each are the same.
data/lib/mini_model.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module MiniModel
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
 
4
4
  class Error < StandardError; end
5
5
 
@@ -151,13 +151,16 @@ module MiniModel
151
151
 
152
152
  # #attributes= is vulnerable to mass assignment attacks if used
153
153
  # directly with user input. Some sort of filter must be in place
154
- # before setting attributes or initializing a new model. Sending
155
- # a key in the hash argument that doesn't have an accessor raises an error.
154
+ # before setting attributes or initializing a new model.
156
155
  def attributes=(attributes)
157
156
  @attributes = {}
158
157
 
159
158
  attributes.each do |key, value|
160
- send(:"#{key}=", value)
159
+ writer = :"#{key}="
160
+
161
+ if respond_to?(writer)
162
+ send(writer, value)
163
+ end
161
164
  end
162
165
  end
163
166
 
data/test/all.rb CHANGED
@@ -248,12 +248,12 @@ test '#attributes= resets all attributes, but not id' do
248
248
  assert_equal user.email, user.attributes[:email]
249
249
  end
250
250
 
251
- test '#attributes= raises when there is no accessor for a key' do
251
+ test '#attributes= ignores keys without a writer' do
252
252
  user = User.new
253
253
 
254
- assert_raise do
255
- user.attributes = { foo: :bar }
256
- end
254
+ user.attributes = { foo: :bar }
255
+
256
+ assert !user.attributes.has_key?(:foo)
257
257
  end
258
258
 
259
259
  setup do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mini_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Weiss