chozo 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,6 +6,11 @@ module Chozo
6
6
  # @author Jamie Winsor <jamie@vialstudios.com>
7
7
  module VariaModel
8
8
  module ClassMethods
9
+ ASSIGNMENT_MODES = [
10
+ :whitelist,
11
+ :carefree
12
+ ]
13
+
9
14
  # @return [HashWithIndifferentAccess]
10
15
  def attributes
11
16
  @attributes ||= Hashie::Mash.new
@@ -16,6 +21,26 @@ module Chozo
16
21
  @validations ||= HashWithIndifferentAccess.new
17
22
  end
18
23
 
24
+ # @return [Symbol]
25
+ def assignment_mode
26
+ @assignment_mode ||= :whitelist
27
+ end
28
+
29
+ # Set the attribute mass assignment mode
30
+ # * :whitelist - only attributes defined on the class will have values set
31
+ # * :carefree - values will be set for attributes that are not explicitly defined
32
+ # in the class definition
33
+ #
34
+ # @param [Symbol] mode
35
+ # an assignment mode to use @see {ASSIGNMENT_MODES}
36
+ def set_assignment_mode(mode)
37
+ unless ASSIGNMENT_MODES.include?(mode)
38
+ raise ArgumentError, "unknown assignment mode: #{mode}"
39
+ end
40
+
41
+ @assignment_mode = mode
42
+ end
43
+
19
44
  # @param [#to_s] name
20
45
  # @option options [Symbol, Array<Symbol>] :type
21
46
  # @option options [Boolean] :required
@@ -172,11 +197,11 @@ module Chozo
172
197
  end
173
198
 
174
199
  def mass_assign(new_attrs = {})
175
- attributes.dotted_paths.each do |dotted_path|
176
- value = new_attrs.dig(dotted_path)
177
- next if value.nil?
178
-
179
- set_attribute(dotted_path, value)
200
+ case self.class.assignment_mode
201
+ when :whitelist
202
+ whitelist_assign(new_attrs)
203
+ when :carefree
204
+ carefree_assign(new_attrs)
180
205
  end
181
206
  end
182
207
  alias_method :attributes=, :mass_assign
@@ -233,6 +258,21 @@ module Chozo
233
258
  def add_error(attribute, message)
234
259
  self.errors[attribute] ||= Array.new
235
260
  self.errors[attribute] << message
236
- end
261
+ end
262
+
263
+ private
264
+
265
+ def carefree_assign(new_attrs = {})
266
+ attributes.deep_merge!(new_attrs)
267
+ end
268
+
269
+ def whitelist_assign(new_attrs = {})
270
+ attributes.dotted_paths.each do |dotted_path|
271
+ value = new_attrs.dig(dotted_path)
272
+ next if value.nil?
273
+
274
+ set_attribute(dotted_path, value)
275
+ end
276
+ end
237
277
  end
238
278
  end
@@ -1,3 +1,3 @@
1
1
  module Chozo
2
- VERSION = "0.4.0"
2
+ VERSION = "0.4.1"
3
3
  end
@@ -90,6 +90,32 @@ describe Chozo::VariaModel do
90
90
  validations.should be_empty
91
91
  end
92
92
  end
93
+
94
+ describe "#assignment_mode" do
95
+ it "returns the default assignment mode :whitelist" do
96
+ subject.assignment_mode.should eql(:whitelist)
97
+ end
98
+ end
99
+
100
+ describe "#set_assignment_mode" do
101
+ it "sets the assignment_mode to whitelist" do
102
+ subject.set_assignment_mode(:whitelist)
103
+
104
+ subject.assignment_mode.should eql(:whitelist)
105
+ end
106
+
107
+ it "sets the assignment_mode to carefree" do
108
+ subject.set_assignment_mode(:carefree)
109
+
110
+ subject.assignment_mode.should eql(:carefree)
111
+ end
112
+
113
+ it "raises if given an invalid assignment mode" do
114
+ expect {
115
+ subject.set_assignment_mode(:not_a_real_mode)
116
+ }.to raise_error(ArgumentError)
117
+ end
118
+ end
93
119
  end
94
120
 
95
121
  describe "::validate_kind_of" do
@@ -498,8 +524,28 @@ describe Chozo::VariaModel do
498
524
  }
499
525
 
500
526
  subject.mass_assign(new_attrs)
527
+ subject.attributes[:undefined_attribute].should be_nil
501
528
  subject.should_not respond_to(:undefined_attribute)
502
529
  end
530
+
531
+ context "when in carefree assignment mode" do
532
+ subject do
533
+ Class.new do
534
+ include Chozo::VariaModel
535
+
536
+ set_assignment_mode :carefree
537
+ end.new
538
+ end
539
+
540
+ it "does not ignore values which are not defined" do
541
+ new_attrs = {
542
+ undefined_attribute: "value"
543
+ }
544
+
545
+ subject.mass_assign(new_attrs)
546
+ subject.attributes[:undefined_attribute].should eql("value")
547
+ end
548
+ end
503
549
  end
504
550
 
505
551
  describe "#from_json" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chozo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -125,7 +125,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
125
125
  version: '0'
126
126
  segments:
127
127
  - 0
128
- hash: -4510203401888880792
128
+ hash: 3156158446945270058
129
129
  requirements: []
130
130
  rubyforge_project:
131
131
  rubygems_version: 1.8.23