ares-ext 0.0.3 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ares-ext (0.0.2)
4
+ ares-ext (0.0.4)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
@@ -11,7 +11,7 @@ Gem::Specification.new do |gem|
11
11
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
12
12
  gem.name = "ares-ext"
13
13
  gem.require_paths = ["lib"]
14
- gem.version = '0.0.3'
14
+ gem.version = '0.0.5'
15
15
 
16
16
  #gem.extra_rdoc_files = [
17
17
  # "LICENSE",
@@ -17,12 +17,12 @@ module ActiveResourceExtensions
17
17
  schema[name.to_s]
18
18
  end
19
19
 
20
- protected
21
-
22
20
  def schema
23
21
  @schema ||= remote_schema
24
22
  end
25
23
 
24
+ protected
25
+
26
26
  def schema= new_schema
27
27
  @schema = new_schema
28
28
  end
@@ -34,6 +34,35 @@ module ActiveResourceExtensions
34
34
  end
35
35
 
36
36
  module InstanceMethods
37
+
38
+ # Update attributes of record before saving the record
39
+ #
40
+ # @param attributes [Hash] Hash of record's attributes.
41
+ #
42
+ # @return [Boolean] Result of save.
43
+ def update_attributes( attributes )
44
+ attributes.each_pair do |attribute_name, value|
45
+ raise ArgumentError, "#{attribute_name.inspect} is not declared in schema." unless self.class.is_attribute?( attribute_name )
46
+ @attributes[attribute_name.to_s] = value
47
+ end
48
+ save
49
+ end
50
+
51
+
52
+ # Automatically add attributes that are referenced in the schema but are not passed as parameters.
53
+ #
54
+ # @param attributes [Hash] Hash of record's attributes.
55
+ #
56
+ # @return [Object] self.
57
+ def load attributes
58
+ super attributes
59
+ for attribute_name in self.class.schema.keys
60
+ @attributes[attribute_name.to_s] = nil unless @attributes.has_key?(attribute_name.to_s)
61
+ end
62
+ self
63
+ end
64
+
65
+ # @todo Declare method instead of always relying on method_missing.
37
66
  def method_missing name, *args, &block
38
67
  if self.class.is_attribute? name
39
68
  value = attributes[name.to_s]
@@ -89,6 +89,34 @@ describe ActiveResourceExtensions::ResourceWithSchema do
89
89
  end
90
90
  end
91
91
 
92
+ describe '.initialize' do
93
+
94
+ before do
95
+ @schema = {
96
+ 'name' => 'String',
97
+ 'age' => 'Integer',
98
+ 'created_at' => 'Time',
99
+ 'birth_date' => 'Date',
100
+ 'is_musician' => 'Boolean',
101
+ 'height' => 'Float',
102
+ 'unknown_1' => 'Company',
103
+ 'unknown_2' => '',
104
+ 'company_id' => 'ObjectId'
105
+ }
106
+ Person.send( :schema=, @schema )
107
+ end
108
+
109
+ it "should load schema automatically" do
110
+ Person.send :schema=, nil
111
+ Person.should_receive( :remote_schema ).and_return @schema
112
+ Person.new.respond_to?( :age ).should be_true
113
+ end
114
+
115
+ it "should add missing attributes" do
116
+ Person.new.respond_to?( :age ).should be_true
117
+ end
118
+ end
119
+
92
120
  describe '.method_missing' do
93
121
 
94
122
  before do
@@ -179,4 +207,50 @@ describe ActiveResourceExtensions::ResourceWithSchema do
179
207
 
180
208
  end
181
209
 
210
+ describe '#update_attributes' do
211
+
212
+ before do
213
+ @schema = {
214
+ 'name' => 'String',
215
+ 'age' => 'Integer',
216
+ 'created_at' => 'Time',
217
+ 'birth_date' => 'Date',
218
+ 'is_musician' => 'Boolean',
219
+ 'height' => 'Float',
220
+ 'unknown_1' => 'Company',
221
+ 'unknown_2' => '',
222
+ 'company_id' => 'ObjectId'
223
+ }
224
+ Person.send( :schema=, @schema )
225
+
226
+ @person = Person.new :name => 'John'
227
+ end
228
+
229
+ it "should reject attributes unknown to schema" do
230
+ lambda do
231
+ @person.update_attributes :foobar => 3
232
+ end.should raise_error ArgumentError
233
+ end
234
+
235
+ it "should return result of #save" do
236
+ @person.should_receive( :save ).and_return true
237
+ @person.update_attributes({}).should be_true
238
+
239
+ @person.should_receive( :save ).and_return false
240
+ @person.update_attributes({}).should be_false
241
+ end
242
+
243
+ it "should update attributes" do
244
+ @person.should_receive( :save ).and_return true
245
+ @person.update_attributes :name => 'Robert'
246
+ @person.name.should == 'Robert'
247
+ end
248
+
249
+ it "should leave other attributes untouched" do
250
+ @person.should_receive( :save ).and_return true
251
+ @person.update_attributes :age => 19
252
+ @person.name.should == 'John'
253
+ end
254
+ end
255
+
182
256
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ares-ext
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 5
10
+ version: 0.0.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Benoit Dinocourt
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-06-11 00:00:00 +02:00
18
+ date: 2012-06-25 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency