transcriber 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (92) hide show
  1. data/.gitignore +1 -0
  2. data/Guardfile +7 -0
  3. data/README.md +39 -0
  4. data/examples/all.rb +14 -0
  5. data/examples/embeds_many/simple.rb +14 -0
  6. data/examples/embeds_many/with_class_name.rb +14 -0
  7. data/examples/embeds_many/with_if.rb +21 -0
  8. data/examples/embeds_many/with_start_key.rb +17 -0
  9. data/examples/embeds_one/simple.rb +14 -0
  10. data/examples/embeds_one/with_class_name.rb +14 -0
  11. data/examples/embeds_one/with_if.rb +21 -0
  12. data/examples/embeds_one/with_start_key.rb +14 -0
  13. data/examples/has_many/simple.rb +29 -0
  14. data/examples/properties/simple.rb +10 -0
  15. data/examples/properties/with_field.rb +10 -0
  16. data/examples/properties/with_field_path.rb +10 -0
  17. data/examples/properties/with_id.rb +10 -0
  18. data/examples/properties/with_if.rb +19 -0
  19. data/examples/properties/with_key_converter.rb +20 -0
  20. data/lib/transcriber/configuration.rb +13 -0
  21. data/lib/transcriber/resource/builder/embeddables.rb +21 -0
  22. data/lib/transcriber/resource/builder/keys.rb +20 -0
  23. data/lib/transcriber/resource/builder/properties.rb +29 -0
  24. data/lib/transcriber/resource/builder/relations.rb +26 -0
  25. data/lib/transcriber/resource/builder/summarizations.rb +14 -0
  26. data/lib/transcriber/resource/builder.rb +10 -0
  27. data/lib/transcriber/resource/input_path.rb +19 -0
  28. data/lib/transcriber/resource/key/association.rb +29 -0
  29. data/lib/transcriber/resource/key/embeddable.rb +8 -0
  30. data/lib/transcriber/resource/key/property.rb +17 -0
  31. data/lib/transcriber/resource/key/relation.rb +24 -0
  32. data/lib/transcriber/resource/key.rb +29 -0
  33. data/lib/transcriber/resource/parser/embeddable.rb +12 -0
  34. data/lib/transcriber/resource/parser/property.rb +15 -0
  35. data/lib/transcriber/resource/parser.rb +21 -18
  36. data/lib/transcriber/resource/response/embeddable.rb +14 -0
  37. data/lib/transcriber/resource/response/property.rb +11 -0
  38. data/lib/transcriber/resource/response/relation.rb +39 -0
  39. data/lib/transcriber/resource/response.rb +11 -0
  40. data/lib/transcriber/resource/serialization/boolean.rb +32 -9
  41. data/lib/transcriber/resource/serialization/date.rb +7 -7
  42. data/lib/transcriber/resource/serialization/float.rb +9 -5
  43. data/lib/transcriber/resource/serialization/integer.rb +13 -0
  44. data/lib/transcriber/resource/serialization/string.rb +7 -5
  45. data/lib/transcriber/resource.rb +34 -14
  46. data/lib/transcriber/version.rb +1 -1
  47. data/lib/transcriber.rb +38 -2
  48. data/spec/integration/has_many_spec.rb +9 -0
  49. data/spec/integration/property_spec.rb +0 -0
  50. data/spec/spec_helper.rb +22 -1
  51. data/spec/support/examples.rb +21 -0
  52. data/spec/support/out.rb +17 -0
  53. data/spec/unit/configuration_spec.rb +24 -0
  54. data/spec/unit/input_path_spec.rb +68 -0
  55. data/spec/unit/key_spec.rb +48 -0
  56. data/spec/{resource → unit}/parser_spec.rb +16 -21
  57. data/spec/unit/resource/builder/embeddables_spec.rb +27 -0
  58. data/spec/unit/resource/builder/properties_spec.rb +154 -0
  59. data/spec/unit/resource/builder/relations_spec.rb +54 -0
  60. data/spec/unit/resource/builder/shared_example_for_association.rb +20 -0
  61. data/spec/unit/resource/builder/summarize_spec.rb +0 -0
  62. data/spec/unit/resource/key/association_spec.rb +69 -0
  63. data/spec/unit/resource/key/embeddable_spec.rb +7 -0
  64. data/spec/unit/resource/key/property_spec.rb +39 -0
  65. data/spec/unit/resource/key/relation_spec.rb +71 -0
  66. data/spec/unit/resource/parser/embeddable_spec.rb +38 -0
  67. data/spec/unit/resource/parser/property_spec.rb +31 -0
  68. data/spec/unit/resource/response/embeddable_spec.rb +55 -0
  69. data/spec/unit/resource/response/property_spec.rb +22 -0
  70. data/spec/unit/resource/response/relation_spec.rb +105 -0
  71. data/spec/unit/resource/serialization/boolean_spec.rb +25 -0
  72. data/spec/unit/resource/serialization/date_spec.rb +13 -0
  73. data/spec/unit/resource/serialization/float_spec.rb +13 -0
  74. data/spec/unit/resource/serialization/integer_spec.rb +13 -0
  75. data/spec/unit/resource/serialization/string_spec.rb +11 -0
  76. data/spec/unit/resource_spec.rb +46 -0
  77. data/spec/unit/response_spec.rb +44 -0
  78. data/transcriber.gemspec +7 -3
  79. metadata +146 -22
  80. data/lib/transcriber/resource/embeddables/embeddable.rb +0 -31
  81. data/lib/transcriber/resource/embeddables/parser.rb +0 -10
  82. data/lib/transcriber/resource/embeddables/resource.rb +0 -9
  83. data/lib/transcriber/resource/embeddables.rb +0 -18
  84. data/lib/transcriber/resource/properties/parser.rb +0 -9
  85. data/lib/transcriber/resource/properties/property.rb +0 -30
  86. data/lib/transcriber/resource/properties/resource.rb +0 -7
  87. data/lib/transcriber/resource/properties.rb +0 -8
  88. data/lib/transcriber/resource/responses.rb +0 -13
  89. data/lib/transcriber/resource/serialization.rb +0 -10
  90. data/spec/resource/embeddables_spec.rb +0 -228
  91. data/spec/resource/properties_spec.rb +0 -69
  92. data/spec/resource_spec.rb +0 -27
