api-model 2.3.1 → 2.4.0
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 +4 -4
- data/.travis.yml +0 -1
- data/Gemfile.lock +1 -1
- data/api-model.gemspec +1 -1
- data/lib/api_model/class_methods.rb +9 -0
- data/lib/api_model/instance_methods.rb +1 -9
- data/spec/api-model/api_model_spec.rb +5 -21
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1f815e2fe813c6ab61d66e7cd060c4684c38b41e
|
4
|
+
data.tar.gz: 75056c124a3c1bf7772216ddea09f824db7c1a50
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cbfbbff8f9dea0f02b804a75d91fb697abadfef39a44e0a9531b053677f0b046b766cc66f4d410074d9bde4fb1947035ddaa33fc21ba150ffdf8022ead0df860
|
7
|
+
data.tar.gz: 695d60e37fc49ec0861516cacf1aaee650e6aa5ab5f0c3e9faf2f20d65d1a46182b4808084c4b56709219150ea8cbaac0b287596e95fd0dc52ca5833b3b64bf5
|
data/.travis.yml
CHANGED
data/Gemfile.lock
CHANGED
data/api-model.gemspec
CHANGED
@@ -2,7 +2,7 @@ $:.push File.expand_path("../lib", __FILE__)
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = "api-model"
|
5
|
-
s.version = "2.
|
5
|
+
s.version = "2.4.0"
|
6
6
|
s.authors = ["Damien Timewell", "Erik Rothoff Andersson"]
|
7
7
|
s.email = ["mail@damientimewell.com", "erik.rothoff@gmail.com"]
|
8
8
|
s.licenses = ['MIT']
|
@@ -1,12 +1,21 @@
|
|
1
1
|
module ApiModel
|
2
2
|
module ClassMethods
|
3
3
|
|
4
|
+
def attribute_synonyms
|
5
|
+
@attribute_synonyms ||= {}
|
6
|
+
end
|
7
|
+
|
4
8
|
def attribute_synonym(primary_method_name, *alternate_names)
|
5
9
|
alternate_names.each do |alternate_name|
|
6
10
|
alias_method "#{alternate_name}=".to_sym, "#{primary_method_name}=".to_sym
|
11
|
+
attribute_synonyms[alternate_name] = primary_method_name
|
7
12
|
end
|
8
13
|
end
|
9
14
|
|
15
|
+
def real_attribute_name_for(name)
|
16
|
+
attribute_synonyms[name] || name
|
17
|
+
end
|
18
|
+
|
10
19
|
def get_json(path, params={}, options={})
|
11
20
|
call_api :get, path, options.merge(params: params)
|
12
21
|
end
|
@@ -43,7 +43,7 @@ module ApiModel
|
|
43
43
|
if obj.respond_to?(field.to_sym) && obj.send(field.to_sym).respond_to?(:set_error_on_self_or_child)
|
44
44
|
obj.send(field.to_sym).set_errors_from_hash messages
|
45
45
|
else
|
46
|
-
obj.errors.add field.to_sym, messages
|
46
|
+
obj.errors.add obj.class.real_attribute_name_for(field.to_sym), messages
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
@@ -83,13 +83,5 @@ module ApiModel
|
|
83
83
|
end
|
84
84
|
end
|
85
85
|
|
86
|
-
# Returns all the defined attributes in a hash without the :persisted attribute which was added automatically.
|
87
|
-
#
|
88
|
-
# This is useful for when you need to pass the entire object back to an API, or if you want to serialize the object.
|
89
|
-
def properties_hash
|
90
|
-
ActiveSupport::Deprecation.warn "properties_hash() is deprecated and may be removed from future releases, use as_json() instead.", caller
|
91
|
-
self.as_json.with_indifferent_access
|
92
|
-
end
|
93
|
-
|
94
86
|
end
|
95
87
|
end
|
@@ -163,6 +163,11 @@ describe ApiModel do
|
|
163
163
|
it 'should return false if errors is not a hash' do
|
164
164
|
car.set_errors_from_hash("Foobar").should be_false
|
165
165
|
end
|
166
|
+
|
167
|
+
it 'should set errors on the aliased method when you refer to the synonym' do
|
168
|
+
car.set_errors_from_hash numberOfDoors: "must be at least 20"
|
169
|
+
car.errors[:number_of_doors].should eq ["must be at least 20"]
|
170
|
+
end
|
166
171
|
end
|
167
172
|
|
168
173
|
describe "setting errors on nested models" do
|
@@ -345,25 +350,4 @@ describe ApiModel do
|
|
345
350
|
end
|
346
351
|
end
|
347
352
|
|
348
|
-
describe "properties_hash" do
|
349
|
-
let(:blog_post) { BlogPost.new title: "Foo", name: "Bar", something_else: "Baz" }
|
350
|
-
|
351
|
-
it 'should return a hash' do
|
352
|
-
blog_post.properties_hash.should be_a(Hash)
|
353
|
-
end
|
354
|
-
|
355
|
-
it 'should include attributes which are defined as properties' do
|
356
|
-
blog_post.properties_hash.should have_key(:title)
|
357
|
-
blog_post.properties_hash.should have_key(:name)
|
358
|
-
end
|
359
|
-
|
360
|
-
it 'should not include attributes which are not defined as properties' do
|
361
|
-
blog_post.properties_hash.should_not have_key(:something_else)
|
362
|
-
end
|
363
|
-
|
364
|
-
it 'should not include the :persisted attribute, even though it is defined' do
|
365
|
-
blog_post.properties_hash.should_not have_key(:persisted)
|
366
|
-
end
|
367
|
-
end
|
368
|
-
|
369
353
|
end
|