rest_model 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +16 -2
- data/examples/all.rb +4 -0
- data/examples/embeds_many/invisible.rb +1 -1
- data/examples/embeds_many/simple.rb +1 -1
- data/examples/embeds_many/with_class_name.rb +1 -1
- data/examples/embeds_many/with_fields.rb +1 -1
- data/examples/embeds_many/with_if.rb +2 -2
- data/examples/embeds_many/with_start_key.rb +1 -2
- data/examples/embeds_one/flattened.rb +1 -1
- data/examples/embeds_one/simple.rb +1 -1
- data/examples/embeds_one/with_class_name.rb +1 -1
- data/examples/embeds_one/with_if.rb +2 -2
- data/examples/embeds_one/with_start_key.rb +1 -1
- data/examples/has_many/simple.rb +1 -1
- data/examples/properties/array_serialization.rb +1 -1
- data/examples/properties/collections.rb +3 -3
- data/examples/properties/simple.rb +1 -1
- data/examples/properties/with_field.rb +1 -1
- data/examples/properties/with_field_path.rb +1 -1
- data/examples/properties/with_id.rb +1 -1
- data/examples/properties/with_if.rb +2 -2
- data/examples/properties/with_key_converter.rb +1 -1
- data/examples/properties/with_two_key_converters.rb +1 -1
- data/examples/properties/with_values.rb +1 -1
- data/examples/summarization/simple.rb +6 -7
- data/examples/to_source/embeds_many.rb +24 -0
- data/examples/to_source/embeds_many_without_key.rb +29 -0
- data/examples/to_source/embeds_one_without_key.rb +21 -0
- data/examples/{to_input → to_source}/serializables.rb +7 -1
- data/examples/{to_input → to_source}/without_key.rb +1 -1
- data/examples/{to_input → to_source}/without_nil.rb +1 -1
- data/examples/update_attributes/embeds_many.rb +2 -2
- data/examples/update_attributes/embeds_one.rb +2 -2
- data/examples/update_attributes/has_many.rb +2 -2
- data/examples/update_attributes/has_one.rb +2 -2
- data/examples/update_attributes/simple.rb +1 -1
- data/lib/rest_model/key/relation.rb +4 -1
- data/lib/rest_model/version.rb +1 -1
- data/spec/integration/embeds_many_spec.rb +14 -14
- data/spec/integration/embeds_one_spec.rb +13 -7
- data/spec/integration/has_many_spec.rb +3 -3
- data/spec/integration/property_spec.rb +27 -5
- data/spec/integration/summarization_spec.rb +1 -1
- data/spec/integration/to_source_spec.rb +37 -0
- data/spec/integration/update_attributes_spec.rb +10 -10
- data/spec/support/examples.rb +16 -18
- metadata +28 -26
- data/examples/to_input/embeds_many.rb +0 -25
- data/examples/to_input/embeds_many_without_key.rb +0 -31
- data/examples/to_input/embeds_one_without_key.rb +0 -23
- /data/examples/{to_input → to_source}/embeds_one.rb +0 -0
- /data/examples/{to_input → to_source}/flattened.rb +0 -0
- /data/examples/{to_input → to_source}/simple.rb +0 -0
- /data/examples/{to_input → to_source}/with_field.rb +0 -0
- /data/examples/{to_input → to_source}/with_field_path.rb +0 -0
- /data/examples/{to_input → to_source}/with_fields.rb +0 -0
data/README.md
CHANGED
@@ -15,8 +15,22 @@
|
|
15
15
|
|| ||
|
16
16
|
\/ ||
|
17
17
|
|
18
|
-
|
19
|
-
|
18
|
+
SOURCE HASH
|
19
|
+
|
20
|
+
.
|
21
|
+
|
22
|
+
class Account < RestModel
|
23
|
+
property :number
|
24
|
+
property :balance, type: Float
|
25
|
+
property :type, values: {gold: '01', platinum: '02'}
|
26
|
+
|
27
|
+
embeds_one :electronic_card
|
28
|
+
embeds_many :transactions
|
29
|
+
|
30
|
+
belongs_to :customer
|
31
|
+
has_one :manager
|
32
|
+
has_many :tickets
|
33
|
+
end
|
20
34
|
|
21
35
|
### Map _from a data source_ to instances of your model class.
|
22
36
|
|
data/examples/all.rb
CHANGED
@@ -7,6 +7,10 @@ Dir["examples/*/**.rb"].each do |file|
|
|
7
7
|
Object.send(:remove_const, klass) if Object.const_defined? klass
|
8
8
|
end
|
9
9
|
|
10
|
+
RestModel::Configuration.configure do |c|
|
11
|
+
c.convert_input_keys = RestModel::Configuration::DefaultHandler
|
12
|
+
end
|
13
|
+
|
10
14
|
puts "\n\nexample file: #{file}\n\n"
|
11
15
|
load file
|
12
16
|
end
|
@@ -11,6 +11,6 @@ end
|
|
11
11
|
|
12
12
|
names = {'en' => 'Woot', 'pt-BR' => 'Úia', 'es' => 'Me gusta'}
|
13
13
|
|
14
|
-
@root = Root.from_source(locale: 'pt-BR', key: 19190839, names: names).first
|
14
|
+
@root = Root.from_source!(locale: 'pt-BR', key: 19190839, names: names).first
|
15
15
|
|
16
16
|
inspect_rest_model(@root)
|
@@ -9,8 +9,8 @@ class Root < RestModel
|
|
9
9
|
embeds_many :items, if: proc {id == "10"}
|
10
10
|
end
|
11
11
|
|
12
|
-
@root_with_items = Root.
|
12
|
+
@root_with_items = Root.from_source!(id: 10, items: [{id: 2000}]).first
|
13
13
|
inspect_rest_model(@root_with_items)
|
14
14
|
|
15
|
-
@root_without_items = Root.
|
15
|
+
@root_without_items = Root.from_source!(id: 1, items: [{id: 2000}]).first
|
16
16
|
inspect_rest_model(@root_without_items)
|
@@ -9,8 +9,8 @@ class Item < RestModel
|
|
9
9
|
property :id
|
10
10
|
end
|
11
11
|
|
12
|
-
@root_with_item = Root.
|
12
|
+
@root_with_item = Root.from_source!(id: 1, item: {id: 2000}).first
|
13
13
|
inspect_rest_model(@root_with_item)
|
14
14
|
|
15
|
-
@root_without_item = Root.
|
15
|
+
@root_without_item = Root.from_source!(id: 100, item: {id: 2000}).first
|
16
16
|
inspect_rest_model(@root_without_item)
|
data/examples/has_many/simple.rb
CHANGED
@@ -23,6 +23,6 @@ class Customer < RestModel
|
|
23
23
|
has_one :devops, class_name: :developer
|
24
24
|
end
|
25
25
|
|
26
|
-
@root = Customer.
|
26
|
+
@root = Customer.from_source!({customer_id: 123, login: 'jackiechan2010'}).first
|
27
27
|
|
28
28
|
inspect_rest_model(@root)
|
@@ -4,8 +4,8 @@ class Customer < RestModel
|
|
4
4
|
property :login
|
5
5
|
end
|
6
6
|
|
7
|
-
@customers = Customer.
|
8
|
-
|
9
|
-
|
7
|
+
@customers = Customer.from_source!([{login: "jackiechan2010"},
|
8
|
+
{login: "brucelee"},
|
9
|
+
{login: "vcr2"}])
|
10
10
|
|
11
11
|
@customers.map(&method(:inspect_rest_model))
|
@@ -6,14 +6,14 @@ class Customer < RestModel
|
|
6
6
|
property :description, if: proc {password == "abc"}
|
7
7
|
end
|
8
8
|
|
9
|
-
@root_with_description = Customer.
|
9
|
+
@root_with_description = Customer.from_source!({
|
10
10
|
login: 2000,
|
11
11
|
password: "abc",
|
12
12
|
description: "description"}).first
|
13
13
|
|
14
14
|
inspect_rest_model(@root_with_description)
|
15
15
|
|
16
|
-
@root_without_description = Customer.
|
16
|
+
@root_without_description = Customer.from_source!({
|
17
17
|
login: 2000,
|
18
18
|
password: "abcd",
|
19
19
|
description: "some text II"}).first
|
@@ -23,5 +23,5 @@ class Product < RestModel
|
|
23
23
|
property :unit_price, type: Float
|
24
24
|
end
|
25
25
|
|
26
|
-
@root = Customer.
|
26
|
+
@root = Customer.from_source!("LOGIN" => 'jackiechan2010', "PRODUCT" => {"UnitPrice" => 29.9}).first
|
27
27
|
inspect_rest_model(@root)
|
@@ -6,5 +6,5 @@ class Customer < RestModel
|
|
6
6
|
property :type, values: {simple: "1", complex: "2"}
|
7
7
|
end
|
8
8
|
|
9
|
-
@root = Customer.
|
9
|
+
@root = Customer.from_source!(periodicity: 'c_53', company: "GOOG", type: 2).first
|
10
10
|
inspect_rest_model(@root)
|
@@ -9,13 +9,12 @@ class Customer < RestModel
|
|
9
9
|
end
|
10
10
|
|
11
11
|
input = {
|
12
|
-
"id"
|
13
|
-
"login"
|
14
|
-
"name"
|
15
|
-
"postal_code"
|
16
|
-
"email"
|
12
|
+
"id" => 138911938,
|
13
|
+
"login" => 'jackiechan2010',
|
14
|
+
"name" => 'jackie chan',
|
15
|
+
"postal_code" => '05492-092',
|
16
|
+
"email" => 'jachan@gmail.com',
|
17
17
|
"secondary_email" => 'jackiepeligroso@yahoo.com'
|
18
18
|
}
|
19
19
|
|
20
|
-
@root = Customer.resources Customer.
|
21
|
-
puts "transcribed: #{@root}"
|
20
|
+
@root = Customer.resources Customer.from_source!(input)
|
@@ -0,0 +1,24 @@
|
|
1
|
+
$:.push 'examples'; require 'helper'
|
2
|
+
|
3
|
+
class Customer < RestModel
|
4
|
+
property :login, field: 'hidden.login'
|
5
|
+
embeds_many :phones
|
6
|
+
end
|
7
|
+
|
8
|
+
class Phone < RestModel
|
9
|
+
properties :number, :extension
|
10
|
+
end
|
11
|
+
|
12
|
+
@root = Customer.new(login: 'jackiechan2010',
|
13
|
+
phones: [
|
14
|
+
{
|
15
|
+
number: "123123123",
|
16
|
+
extension: "111"
|
17
|
+
},
|
18
|
+
{
|
19
|
+
number: "098098098",
|
20
|
+
extension: "999"
|
21
|
+
}
|
22
|
+
])
|
23
|
+
|
24
|
+
inspect_rest_model(@root)
|
@@ -0,0 +1,29 @@
|
|
1
|
+
$:.push 'examples'; require 'helper'
|
2
|
+
|
3
|
+
class Customer < RestModel
|
4
|
+
properties :login, :name
|
5
|
+
embeds_many :phones
|
6
|
+
end
|
7
|
+
|
8
|
+
class Phone < RestModel
|
9
|
+
properties :number, :extension, :description
|
10
|
+
end
|
11
|
+
|
12
|
+
@root = Customer.new(login: 'jackiechan2010',
|
13
|
+
name: "Jackie Chan",
|
14
|
+
phones: [
|
15
|
+
{
|
16
|
+
number: "897289472",
|
17
|
+
extension: "3897",
|
18
|
+
description: "abab"
|
19
|
+
},
|
20
|
+
{
|
21
|
+
number: "987198732",
|
22
|
+
extension: "1897",
|
23
|
+
description: "eheh"
|
24
|
+
}
|
25
|
+
])
|
26
|
+
|
27
|
+
inspect_rest_model(@root)
|
28
|
+
|
29
|
+
puts "to input without description: #{@root.to_source(phones: {without: :description})}"
|
@@ -0,0 +1,21 @@
|
|
1
|
+
$:.push 'examples'; require 'helper'
|
2
|
+
|
3
|
+
class Customer < RestModel
|
4
|
+
properties :login, :name
|
5
|
+
embeds_one :address
|
6
|
+
end
|
7
|
+
|
8
|
+
class Address < RestModel
|
9
|
+
properties :street, :number, :hint
|
10
|
+
end
|
11
|
+
|
12
|
+
@root = Customer.new(login: 'jackiechan2010',
|
13
|
+
name: "Jackie Chan",
|
14
|
+
address: {
|
15
|
+
street: "Aurora St",
|
16
|
+
number: 1833,
|
17
|
+
hint: "near gas station"
|
18
|
+
})
|
19
|
+
|
20
|
+
inspect_rest_model(@root)
|
21
|
+
puts "root.to_source without hint: #{@root.to_source(address: {without: :hint})}"
|
@@ -15,5 +15,11 @@ class Customer < RestModel
|
|
15
15
|
property :balance, type: Float
|
16
16
|
end
|
17
17
|
|
18
|
-
@root = Customer.new(
|
18
|
+
@root = Customer.new(login: 'jackiechan2010',
|
19
|
+
age: 22,
|
20
|
+
birth: "1990-07-04",
|
21
|
+
active: true,
|
22
|
+
services: ["Hosting", "Email"],
|
23
|
+
balance: 200.00)
|
24
|
+
|
19
25
|
inspect_rest_model(@root)
|
@@ -8,7 +8,7 @@ class Root < RestModel
|
|
8
8
|
embeds_many :items
|
9
9
|
end
|
10
10
|
|
11
|
-
@root = Root.
|
12
|
-
@root.update_attributes(
|
11
|
+
@root = Root.from_source!(items: [{id: 2000}, {id: 2001}]).first
|
12
|
+
@root.update_attributes(items: [{id: 3000}])
|
13
13
|
|
14
14
|
inspect_rest_model(@root)
|
@@ -8,7 +8,7 @@ class Item < RestModel
|
|
8
8
|
properties :id, :name
|
9
9
|
end
|
10
10
|
|
11
|
-
@root = Root.
|
12
|
-
@root.update_attributes(
|
11
|
+
@root = Root.from_source!(item: {id: 2000}).first
|
12
|
+
@root.update_attributes(item: {name: "name"})
|
13
13
|
|
14
14
|
inspect_rest_model(@root)
|
@@ -11,7 +11,7 @@ class Customer < RestModel
|
|
11
11
|
has_many :services
|
12
12
|
end
|
13
13
|
|
14
|
-
@root = Customer.
|
15
|
-
@root.update_attributes(
|
14
|
+
@root = Customer.from_source!(customer_id: 123, login: 'jackiechan2010').first
|
15
|
+
@root.update_attributes(services: [{name: "new_service_name"}])
|
16
16
|
|
17
17
|
inspect_rest_model(@root)
|
@@ -11,7 +11,7 @@ class Customer < RestModel
|
|
11
11
|
has_one :billing
|
12
12
|
end
|
13
13
|
|
14
|
-
@root = Customer.
|
15
|
-
@root.update_attributes(
|
14
|
+
@root = Customer.from_source!(customer_id: 123, login: 'jackiechan2010').first
|
15
|
+
@root.update_attributes(billing: {login: "new_billing_login"})
|
16
16
|
|
17
17
|
inspect_rest_model(@root)
|
data/lib/rest_model/version.rb
CHANGED
@@ -5,41 +5,41 @@ require "spec_helper"
|
|
5
5
|
describe "embeds_many" do
|
6
6
|
describe_example "embeds_many/simple" do
|
7
7
|
it "parses first embedded item" do
|
8
|
-
root.items[0].id.should == 2000
|
8
|
+
@root.items[0].id.should == 2000
|
9
9
|
end
|
10
10
|
|
11
11
|
it "parses second embedded item" do
|
12
|
-
root.items[1].id.should == 2001
|
12
|
+
@root.items[1].id.should == 2001
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
16
|
describe_example "embeds_many/invisible" do
|
17
17
|
it "parses invisible items correctly" do
|
18
|
-
root.locale.should == "pt-BR"
|
19
|
-
root.names.should == {"en"=>"Woot", "pt-BR"=>"Úia", "es"=>"Me gusta"}
|
18
|
+
@root.locale.should == "pt-BR"
|
19
|
+
@root.names.should == {"en"=>"Woot", "pt-BR"=>"Úia", "es"=>"Me gusta"}
|
20
20
|
end
|
21
21
|
|
22
22
|
it "doesn't show embedded items in resource" do
|
23
|
-
root.resource.should_not have_key("locale")
|
24
|
-
root.resource.should_not have_key("names")
|
23
|
+
@root.resource.should_not have_key("locale")
|
24
|
+
@root.resource.should_not have_key("names")
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
28
|
describe_example "embeds_many/with_array" do
|
29
29
|
it "parses items" do
|
30
|
-
root.items.should == ["a", "b"]
|
30
|
+
@root.items.should == ["a", "b"]
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
34
|
describe_example "embeds_many/with_class_name" do
|
35
35
|
it "parses embedded item" do
|
36
|
-
root.items.first.id.should == 2000
|
36
|
+
@root.items.first.id.should == 2000
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
40
40
|
describe_example "embeds_many/with_fields" do
|
41
41
|
it "parses fields" do
|
42
|
-
root.items.should == ["i1", "i2", "i3"]
|
42
|
+
@root.items.should == ["i1", "i2", "i3"]
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
@@ -47,13 +47,13 @@ describe "embeds_many" do
|
|
47
47
|
context "when an embeddable has a conditional proc (:if)" do
|
48
48
|
context "and it evaluates to true" do
|
49
49
|
it "parses embedded item" do
|
50
|
-
root_with_items.items.first.id.should == "2000"
|
50
|
+
@root_with_items.items.first.id.should == "2000"
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
54
54
|
context "and it evaluates to false" do
|
55
55
|
it "doesn't parse embedded item" do
|
56
|
-
root_without_items.items.should_not be
|
56
|
+
@root_without_items.items.should_not be
|
57
57
|
end
|
58
58
|
end
|
59
59
|
end
|
@@ -61,17 +61,17 @@ describe "embeds_many" do
|
|
61
61
|
|
62
62
|
describe_example "embeds_many/with_nil_value" do
|
63
63
|
it "has items nil" do
|
64
|
-
root.items.should_not be
|
64
|
+
@root.items.should_not be
|
65
65
|
end
|
66
66
|
|
67
67
|
it "returns an empty items list" do
|
68
|
-
root.resource[:items].should == []
|
68
|
+
@root.resource[:items].should == []
|
69
69
|
end
|
70
70
|
end
|
71
71
|
|
72
72
|
describe_example "embeds_many/with_start_key" do
|
73
73
|
it "parses embedded items" do
|
74
|
-
root.items.first.id.should == "2000"
|
74
|
+
@root.items.first.id.should == "2000"
|
75
75
|
end
|
76
76
|
end
|
77
77
|
end
|
@@ -3,14 +3,20 @@ require 'spec_helper'
|
|
3
3
|
describe "embeds_one" do
|
4
4
|
describe_example "embeds_one/simple" do
|
5
5
|
it 'parses embedded item' do
|
6
|
-
root.item.id.should == "2000"
|
6
|
+
@root.item.id.should == "2000"
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
describe_example "embeds_one/flattened" do
|
11
|
+
it "parses flattened key" do
|
12
|
+
@root.item.id.should == "2000"
|
7
13
|
end
|
8
14
|
end
|
9
15
|
|
10
16
|
describe_example "embeds_one/with_class_name" do
|
11
17
|
context 'when a different class name is used for embeddable' do
|
12
18
|
it 'parses embedded item' do
|
13
|
-
root.item.id.should == "2000"
|
19
|
+
@root.item.id.should == "2000"
|
14
20
|
end
|
15
21
|
end
|
16
22
|
end
|
@@ -19,15 +25,15 @@ describe "embeds_one" do
|
|
19
25
|
context 'when an embeddable has a conditional proc (:if)' do
|
20
26
|
context 'and it evaluates to true' do
|
21
27
|
it 'parses embedded item' do
|
22
|
-
root_with_item.id.should == "1"
|
23
|
-
root_with_item.item.id.should == "2000"
|
28
|
+
@root_with_item.id.should == "1"
|
29
|
+
@root_with_item.item.id.should == "2000"
|
24
30
|
end
|
25
31
|
end
|
26
32
|
|
27
33
|
context 'and it evaluates to false' do
|
28
34
|
it "doesn't parse embedded item" do
|
29
|
-
root_without_item.id.should == "100"
|
30
|
-
root_without_item.item.should_not be
|
35
|
+
@root_without_item.id.should == "100"
|
36
|
+
@root_without_item.item.should_not be
|
31
37
|
end
|
32
38
|
end
|
33
39
|
end
|
@@ -36,7 +42,7 @@ describe "embeds_one" do
|
|
36
42
|
describe_example "embeds_one/with_start_key" do
|
37
43
|
context 'when there is a start key to parse input' do
|
38
44
|
it 'parses embedded item' do
|
39
|
-
root.item.id.should == "2000"
|
45
|
+
@root.item.id.should == "2000"
|
40
46
|
end
|
41
47
|
end
|
42
48
|
end
|
@@ -3,12 +3,12 @@ require 'spec_helper'
|
|
3
3
|
describe "has_many" do
|
4
4
|
describe_example "has_many/simple" do
|
5
5
|
it 'parses properly' do
|
6
|
-
root.id.should == 123
|
7
|
-
root.login.should == 'jackiechan2010'
|
6
|
+
@root.id.should == 123
|
7
|
+
@root.login.should == 'jackiechan2010'
|
8
8
|
end
|
9
9
|
|
10
10
|
describe "#resource" do
|
11
|
-
subject {root.resource}
|
11
|
+
subject {@root.resource}
|
12
12
|
|
13
13
|
it 'has a link' do
|
14
14
|
subject[:link].should be_an(Array)
|
@@ -2,11 +2,25 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
def it_parses_property
|
4
4
|
it 'parses property' do
|
5
|
-
root.login.should == 'jackiechan2010'
|
5
|
+
@root.login.should == 'jackiechan2010'
|
6
6
|
end
|
7
7
|
end
|
8
8
|
|
9
9
|
describe "properties" do
|
10
|
+
describe_example "properties/array_serialization" do
|
11
|
+
it "parses property" do
|
12
|
+
@root.products.should == ["a", "b", "c"]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe_example "properties/collections" do
|
17
|
+
it "parses customers" do
|
18
|
+
@customers[0].login.should == "jackiechan2010"
|
19
|
+
@customers[1].login.should == "brucelee"
|
20
|
+
@customers[2].login.should == "vcr2"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
10
24
|
describe_example 'properties/simple' do
|
11
25
|
it_parses_property
|
12
26
|
end
|
@@ -26,7 +40,7 @@ describe "properties" do
|
|
26
40
|
describe_example 'properties/with_id' do
|
27
41
|
context 'when property is an id' do
|
28
42
|
it 'parses id' do
|
29
|
-
root.id.should == "2000"
|
43
|
+
@root.id.should == "2000"
|
30
44
|
end
|
31
45
|
end
|
32
46
|
end
|
@@ -35,13 +49,13 @@ describe "properties" do
|
|
35
49
|
context 'when a property has a conditional proc (:if)' do
|
36
50
|
context 'and it evaluates to true' do
|
37
51
|
it 'parses property' do
|
38
|
-
root_with_description.description.should == "description"
|
52
|
+
@root_with_description.description.should == "description"
|
39
53
|
end
|
40
54
|
end
|
41
55
|
|
42
56
|
context 'and it evaluates to false' do
|
43
57
|
it "doesn't parse property" do
|
44
|
-
root_without_description.description.should_not be
|
58
|
+
@root_without_description.description.should_not be
|
45
59
|
end
|
46
60
|
end
|
47
61
|
end
|
@@ -58,8 +72,16 @@ describe "properties" do
|
|
58
72
|
it_parses_property
|
59
73
|
|
60
74
|
it 'parses other property with different key converter' do
|
61
|
-
root.product.unit_price.should == 29.9
|
75
|
+
@root.product.unit_price.should == 29.9
|
62
76
|
end
|
63
77
|
end
|
64
78
|
end
|
79
|
+
|
80
|
+
describe_example "properties/with_values" do
|
81
|
+
it "maps values" do
|
82
|
+
@root.periodicity.should == 1
|
83
|
+
@root.company.should == "google"
|
84
|
+
@root.type.should == :complex
|
85
|
+
end
|
86
|
+
end
|
65
87
|
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
describe "to_source" do
|
6
|
+
describe_example "to_source/simple" do
|
7
|
+
it "generates source" do
|
8
|
+
@root.to_source[:login].should == "jackiechan2010"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe_example "to_source/embeds_many" do
|
13
|
+
it "generates source" do
|
14
|
+
source = @root.to_source
|
15
|
+
source['hidden']['login'].should == "jackiechan2010"
|
16
|
+
source["phones"][0].should == {"number" => "123123123", "extension" => "111"}
|
17
|
+
source["phones"][1].should == {"number" => "098098098", "extension" => "999"}
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe_example "to_source/embeds_many_without_key" do
|
22
|
+
it "generates source without a key" do
|
23
|
+
source = @root.to_source(phones: {without: :description})
|
24
|
+
source["phones"][0].should_not have_key("description")
|
25
|
+
source["phones"][1].should_not have_key("description")
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe_example "to_source/embeds_one" do
|
30
|
+
it "generates source" do
|
31
|
+
source = @root.to_source
|
32
|
+
source['hidden']['login'].should == "jackiechan2010"
|
33
|
+
source["address"]["street"].should == "Aurora St"
|
34
|
+
source["address"]["number"].should == "666"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -5,36 +5,36 @@ require "spec_helper"
|
|
5
5
|
describe "update_attributes" do
|
6
6
|
describe_example "update_attributes/embeds_one" do
|
7
7
|
it "updates customer item name" do
|
8
|
-
root.item.id.should == "2000"
|
9
|
-
root.item.name.should == "name"
|
8
|
+
@root.item.id.should == "2000"
|
9
|
+
@root.item.name.should == "name"
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
13
|
describe_example "update_attributes/simple" do
|
14
14
|
it "updates customer login" do
|
15
|
-
root.login.should == "newjackiechan2010"
|
15
|
+
@root.login.should == "newjackiechan2010"
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
19
|
describe_example "update_attributes/embeds_many" do
|
20
20
|
it "updates customer items" do
|
21
|
-
root.items.count.should == 1
|
22
|
-
root.items.first.id.should == 3000
|
21
|
+
@root.items.count.should == 1
|
22
|
+
@root.items.first.id.should == 3000
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
26
|
describe_example "update_attributes/has_one" do
|
27
27
|
it "updates customer billing" do
|
28
|
-
root.billing.should_not be_nil
|
29
|
-
root.billing.login.should == "new_billing_login"
|
28
|
+
@root.billing.should_not be_nil
|
29
|
+
@root.billing.login.should == "new_billing_login"
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
33
|
describe_example "update_attributes/has_many" do
|
34
34
|
it "updates customer services" do
|
35
|
-
root.services.should_not be_nil
|
36
|
-
root.services.count.should == 1
|
37
|
-
root.services.first.name.should == "new_service_name"
|
35
|
+
@root.services.should_not be_nil
|
36
|
+
@root.services.count.should == 1
|
37
|
+
@root.services.first.name.should == "new_service_name"
|
38
38
|
end
|
39
39
|
end
|
40
40
|
end
|
data/spec/support/examples.rb
CHANGED
@@ -1,26 +1,24 @@
|
|
1
1
|
module Examples
|
2
|
-
|
3
|
-
describe "example #{file}", tags do
|
4
|
-
[:Root, :Item, :Customer, :Entry].each do |klass|
|
5
|
-
Examples.send(:remove_const, klass) if Examples.const_defined?(klass)
|
6
|
-
end
|
7
|
-
|
8
|
-
RestModel::Configuration.configure do |c|
|
9
|
-
c.convert_input_keys = RestModel::Configuration::DefaultHandler
|
10
|
-
end
|
2
|
+
extend RSpec::Core::Hooks
|
11
3
|
|
12
|
-
|
4
|
+
CONSTANTS = [:Root, :Item, :Customer, :Entry, :Phone,
|
5
|
+
:Service, :Billing, :Developer, :Upcasing,
|
6
|
+
:Camelizing, :Product, :Address]
|
13
7
|
|
14
|
-
|
8
|
+
def describe_example(file, tags = {}, &block)
|
9
|
+
describe "example #{file}", tags do
|
10
|
+
before :all do
|
11
|
+
CONSTANTS.each do |klass|
|
12
|
+
self.class.ancestors.concat([self.class, Examples]).each do |mod|
|
13
|
+
mod.send(:remove_const, klass) if mod.const_defined?(klass, false)
|
14
|
+
end
|
15
|
+
end
|
15
16
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
buffer.merge method_name => instance_variable_get(name)
|
20
|
-
end
|
17
|
+
RestModel::Configuration.configure do |c|
|
18
|
+
c.convert_input_keys = RestModel::Configuration::DefaultHandler
|
19
|
+
end
|
21
20
|
|
22
|
-
|
23
|
-
values.each {|name, value| instance_variable_set "@#{name}", value}
|
21
|
+
silently {eval File.read("examples/#{file}.rb")}
|
24
22
|
end
|
25
23
|
|
26
24
|
instance_eval &block if block
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rest_model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -11,11 +11,11 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2011-10-
|
14
|
+
date: 2011-10-18 00:00:00.000000000Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: activesupport
|
18
|
-
requirement: &
|
18
|
+
requirement: &70262930498480 !ruby/object:Gem::Requirement
|
19
19
|
none: false
|
20
20
|
requirements:
|
21
21
|
- - ~>
|
@@ -23,10 +23,10 @@ dependencies:
|
|
23
23
|
version: '3.0'
|
24
24
|
type: :runtime
|
25
25
|
prerelease: false
|
26
|
-
version_requirements: *
|
26
|
+
version_requirements: *70262930498480
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: i18n
|
29
|
-
requirement: &
|
29
|
+
requirement: &70262930497940 !ruby/object:Gem::Requirement
|
30
30
|
none: false
|
31
31
|
requirements:
|
32
32
|
- - ! '>='
|
@@ -34,10 +34,10 @@ dependencies:
|
|
34
34
|
version: '0.5'
|
35
35
|
type: :runtime
|
36
36
|
prerelease: false
|
37
|
-
version_requirements: *
|
37
|
+
version_requirements: *70262930497940
|
38
38
|
- !ruby/object:Gem::Dependency
|
39
39
|
name: rspec
|
40
|
-
requirement: &
|
40
|
+
requirement: &70262930497340 !ruby/object:Gem::Requirement
|
41
41
|
none: false
|
42
42
|
requirements:
|
43
43
|
- - ~>
|
@@ -45,10 +45,10 @@ dependencies:
|
|
45
45
|
version: '2.6'
|
46
46
|
type: :development
|
47
47
|
prerelease: false
|
48
|
-
version_requirements: *
|
48
|
+
version_requirements: *70262930497340
|
49
49
|
- !ruby/object:Gem::Dependency
|
50
50
|
name: guard
|
51
|
-
requirement: &
|
51
|
+
requirement: &70262930496860 !ruby/object:Gem::Requirement
|
52
52
|
none: false
|
53
53
|
requirements:
|
54
54
|
- - ~>
|
@@ -56,10 +56,10 @@ dependencies:
|
|
56
56
|
version: '0.5'
|
57
57
|
type: :development
|
58
58
|
prerelease: false
|
59
|
-
version_requirements: *
|
59
|
+
version_requirements: *70262930496860
|
60
60
|
- !ruby/object:Gem::Dependency
|
61
61
|
name: guard-rspec
|
62
|
-
requirement: &
|
62
|
+
requirement: &70262930496260 !ruby/object:Gem::Requirement
|
63
63
|
none: false
|
64
64
|
requirements:
|
65
65
|
- - ~>
|
@@ -67,10 +67,10 @@ dependencies:
|
|
67
67
|
version: '0.4'
|
68
68
|
type: :development
|
69
69
|
prerelease: false
|
70
|
-
version_requirements: *
|
70
|
+
version_requirements: *70262930496260
|
71
71
|
- !ruby/object:Gem::Dependency
|
72
72
|
name: growl
|
73
|
-
requirement: &
|
73
|
+
requirement: &70262930495740 !ruby/object:Gem::Requirement
|
74
74
|
none: false
|
75
75
|
requirements:
|
76
76
|
- - ~>
|
@@ -78,7 +78,7 @@ dependencies:
|
|
78
78
|
version: '1.0'
|
79
79
|
type: :development
|
80
80
|
prerelease: false
|
81
|
-
version_requirements: *
|
81
|
+
version_requirements: *70262930495740
|
82
82
|
description: ''
|
83
83
|
email:
|
84
84
|
- victorcrodrigues@gmail.com
|
@@ -124,18 +124,18 @@ files:
|
|
124
124
|
- examples/properties/with_two_key_converters.rb
|
125
125
|
- examples/properties/with_values.rb
|
126
126
|
- examples/summarization/simple.rb
|
127
|
-
- examples/
|
128
|
-
- examples/
|
129
|
-
- examples/
|
130
|
-
- examples/
|
131
|
-
- examples/
|
132
|
-
- examples/
|
133
|
-
- examples/
|
134
|
-
- examples/
|
135
|
-
- examples/
|
136
|
-
- examples/
|
137
|
-
- examples/
|
138
|
-
- examples/
|
127
|
+
- examples/to_source/embeds_many.rb
|
128
|
+
- examples/to_source/embeds_many_without_key.rb
|
129
|
+
- examples/to_source/embeds_one.rb
|
130
|
+
- examples/to_source/embeds_one_without_key.rb
|
131
|
+
- examples/to_source/flattened.rb
|
132
|
+
- examples/to_source/serializables.rb
|
133
|
+
- examples/to_source/simple.rb
|
134
|
+
- examples/to_source/with_field.rb
|
135
|
+
- examples/to_source/with_field_path.rb
|
136
|
+
- examples/to_source/with_fields.rb
|
137
|
+
- examples/to_source/without_key.rb
|
138
|
+
- examples/to_source/without_nil.rb
|
139
139
|
- examples/update_attributes/embeds_many.rb
|
140
140
|
- examples/update_attributes/embeds_one.rb
|
141
141
|
- examples/update_attributes/has_many.rb
|
@@ -179,6 +179,7 @@ files:
|
|
179
179
|
- spec/integration/has_many_spec.rb
|
180
180
|
- spec/integration/property_spec.rb
|
181
181
|
- spec/integration/summarization_spec.rb
|
182
|
+
- spec/integration/to_source_spec.rb
|
182
183
|
- spec/integration/update_attributes_spec.rb
|
183
184
|
- spec/spec_helper.rb
|
184
185
|
- spec/support/examples.rb
|
@@ -237,6 +238,7 @@ test_files:
|
|
237
238
|
- spec/integration/has_many_spec.rb
|
238
239
|
- spec/integration/property_spec.rb
|
239
240
|
- spec/integration/summarization_spec.rb
|
241
|
+
- spec/integration/to_source_spec.rb
|
240
242
|
- spec/integration/update_attributes_spec.rb
|
241
243
|
- spec/spec_helper.rb
|
242
244
|
- spec/support/examples.rb
|
@@ -1,25 +0,0 @@
|
|
1
|
-
$:.push 'examples'; require 'helper'
|
2
|
-
|
3
|
-
class Customer < RestModel
|
4
|
-
property :login, field: 'hidden.login'
|
5
|
-
embeds_many :phones
|
6
|
-
end
|
7
|
-
|
8
|
-
class Phone < RestModel
|
9
|
-
properties :number, :extension
|
10
|
-
end
|
11
|
-
|
12
|
-
@root = Customer.new({
|
13
|
-
login: 'jackiechan2010',
|
14
|
-
phones: [
|
15
|
-
{
|
16
|
-
number: "123123123",
|
17
|
-
extension: "111"
|
18
|
-
},
|
19
|
-
{
|
20
|
-
number: "098098098",
|
21
|
-
extension: "999"
|
22
|
-
}
|
23
|
-
]})
|
24
|
-
|
25
|
-
inspect_rest_model(@root)
|
@@ -1,31 +0,0 @@
|
|
1
|
-
$:.push 'examples'; require 'helper'
|
2
|
-
|
3
|
-
class Customer < RestModel
|
4
|
-
properties :login, :name
|
5
|
-
embeds_many :phones
|
6
|
-
end
|
7
|
-
|
8
|
-
class Phone < RestModel
|
9
|
-
properties :number, :extension, :description
|
10
|
-
end
|
11
|
-
|
12
|
-
@root = Customer.new({
|
13
|
-
login: 'jackiechan2010',
|
14
|
-
name: "Jackie Chan",
|
15
|
-
phones: [
|
16
|
-
{
|
17
|
-
number: "897289472",
|
18
|
-
extension: "3897",
|
19
|
-
description: "abab"
|
20
|
-
},
|
21
|
-
{
|
22
|
-
number: "987198732",
|
23
|
-
extension: "1897",
|
24
|
-
description: "eheh"
|
25
|
-
}
|
26
|
-
]
|
27
|
-
})
|
28
|
-
|
29
|
-
inspect_rest_model(@root)
|
30
|
-
|
31
|
-
puts "to input without description: #{@root.to_input(phones: {without: :descriptions})}"
|
@@ -1,23 +0,0 @@
|
|
1
|
-
$:.push 'examples'; require 'helper'
|
2
|
-
|
3
|
-
class Customer < RestModel
|
4
|
-
properties :login, :name
|
5
|
-
embeds_one :address
|
6
|
-
end
|
7
|
-
|
8
|
-
class Address < RestModel
|
9
|
-
properties :street, :number, :hint
|
10
|
-
end
|
11
|
-
|
12
|
-
@root = Customer.new({
|
13
|
-
login: 'jackiechan2010',
|
14
|
-
name: "Jackie Chan",
|
15
|
-
address: {
|
16
|
-
street: "Aurora St",
|
17
|
-
number: 1833,
|
18
|
-
hint: "near gas station"
|
19
|
-
}
|
20
|
-
})
|
21
|
-
|
22
|
-
inspect_rest_model(@root)
|
23
|
-
puts "root.to_input without hint: #{@root.to_input(address: {without: :hint})}"
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|