@@ -1,28 +1,23 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Transcriber::Resource::Parser do
4
- class Example < Transcriber::Resource
5
- end
6
-
7
- before { Example.keys.clear }
8
-
3
+ describe Resource::Parser do
9
4
  describe '.parse' do
10
5
  it 'parses simple properties' do
11
- class Example < Transcriber::Resource
6
+ class Example < Resource
12
7
  property :login
13
8
  end
14
9
 
15
- models = Example.parse({'LOGIN' => 'jackiechan2010'})
10
+ models = Example.parse({'login' => 'jackiechan2010'})
16
11
  models.should be_an(Array)
17
12
  models.first.login.should == 'jackiechan2010'
18
13
  end
19
14
 
20
15
  it 'uses different field name if defined' do
21
- class Example < Transcriber::Resource
16
+ class Example < Resource
22
17
  property :login, field: :customer_login
23
18
  end
24
19
 
25
- models = Example.parse({'CUSTOMER_LOGIN' => 'jackiechan2010'})
20
+ models = Example.parse({'customer_login' => 'jackiechan2010'})
26
21
  models.should be_an(Array)
27
22
  models.first.login.should == 'jackiechan2010'
28
23
  end
@@ -30,40 +25,40 @@ describe Transcriber::Resource::Parser do
30
25
  context "uses type defined" do
31
26
  context 'boolean' do
32
27
  before do
33
- class Example < Transcriber::Resource
28
+ class Example < Resource
34
29
  property :can_merge, type: Boolean
35
30
  end
36
31
  end
37
32
 
38
33
  it "returns true when value is 'X'" do
39
- models = Example.parse({'CAN_MERGE' => 'X'})
34
+ models = Example.parse({'can_merge' => 'X'})
40
35
  models.first.can_merge.should be_true
41
36
  end
42
37
 
43
38
  it "returns false when value isn't 'X'" do
44
- models = Example.parse({'CAN_MERGE' => ''})
39
+ models = Example.parse({'can_merge' => ''})
45
40
  models.first.can_merge.should be_false
46
41
  end
47
42
  end
48
43
 
49
44
  context "date" do
50
45
  it "parse" do
51
- class Example < Transcriber::Resource
46
+ class Example < Resource
52
47
  property :issue_date, type: Date
53
48
  end
54
49
 
