rest_model 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (114) hide show
  1. data/.gitignore +5 -0
  2. data/Gemfile +2 -0
  3. data/Guardfile +7 -0
  4. data/README.md +237 -0
  5. data/Rakefile +2 -0
  6. data/examples/all.rb +12 -0
  7. data/examples/embeds_many/invisible.rb +16 -0
  8. data/examples/embeds_many/simple.rb +13 -0
  9. data/examples/embeds_many/with_array.rb +10 -0
  10. data/examples/embeds_many/with_class_name.rb +13 -0
  11. data/examples/embeds_many/with_fields.rb +9 -0
  12. data/examples/embeds_many/with_if.rb +16 -0
  13. data/examples/embeds_many/with_nil_value.rb +12 -0
  14. data/examples/embeds_many/with_start_key.rb +13 -0
  15. data/examples/embeds_one/flattened.rb +12 -0
  16. data/examples/embeds_one/simple.rb +12 -0
  17. data/examples/embeds_one/with_class_name.rb +12 -0
  18. data/examples/embeds_one/with_if.rb +16 -0
  19. data/examples/embeds_one/with_start_key.rb +12 -0
  20. data/examples/has_many/simple.rb +28 -0
  21. data/examples/helper.rb +9 -0
  22. data/examples/initialize/embeds_many.rb +25 -0
  23. data/examples/initialize/embeds_many_array.rb +9 -0
  24. data/examples/initialize/embeds_one.rb +19 -0
  25. data/examples/initialize/simple.rb +8 -0
  26. data/examples/properties/array_serialization.rb +8 -0
  27. data/examples/properties/collections.rb +11 -0
  28. data/examples/properties/simple.rb +8 -0
  29. data/examples/properties/with_field.rb +8 -0
  30. data/examples/properties/with_field_path.rb +8 -0
  31. data/examples/properties/with_id.rb +8 -0
  32. data/examples/properties/with_if.rb +21 -0
  33. data/examples/properties/with_key_converter.rb +18 -0
  34. data/examples/properties/with_two_key_converters.rb +27 -0
  35. data/examples/properties/with_values.rb +10 -0
  36. data/examples/summarization/simple.rb +21 -0
  37. data/examples/to_input/embeds_many.rb +25 -0
  38. data/examples/to_input/embeds_many_without_key.rb +31 -0
  39. data/examples/to_input/embeds_one.rb +13 -0
  40. data/examples/to_input/embeds_one_without_key.rb +23 -0
  41. data/examples/to_input/flattened.rb +12 -0
  42. data/examples/to_input/serializables.rb +19 -0
  43. data/examples/to_input/simple.rb +8 -0
  44. data/examples/to_input/with_field.rb +8 -0
  45. data/examples/to_input/with_field_path.rb +8 -0
  46. data/examples/to_input/with_fields.rb +8 -0
  47. data/examples/to_input/without_key.rb +11 -0
  48. data/examples/to_input/without_nil.rb +11 -0
  49. data/lib/rest_model/configuration.rb +45 -0
  50. data/lib/rest_model/key/association.rb +42 -0
  51. data/lib/rest_model/key/builder.rb +30 -0
  52. data/lib/rest_model/key/embeddable/builder.rb +19 -0
  53. data/lib/rest_model/key/embeddable/response.rb +25 -0
  54. data/lib/rest_model/key/embeddable/retriever.rb +20 -0
  55. data/lib/rest_model/key/embeddable/sender.rb +41 -0
  56. data/lib/rest_model/key/embeddable.rb +29 -0
  57. data/lib/rest_model/key/href/response.rb +10 -0
  58. data/lib/rest_model/key/href.rb +10 -0
  59. data/lib/rest_model/key/property/builder.rb +27 -0
  60. data/lib/rest_model/key/property/response.rb +9 -0
  61. data/lib/rest_model/key/property/retriever.rb +24 -0
  62. data/lib/rest_model/key/property/sender.rb +41 -0
  63. data/lib/rest_model/key/property.rb +22 -0
  64. data/lib/rest_model/key/relation/builder.rb +24 -0
  65. data/lib/rest_model/key/relation/response.rb +38 -0
  66. data/lib/rest_model/key/relation.rb +23 -0
  67. data/lib/rest_model/key.rb +44 -0
  68. data/lib/rest_model/response.rb +42 -0
  69. data/lib/rest_model/serialization/boolean.rb +48 -0
  70. data/lib/rest_model/serialization/date.rb +19 -0
  71. data/lib/rest_model/serialization/enumerable.rb +15 -0
  72. data/lib/rest_model/serialization/float.rb +17 -0
  73. data/lib/rest_model/serialization/integer.rb +17 -0
  74. data/lib/rest_model/serialization/string.rb +13 -0
  75. data/lib/rest_model/source/path.rb +25 -0
  76. data/lib/rest_model/source/retriever.rb +48 -0
  77. data/lib/rest_model/source/sender.rb +38 -0
  78. data/lib/rest_model/version.rb +3 -0
  79. data/lib/rest_model.rb +101 -0
  80. data/rest_model.gemspec +26 -0
  81. data/spec/.DS_Store +0 -0
  82. data/spec/integration/embeds_many_spec.rb +57 -0
  83. data/spec/integration/embeds_one_spec.rb +41 -0
  84. data/spec/integration/has_many_spec.rb +27 -0
  85. data/spec/integration/property_spec.rb +69 -0
  86. data/spec/integration/summarization_spec.rb +12 -0
  87. data/spec/spec_helper.rb +26 -0
  88. data/spec/support/examples.rb +29 -0
  89. data/spec/support/out.rb +17 -0
  90. data/spec/support/shared_examples.rb +20 -0
  91. data/spec/unit/configuration_spec.rb +24 -0
  92. data/spec/unit/key/association_spec.rb +69 -0
  93. data/spec/unit/key/embeddable/builder_spec.rb +26 -0
  94. data/spec/unit/key/embeddable/response_spec.rb +55 -0
  95. data/spec/unit/key/embeddable/retriever_spec.rb +38 -0
  96. data/spec/unit/key/embeddable_spec.rb +7 -0
  97. data/spec/unit/key/property/builder_spec.rb +154 -0
  98. data/spec/unit/key/property/response_spec.rb +22 -0
  99. data/spec/unit/key/property/retriever_spec.rb +31 -0
  100. data/spec/unit/key/property_spec.rb +39 -0
  101. data/spec/unit/key/relation/builder_spec.rb +53 -0
  102. data/spec/unit/key/relation/response_spec.rb +105 -0
  103. data/spec/unit/key/relation_spec.rb +71 -0
  104. data/spec/unit/key_spec.rb +101 -0
  105. data/spec/unit/response_spec.rb +44 -0
  106. data/spec/unit/rest_model_spec.rb +46 -0
  107. data/spec/unit/serialization/boolean_spec.rb +25 -0
  108. data/spec/unit/serialization/date_spec.rb +13 -0
  109. data/spec/unit/serialization/float_spec.rb +13 -0
  110. data/spec/unit/serialization/integer_spec.rb +13 -0
  111. data/spec/unit/serialization/string_spec.rb +11 -0
  112. data/spec/unit/source/path_spec.rb +62 -0
  113. data/spec/unit/source/retriever_spec.rb +85 -0
  114. metadata +260 -0
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ describe RestModel::Relation::Builder do
4
+ describe ".has_one" do
5
+ before do
6
+ class Example < RestModel
7
+ has_one :item, some_option: 'option'
8
+ end
9
+ end
10
+
11
+ let(:field) {:item}
12
+ let(:many) {false}
13
+ let(:options) {{some_option: 'option', many: many, has: true}}
14
+
15
+ it_behaves_like "an association"
16
+
17
+ it "puts a new relation in relation list" do
18
+ Example.relations.find {|relation| relation.name == field}.name.should == field
19
+ end
20
+ end
21
+
22
+ describe ".has_many" do
23
+ before do
24
+ class Example < RestModel
25
+ has_many :items, some_option: 'option'
26
+ end
27
+ end
28
+
29
+ let(:field) {:items}
30
+ let(:many) {true}
31
+ let(:options) {{some_option: 'option', many: many, has: true}}
32
+
33
+ it_behaves_like "an association"
34
+
35
+ it "puts a new relation in relation list" do
36
+ Example.relations.find {|relation| relation.name == field}.name.should == field
37
+ end
38
+ end
39
+
40
+ describe ".belongs_to" do
41
+ before do
42
+ class Example < RestModel
43
+ belongs_to :item, some_option: 'option'
44
+ end
45
+ end
46
+
47
+ let(:field) {:item}
48
+ let(:many) {false}
49
+ let(:options) {{some_option: 'option', many: many, has: false}}
50
+
51
+ it_behaves_like "an association"
52
+ end
53
+ end
@@ -0,0 +1,105 @@
1
+ require 'spec_helper'
2
+
3
+ describe RestModel::Response do
4
+ shared_examples_for "a relation" do
5
+ context "#to_relation" do
6
+ it "returns a pair with property name and value" do
7
+ subject.to_relation(example).should == result
8
+ end
9
+ end
10
+
11
+ context "#to_resource" do
12
+ context "when the key shouldn't be visible on resource" do
13
+ it "returns an empty hash" do
14
+ subject.should_receive(:visible?).and_return false
15
+ subject.to_resource(example).should == {}
16
+ end
17
+ end
18
+
19
+ context "when the relation instance variable in parent returns nil" do
20
+ it "returns an empty hash" do
21
+ subject.to_resource(example).should == {}
22
+ end
23
+ end
24
+
25
+ context "otherwise" do
26
+
27
+ end
28
+ end
29
+ end
30
+
31
+ context "when has one" do
32
+ before do
33
+ class ExampleChild < RestModel
34
+ id
35
+ belongs_to :example
36
+ end
37
+
38
+ class Example < RestModel
39
+ id
40
+ has_one :example_child
41
+ end
42
+ end
43
+
44
+ subject {Example.keys[1]}
45
+
46
+ let(:example) {Example.new(id: 100)}
47
+ let(:result) {{rel: :example_child, href: "http://example.com/examples/100/example_child"}}
48
+
49
+ it_behaves_like "a relation"
50
+ end
51
+
52
+ context "when has many" do
53
+ before do
54
+ class ExampleChild < RestModel
55
+ property :id
56
+ belongs_to :example
57
+ end
58
+
59
+ class Example < RestModel
60
+ property :id
61
+ has_many :example_children
62
+ end
63
+ end
64
+
65
+ subject {Example.keys[1]}
66
+
67
+ let(:example) {Example.new(id: 200)}
68
+ let(:result) {{rel: :example_children, href: "http://example.com/examples/200/example_children"}}
69
+
70
+ it_behaves_like "a relation"
71
+ end
72
+
73
+ context "when belongs to one" do
74
+ before do
75
+ class ExampleChild < RestModel
76
+ property :id
77
+ belongs_to :example
78
+ end
79
+
80
+ class Example < RestModel
81
+ property :id
82
+ has_many :example_children
83
+ end
84
+ end
85
+
86
+ subject {ExampleChild.keys[1]}
87
+
88
+ let(:example) {ExampleChild.new(id: 200)}
89
+ let(:result) {{rel: :example, href: "http://example.com/examples/200/example_children"}}
90
+
91
+ it_behaves_like "a relation"
92
+ end
93
+
94
+ context "when using forbidden key names" do
95
+ [:resource_id, :resource].each do |forbidden_name|
96
+ it "raises an exception for #{forbidden_name}" do
97
+ expect {
98
+ class Example < RestModel
99
+ property forbidden_name
100
+ end
101
+ }.to raise_error
102
+ end
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+
3
+ describe RestModel::Relation do
4
+ it "is a relation" do
5
+ RestModel::Relation.superclass.should == RestModel::Association
6
+ end
7
+
8
+ describe "#has?" do
9
+ context "when it has one resource" do
10
+ subject do
11
+ RestModel::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
+ RestModel::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
+ RestModel::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
+ RestModel::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
+ RestModel::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
+ RestModel::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,101 @@
1
+ require 'spec_helper'
2
+
3
+ describe RestModel::Key do
4
+ let(:name) {"key_name"}
5
+ let(:options) {{any_option: "here"}}
6
+
7
+ subject {RestModel::Property.new(name, options)}
8
+
9
+ describe "#initialize" do
10
+ it "sets key name" do
11
+ subject.name.should == name
12
+ end
13
+
14
+ it "sets options" do
15
+ subject.options.should == options
16
+ end
17
+ end
18
+
19
+ describe "#visible?" do
20
+ context "when :visible option was defined" do
21
+ context "and :if option was defined with false" do
22
+ subject {RestModel::Property.new(name, {if: proc {false}, visible: true})}
23
+ it "return false even when visible is true" do
24
+ subject.visible?(nil).should be_false
25
+ end
26
+ end
27
+
28
+ context "and it evaluates to false" do
29
+ subject {RestModel::Property.new(name, {visible: false})}
30
+
31
+ it "returns false" do
32
+ subject.visible?(nil).should be_false
33
+ end
34
+ end
35
+
36
+ context "and it evaluates to true" do
37
+ subject {RestModel::Property.new(name, {visible: true})}
38
+
39
+ it "returns true" do
40
+ subject.visible?(nil).should be_true
41
+ end
42
+ end
43
+
44
+ context "with a proc" do
45
+ context "and it evaluates true" do
46
+ subject {RestModel::Property.new(name, {visible: proc {true}})}
47
+ it "returns true" do
48
+ subject.visible?("a").should be_true
49
+ end
50
+ end
51
+
52
+ context "and it evaluates false" do
53
+ subject {RestModel::Property.new(name, {visible: proc {false}})}
54
+ it "returns false" do
55
+ subject.visible?("a").should be_false
56
+ end
57
+ end
58
+
59
+ context "and it depends on resource instance" do
60
+ subject {RestModel::Property.new(name, {visible: proc {show}})}
61
+
62
+ it "uses it" do
63
+ resource = OpenStruct.new
64
+ resource.show = true
65
+ subject.visible?(resource).should be_true
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
71
+
72
+ describe "#present?" do
73
+ context "when :if option was defined with a proc" do
74
+ context "and it evaluates to false" do
75
+ subject {RestModel::Property.new(name, {if: proc {false}})}
76
+
77
+ it "returns false" do
78
+ subject.present?(nil).should be_false
79
+ end
80
+ end
81
+
82
+ context "and it evaluates to true" do
83
+ subject {RestModel::Property.new(name, {if: proc {true}})}
84
+
85
+ it "returns true" do
86
+ subject.present?(nil).should be_true
87
+ end
88
+ end
89
+
90
+ context "and it depends on resource instance" do
91
+ subject {RestModel::Property.new(name, {if: proc {show}})}
92
+
93
+ it "uses it" do
94
+ resource = OpenStruct.new
95
+ resource.show = true
96
+ subject.present?(resource).should be_true
97
+ end
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ describe RestModel::Response do
4
+ before do
5
+ class Example < RestModel
6
+ id :login
7
+ property :phone
8
+ embeds_one :address, class_name: :example_child
9
+ end
10
+
11
+ class ExampleChild < RestModel
12
+ property :street
13
+ property :city
14
+ end
15
+ end
16
+
17
+ let(:input) do
18
+ {
19
+ login: 'jackiechan2010',
20
+ phone: '32232188',
21
+ address: {
22
+ street: 'Aurora St.',
23
+ city: 'Recife'
24
+ }
25
+ }
26
+ end
27
+
28
+ describe "#normalize" do
29
+ context "when one resource is received" do
30
+ it "returns a resource" do
31
+ model = Example.parse(input).first
32
+ Example.normalize(model).with_indifferent_access.should == input.with_indifferent_access
33
+ end
34
+ end
35
+
36
+ context "when a list of resources is received" do
37
+ it "returns some resources in an entries array" do
38
+ model = 3.times.map {Example.parse(input).first}
39
+ result = {entries: 3.times.map {input}}
40
+ Example.normalize(model).with_indifferent_access.should == result.with_indifferent_access
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ describe RestModel do
4
+ describe '#resource' do
5
+ before do
6
+ class Example < RestModel
7
+ property :login
8
+ property :name
9
+ property :age, type: Float
10
+ end
11
+ end
12
+
13
+ it "returns a hash with properties and its values" do
14
+ example = Example.new(login: 'jackiechan2010', name: 'Jackie Chan', age: 45)
15
+ example.resource.should == {login: 'jackiechan2010', name: 'Jackie Chan', age: 45}
16
+ end
17
+ end
18
+
19
+ context "when using not allowed names" do
20
+ [:resource_id, :resource, :link].each do |unallowed|
21
+ it "warns when method #{unallowed} is redefined" do
22
+ output = out do
23
+ eval <<-RUBY
24
+ class Example < RestModel
25
+ def #{unallowed}
26
+ end
27
+ end
28
+ RUBY
29
+ end
30
+ output.should =~ /^warning: redefining '#{unallowed}' may cause serious problems/
31
+ end
32
+
33
+ %w(property embeds_one embeds_many has_one has_many belongs_to).each do |kind|
34
+ it "removes method #{unallowed} created by #{kind}" do
35
+ expect {
36
+ eval <<-RUBY
37
+ class Example < RestModel
38
+ #{kind} :#{unallowed}
39
+ end
40
+ RUBY
41
+ }.to raise_error "you can't define a key with name '#{unallowed}'"
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe RestModel::Serialization::Boolean do
4
+ [true, "true", "TRUE", "1", 1, 1.0, "x", "X", "t", "T"].each do |true_value|
5
+ context "when value is #{true_value}" do
6
+ it "returns true" do
7
+ subject.class.serialize(true_value).should be_true
8
+ end
9
+ end
10
+ end
11
+
12
+ [false, "false", "FALSE", "0", 0, 0.0, "", " ", "f", "F", nil].each do |false_value|
13
+ context "when value is #{false_value}" do
14
+ it "returns false" do
15
+ subject.class.serialize(false_value).should be_false
16
+ end
17
+ end
18
+ end
19
+
20
+ context "when an invalid value is serialized" do
21
+ it "raises an error" do
22
+ expect {subject.class.serialize("neither true or false")}.to raise_error
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe RestModel::Serialization::Date do
4
+ it "converts to date" do
5
+ subject.class.serialize('2011-07-01').should == Date.new(2011, 07, 01)
6
+ end
7
+
8
+ context "when an invalid value is serialized" do
9
+ it "raises an error" do
10
+ expect {subject.class.serialize("not a date")}.to raise_error
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe RestModel::Serialization::Float do
4
+ it "converts to float" do
5
+ subject.class.serialize('1200.49').should == 1200.49
6
+ end
7
+
8
+ context "when an invalid value is serialized" do
9
+ it "raises an error" do
10
+ expect {subject.class.serialize("not a float")}.to raise_error
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe RestModel::Serialization::Integer do
4
+ it "converts to integer" do
5
+ subject.class.serialize('1200').should == 1200
6
+ end
7
+
8
+ context "when an invalid value is serialized" do
9
+ it "raises an error" do
10
+ expect {subject.class.serialize("not an integer")}.to raise_error
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe RestModel::Serialization::String do
4
+ it "converts value to string" do
5
+ subject.class.serialize(:something).should == "something"
6
+ end
7
+
8
+ it "returns nil when value is nil" do
9
+ subject.class.serialize(nil).should be_nil
10
+ end
11
+ end
@@ -0,0 +1,62 @@
1
+ require 'spec_helper'
2
+
3
+ module Upcasing
4
+ def self.call(keys)
5
+ keys.map {|key| key.to_s.upcase}
6
+ end
7
+ end
8
+
9
+ describe RestModel::Source::Path do
10
+ shared_examples_for "an input path" do
11
+ it "brings the correct input path" do
12
+ subject.source_path.should == path
13
+ end
14
+ end
15
+
16
+ context "when a custom path is not defined" do
17
+ context "and there is'nt a custom keys input converter" do
18
+ subject {RestModel::Key.new(:name)}
19
+ let(:path) {[:name]}
20
+ it_behaves_like "an input path"
21
+ end
22
+
23
+ context "and there is a custom keys input converter" do
24
+ subject {RestModel::Key.new(:name, convert_input_keys: Upcasing)}
25
+ let(:path) {['NAME']}
26
+ it_behaves_like "an input path"
27
+ end
28
+ end
29
+
30
+ context "when a custom path is defined" do
31
+ context "and it is a simple name" do
32
+ subject {RestModel::Key.new(:name, field: :other_name)}
33
+ let(:path) {[:other_name]}
34
+ it_behaves_like "an input path"
35
+ end
36
+
37
+ context "and it is a set of names composed by dot" do
38
+ subject {RestModel::Key.new(:name, field: 'a.long.way.to.go')}
39
+ let(:path) {['a', 'long', 'way', 'to', 'go']}
40
+ it_behaves_like "an input path"
41
+ end
42
+
43
+ context "and it is a set of names composed by dot" do
44
+ subject {RestModel::Key.new(:name, field: 'a.long.long.way.to.go')}
45
+ let(:path) {['a', 'long', 'long', 'way', 'to', 'go']}
46
+ it_behaves_like "an input path"
47
+ end
48
+
49
+ context "and it is a set of names composed by dot using :start_key instead of :field" do
50
+ subject {RestModel::Key.new(:name, start_key: 'a.long.way.to.go')}
51
+ let(:path) {['a', 'long', 'way', 'to', 'go']}
52
+ it_behaves_like "an input path"
53
+ end
54
+
55
+ context "and there is a custom keys input converter" do
56
+ let(:options) {{start_key: 'yay.upcase.path', convert_input_keys: Upcasing}}
57
+ subject {RestModel::Key.new(:name, options)}
58
+ let(:path) {['YAY', 'UPCASE', 'PATH']}
59
+ it_behaves_like "an input path"
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,85 @@
1
+ require 'spec_helper'
2
+
3
+ describe RestModel::Source::Retriever do
4
+ describe '.parse' do
5
+ it 'parses simple properties' do
6
+ class Example < RestModel
7
+ property :login
8
+ end
9
+
10
+ models = Example.parse({login: 'jackiechan2010'})
11
+ models.should be_an(Array)
12
+ models.first.login.should == 'jackiechan2010'
13
+ end
14
+
15
+ it 'uses different field name if defined' do
16
+ class Example < RestModel
17
+ property :login, field: :customer_login
18
+ end
19
+
20
+ models = Example.parse({customer_login: 'jackiechan2010'})
21
+ models.should be_an(Array)
22
+ models.first.login.should == 'jackiechan2010'
23
+ end
24
+
25
+ context "uses type defined" do
26
+ context 'boolean' do
27
+ before do
28
+ class Example < RestModel
29
+ property :can_merge, type: Boolean
30
+ end
31
+ end
32
+
33
+ it "returns true when value is 'X'" do
34
+ models = Example.parse({can_merge: 'X'})
35
+ models.first.can_merge.should be_true
36
+ end
37
+
38
+ it "returns false when value isn't 'X'" do
39
+ models = Example.parse({can_merge: ''})
40
+ models.first.can_merge.should be_false
41
+ end
42
+ end
43
+
44
+ context "date" do
45
+ it "parse" do
46
+ class Example < RestModel
47
+ property :issue_date, type: Date
48
+ end
49
+
50
+ models = Example.parse({issue_date: '2011-01-01'})
51
+ models.first.issue_date.should == Date.parse('2011-01-01')
52
+ end
53
+ end
54
+
55
+ context "float" do
56
+ it "parse" do
57
+ class Example < RestModel
58
+ property :amount, type: Float
59
+ end
60
+
61
+ models = Example.parse({amount: '1234.5'})
62
+ models.first.amount.should == 1234.5
63
+ end
64
+ end
65
+ end
66
+
67
+ context "maps values if defined" do
68
+ before do
69
+ class Example < RestModel
70
+ property :status, values: {paid: '01', unpaid: '02'}
71
+ end
72
+ end
73
+
74
+ it "returns key when value was mapped" do
75
+ models = Example.parse({status: '02'})
76
+ models.first.status.should == :unpaid
77
+ end
78
+
79
+ it "returns nil when value wasn't mapped" do
80
+ models = Example.parse({status: '03'})
81
+ models.first.status.should be_nil
82
+ end
83
+ end
84
+ end
85
+ end