55
- models = Example.parse({'ISSUE_DATE' => '2011-01-01'})
50
+ models = Example.parse({'issue_date' => '2011-01-01'})
56
51
  models.first.issue_date.should == Date.parse('2011-01-01')
57
52
  end
58
53
  end
59
54
 
60
55
  context "float" do
61
56
  it "parse" do
62
- class Example < Transcriber::Resource
57
+ class Example < Resource
63
58
  property :amount, type: Float
64
59
  end
65
60
 
66
- models = Example.parse({'AMOUNT' => '1234.5'})
61
+ models = Example.parse({'amount' => '1234.5'})
67
62
  models.first.amount.should == 1234.5
68
63
  end
69
64
  end
@@ -71,20 +66,20 @@ describe Transcriber::Resource::Parser do
71
66
 
72
67
  context "maps values if defined" do
73
68
  before do
74
- class Example < Transcriber::Resource
69
+ class Example < Resource
75
70
  property :status, values: {paid: '01', unpaid: '02'}
76
71
  end
77
72
  end
78
73
 
79
74
  it "returns key when value was mapped" do
80
- models = Example.parse({'STATUS' => '02'})
75
+ models = Example.parse({'status' => '02'})
81
76
  models.first.status.should == :unpaid
82
77
  end
83
78
 
84
79
  it "returns nil when value wasn't mapped" do
85
- models = Example.parse({'STATUS' => '03'})
80
+ models = Example.parse({'status' => '03'})
86
81
  models.first.status.should be_nil
87
82
  end
88
83
  end
89
84
  end
90
- end
85
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+ require 'unit/resource/builder/shared_example_for_association'
3
+
4
+ describe Resource::Builder::Embeddables do
5
+ before do
6
+ class Example < Transcriber::Resource
7
+ embeds_one :contract, some_option: 'contract options'
8
+ embeds_many :invoice_items, some_option: 'invoice options'
9
+ end
10
+ end
11
+
12
+ describe ".embeds_one" do
13
+ let(:field) {:contract}
14
+ let(:many) {false}
15
+ let(:options) {{some_option: 'contract options', many: many}}
16
+
17
+ it_behaves_like "an association"
18
+ end
19
+
20
+ describe ".embeds_many" do
21
+ let(:field) {:invoice_items}
22
+ let(:many) {true}
23
+ let(:options) {{some_option: 'invoice options', many: many}}
24
+
25
+ it_behaves_like "an association"
26
+ end
27
+ end
@@ -0,0 +1,154 @@
1
+ require 'spec_helper'
2
+
3
+ describe Resource::Builder::Properties do
4
+ shared_examples_for "a property" do
5
+ it "defines an attr_accessor with property name" do
6
+ Example.new.tap do |example|
7
+ example.respond_to?("#{field}").should be_true
8
+ example.respond_to?("#{field}=").should be_true
9
+ end
10
+ end
11
+
12
+ it "puts a new property in keys list" do
13
+ Example.keys.find {|key| key.name == field}.name.should == field
14
+ end
15
+
16
+ it "supports option definition" do
17
+ Example.keys.find {|key| key.name == field}.options.should == options
18
+ end
19
+ end
20
+
21
+ shared_examples_for "a key" do
22
+ it "is referenced as the resource id" do
23
+ Example.id_key.name.should == field
24
+ end
25
+ end
26
+
27
+ describe ".id" do
28
+ context "when it's just 'id'" do
29
+ before do
30
+ class Example < Transcriber::Resource
31
+ id
32
+ end
33
+ end
34
+
35
+ let(:field) {:id}
36
+ let(:options) {{id: true}}
37
+
38
+ it_behaves_like "a property"
39
+ it_behaves_like "a key"
40
+ end
41
+
42
+ context "when it has a different name" do
43
+ before do
44
+ class Example < Transcriber::Resource
45
+ id :different_name
46
+ end
47
+ end
48
+
49
+ let(:field) {:different_name}
50
+ let(:options) {{id: true}}
51
+
52
+ it_behaves_like "a property"
53
+ it_behaves_like "a key"
54
+ end
55
+
56
+ context "when it has options" do
57
+ before do
58
+ class Example < Transcriber::Resource
59
+ id some_option: 'some option value'
60
+ end
61
+ end
62
+
63
+ let(:field) {:id}
64
+ let(:options) {{some_option: 'some option value', id: true}}
65
+
66
+ it_behaves_like "a property"
67
+ it_behaves_like "a key"
68
+ end
69
+
70
+ context "when it has both different name and options" do
71
+ before do
72
+ class Example < Transcriber::Resource
73
+ id :different_name, some_option: 'some option value'
74
+ end
75
+ end
76
+
77
+ let(:field) {:different_name}
78
+ let(:options) {{some_option: 'some option value', id: true}}
79
+
80
+ it_behaves_like "a property"
81
+ it_behaves_like "a key"
82
+ end
83
+
84
+ context "when it defines somehow the option id: false" do
85
+ before do
86
+ class Example < Transcriber::Resource
87
+ id id: false
88
+ end
89
+ end
90
+
91
+ let(:field) {:id}
92
+ let(:options) {{id: true}}
93
+
94
+ it_behaves_like "a property"
95
+ it_behaves_like "a key"
96
+ end
97
+ end
98
+
99
+ describe ".property" do
100
+ context "when it is a normal property" do
101
+ before do
102
+ class Example < Transcriber::Resource
103
+ property :document_number, some_option: 'this is an option'
104
+ end
105
+ end
106
+
107
+ let(:field) {:document_number}
108
+ let(:options) {{some_option: 'this is an option'}}
109
+
110
+ it_behaves_like "a property"
111
+ end
112
+
113
+ context "when it has the option :id set to true" do
114
+ before do
115
+ class Example < Transcriber::Resource
116
+ property :document_number, id: true
117
+ end
118
+ end
119
+
120
+ let(:field) {:document_number}
121
+ let(:options) {{id: true}}
122
+
123
+ it_behaves_like "a property"
124
+ it_behaves_like "a key"
125
+ end
126
+ end
127
+
128
+ describe ".properties" do
129
+ before do
130
+ class Example < Transcriber::Resource
131
+ properties :id, :description
132
+ properties :price, :quantity, firstoption: 'this is the first option', secondoption: 'this is the second option'
133
+ end
134
+ end
135
+
136
+ [:id, :description].each do |item|
137
+ context "for #{item}" do
138
+ let(:field) {item}
139
+ let(:options) {{}}
140
+
141
+ it_behaves_like "a property"
142
+ end
143
+ end
144
+
145
+ [:price, :quantity].each do |item|
146
+ context "for #{item}" do
147
+ let(:field) {item}
148
+ let(:options) {{firstoption: 'this is the first option', secondoption: 'this is the second option'}}
149
+
150
+ it_behaves_like "a property"
151
+ end
152
+ end
153
+ end
154
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+ require 'unit/resource/builder/shared_example_for_association'
3
+
4
+ describe Resource::Builder::Relations do
5
+ describe ".has_one" do
6
+ before do
7
+ class Example < Transcriber::Resource
8
+ has_one :item, some_option: 'option'
9
+ end
10
+ end
11
+
12
+ let(:field) {:item}
13
+ let(:many) {false}
14
+ let(:options) {{some_option: 'option', many: many, has: true}}
15
+
16
+ it_behaves_like "an association"
17
+
18
+ it "puts a new relation in relation list" do
19
+ Example.relations.find {|relation| relation.name == field}.name.should == field
20
+ end
21
+ end
22
+
23
+ describe ".has_many" do
24
+ before do
25
+ class Example < Transcriber::Resource
26
+ has_many :items, some_option: 'option'
27
+ end
28
+ end
29
+
30
+ let(:field) {:items}
31
+ let(:many) {true}
32
+ let(:options) {{some_option: 'option', many: many, has: true}}
33
+
34
+ it_behaves_like "an association"
35
+
36
+ it "puts a new relation in relation list" do
37
+ Example.relations.find {|relation| relation.name == field}.name.should == field
38
+ end
39
+ end
40
+
41
+ describe ".belongs_to" do
42
+ before do
43
+ class Example < Transcriber::Resource
44
+ belongs_to :item, some_option: 'option'
45
+ end
46
+ end
47
+
48
+ let(:field) {:item}
49
+ let(:many) {false}
50
+ let(:options) {{some_option: 'option', many: many, has: false}}
51
+
52
+ it_behaves_like "an association"
53
+ end
54
+ end
@@ -0,0 +1,20 @@
1
+ shared_examples_for "an association" do
2
+ it "defines an attr_accessor with association name" do
3
+ Example.new.tap do |example|
4
+ example.respond_to?("#{field}").should be_true
5
+ example.respond_to?("#{field}=").should be_true
6
+ end
7
+ end
8
+
9
+ it "puts a new association in keys list" do
10
+ Example.keys.find {|key| key.name == field}.name.should == field
11
+ end
12
+
13
+ it "supports option definition" do
14
+ Example.keys.find {|key| key.name == field}.options.should == options
15
+ end
16
+
17
+ it "many? responds appropriately" do
18
+ Example.keys.find {|key| key.name == field}.many?.should == many
19
+ end
20
+ end
File without changes
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+
3
+ describe Resource::Association do
4
+ describe "#initialize" do
5
+ context "class_name" do
6
+ context "when class_name option is passed" do
7
+ subject do
8
+ Resource::Association.new(:login, class_name: :some_class)
9
+ end
10
+
11
+ it "sets passed class_name" do
12
+ subject.instance_variable_get("@class_name").should == "SomeClass"
13
+ end
14
+ end
15
+
16
+ context "when class_name option is not passed" do
17
+ subject {Resource::Association.new(:login)}
18
+
19
+ it "sets default class_name" do
20
+ subject.instance_variable_get("@class_name").should == "Login"
21
+ end
22
+ end
23
+ end
24
+
25
+ context "many" do
26
+ context "when many option is passed with true" do
27
+ subject do
28
+ Resource::Association.new(:login, many: true)
29
+ end
30
+
31
+ it "returns false to one?" do
32
+ subject.one?.should be_false
33
+ end
34
+
35
+ it "returns true to many?" do
36
+ subject.many?.should be_true
37
+ end
38
+ end
39
+
40
+ context "when many option is passed with false" do
41
+ subject do
42
+ Resource::Association.new(:login, many: false)
43
+ end
44
+
45
+ it "returns true to one?" do
46
+ subject.one?.should be_true
47
+ end
48
+
49
+ it "returns false to many?" do
50
+ subject.many?.should be_false
51
+ end
52
+ end
53
+
54
+ context "when many option is not passed" do
55
+ subject do
56
+ Resource::Association.new(:login)
57
+ end
58
+
59
+ it "returns true to one?" do
60
+ subject.one?.should be_true
61
+ end
62
+
63
+ it "returns false to many?" do
64
+ subject.many?.should be_false
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe Resource::Embeddable do
4
+ it "is a relation" do
5
+ Resource::Embeddable.superclass.should == Resource::Association
6
+ end
7
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ describe Resource::Property do
4
+ describe "#initialize" do
5
+ context "serializer" do
6
+ context "when custom is provided" do
7
+ subject do
8
+ options = {type: Resource::Serialization::Boolean}
9
+ Resource::Property.new(:haz_bool?, options)
10
+ end
11
+
12
+ it "configures custom" do
13
+ subject.serializer.should == Resource::Serialization::Boolean
14
+ end
15
+ end
16
+
17
+ context "when it isn't provided" do
18
+ subject {Resource::Property.new(:login)}
19
+
20
+ it "uses String serializer" do
21
+ subject.serializer.should == Resource::Serialization::String
22
+ end
23
+ end
24
+ end
25
+
26
+ context "when values map is provided" do
27
+ let(:values) {{paid: '01', unpaid: '02'}}
28
+
29
+ subject do
30
+ options = {values: values}
31
+ Resource::Property.new(:payment_status, options)
32
+ end
33
+
34
+ it "configures values" do
35
+ subject.translations.should == values.invert
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+
3
+ describe Resource::Relation do
4
+ it "is a relation" do
5
+ Resource::Relation.superclass.should == Resource::Association
6
+ end
7
+
8
+ describe "#has?" do
9
+ context "when it has one resource" do
10
+ subject do
11
+ Resource::Relation.new(:service, many: false, has: true)
12
+ end
13
+
14
+ it "returns true" do
15
+ subject.has?.should be_true
16
+ end
17
+ end
18
+
19
+ context "when it has many resources" do
20
+ subject do
21
+ Resource::Relation.new(:service, many: true, has: true)
22
+ end
23
+
24
+ it "returns true" do
25
+ subject.has?.should be_true
26
+ end
27
+ end
28
+
29
+ context "when it belongs to a resource" do
30
+ subject do
31
+ Resource::Relation.new(:service, many: false, has: false)
32
+ end
33
+
34
+ it "returns true" do
35
+ subject.has?.should be_false
36
+ end
37
+ end
38
+ end
39
+
40
+ describe "#belongs?" do
41
+ context "when it belongs to a resource" do
42
+ subject do
43
+ Resource::Relation.new(:service, many: false, has: false)
44
+ end
45
+
46
+ it "returns true" do
47
+ subject.belongs?.should be_true
48
+ end
49
+ end
50
+
51
+ context "when it has one resource" do
52
+ subject do
53
+ Resource::Relation.new(:service, many: false, has: true)
54
+ end
55
+
56
+ it "returns true" do
57
+ subject.belongs?.should be_false
58
+ end
59
+ end
60
+
61
+ context "when it has many resources" do
62
+ subject do
63
+ Resource::Relation.new(:service, many: true, has: true)
64
+ end
65
+
66
+ it "returns true" do
67
+ subject.belongs?.should be_false
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ describe Resource::Parser::Embeddable do
4
+ context "when it embeds one" do
5
+ before do
6
+ class ExampleChild < Resource
7
+ property :id
8
+ end
9
+ end
10
+
11
+ let(:value) {{"id" => "7000"}}
12
+
13
+ subject {Resource::Embeddable.new(:example_child, many: false)}
14
+
15
+ it "parses child" do
16
+ child = subject.parse(value)
17
+ child.id.should == "7000"
18
+ end
19
+ end
20
+
21
+ context "when it embeds many" do
22
+ before do
23
+ class ExampleChild < Resource
24
+ property :id
25
+ end
26
+ end
27
+
28
+ let(:value) {[{"id" => "7000"}, {"id" => "7001"}]}
29
+
30
+ subject {Resource::Embeddable.new(:example_children, many: true)}
31
+
32
+ it "parses children" do
33
+ children = subject.parse(value)
34
+ children[0].id.should == "7000"
35
+ children[1].id.should == "7001"
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe Resource::Parser::Property do
4
+ subject {Resource::Property.new(:login)}
5
+
6
+ let(:value) {"jackiechan2010"}
7
+ let(:serializer_mock) {mock :serializer}
8
+
9
+ before do
10
+ subject.stub!(:serializer).and_return serializer_mock
11
+ end
12
+
13
+ it "tries to serialize value" do
14
+ serializer_mock.should_receive(:serialize).with(value).and_return(value)
15
+ subject.parse(value)
16
+ end
17
+
18
+ context "when there is a values map" do
19
+ let(:values) {{paid: '01', unpaid: '02'}}
20
+
21
+ subject do
22
+ options = {values: values}
23
+ Resource::Property.new(:payment_status, options)
24
+ end
25
+
26
+ it "maps value" do
27
+ serializer_mock.should_receive(:serialize).with(value).and_return "02"
28
+ subject.parse(value).should == :unpaid
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ describe Resource::Response::Embeddable do
4
+ before do
5
+ class ExampleChild < Resource
6
+ property :id
7
+ end
8
+
9
+ class Example < Resource
10
+ embeds_one :example_child
11
+ embeds_many :example_children
12
+ end
13
+ end
14
+
15
+ shared_examples_for "an embeddable" do
16
+ it "returns a pair with property name and value" do
17
+ subject.to_resource(example).should == result
18
+ end
19
+
20
+ context "when this key shouldn't be present on resource" do
21
+ it "returns an empty hash" do
22
+ subject.should_receive(:present?).and_return false
23
+ subject.to_resource(example).should == {}
24
+ end
25
+ end
26
+ end
27
+
28
+ context "when embeds one" do
29
+ subject {Example.keys[0]}
30
+
31
+ let :example do
32
+ Example.new.tap do |example|
33
+ example.example_child = ExampleChild.new(id: "200")
34
+ end
35
+ end
36
+
37
+ let(:result) {{example_child: {id: "200"}}}
38
+
39
+ it_behaves_like "an embeddable"
40
+ end
41
+
42
+ context "when embeds many" do
43
+ subject {Example.keys[1]}
44
+
45
+ let :example do
46
+ Example.new.tap do |example|
47
+ example.example_children = [ExampleChild.new(id: "300")]
48
+ end
49
+ end
50
+
51
+ let(:result) {{example_children: [{id: "300"}]}}
52
+
53
+ it_behaves_like "an embeddable"
54
+ end
55
+ end