rest_model 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/Gemfile +2 -0
- data/Guardfile +7 -0
- data/README.md +237 -0
- data/Rakefile +2 -0
- data/examples/all.rb +12 -0
- data/examples/embeds_many/invisible.rb +16 -0
- data/examples/embeds_many/simple.rb +13 -0
- data/examples/embeds_many/with_array.rb +10 -0
- data/examples/embeds_many/with_class_name.rb +13 -0
- data/examples/embeds_many/with_fields.rb +9 -0
- data/examples/embeds_many/with_if.rb +16 -0
- data/examples/embeds_many/with_nil_value.rb +12 -0
- data/examples/embeds_many/with_start_key.rb +13 -0
- data/examples/embeds_one/flattened.rb +12 -0
- data/examples/embeds_one/simple.rb +12 -0
- data/examples/embeds_one/with_class_name.rb +12 -0
- data/examples/embeds_one/with_if.rb +16 -0
- data/examples/embeds_one/with_start_key.rb +12 -0
- data/examples/has_many/simple.rb +28 -0
- data/examples/helper.rb +9 -0
- data/examples/initialize/embeds_many.rb +25 -0
- data/examples/initialize/embeds_many_array.rb +9 -0
- data/examples/initialize/embeds_one.rb +19 -0
- data/examples/initialize/simple.rb +8 -0
- data/examples/properties/array_serialization.rb +8 -0
- data/examples/properties/collections.rb +11 -0
- data/examples/properties/simple.rb +8 -0
- data/examples/properties/with_field.rb +8 -0
- data/examples/properties/with_field_path.rb +8 -0
- data/examples/properties/with_id.rb +8 -0
- data/examples/properties/with_if.rb +21 -0
- data/examples/properties/with_key_converter.rb +18 -0
- data/examples/properties/with_two_key_converters.rb +27 -0
- data/examples/properties/with_values.rb +10 -0
- data/examples/summarization/simple.rb +21 -0
- data/examples/to_input/embeds_many.rb +25 -0
- data/examples/to_input/embeds_many_without_key.rb +31 -0
- data/examples/to_input/embeds_one.rb +13 -0
- data/examples/to_input/embeds_one_without_key.rb +23 -0
- data/examples/to_input/flattened.rb +12 -0
- data/examples/to_input/serializables.rb +19 -0
- data/examples/to_input/simple.rb +8 -0
- data/examples/to_input/with_field.rb +8 -0
- data/examples/to_input/with_field_path.rb +8 -0
- data/examples/to_input/with_fields.rb +8 -0
- data/examples/to_input/without_key.rb +11 -0
- data/examples/to_input/without_nil.rb +11 -0
- data/lib/rest_model/configuration.rb +45 -0
- data/lib/rest_model/key/association.rb +42 -0
- data/lib/rest_model/key/builder.rb +30 -0
- data/lib/rest_model/key/embeddable/builder.rb +19 -0
- data/lib/rest_model/key/embeddable/response.rb +25 -0
- data/lib/rest_model/key/embeddable/retriever.rb +20 -0
- data/lib/rest_model/key/embeddable/sender.rb +41 -0
- data/lib/rest_model/key/embeddable.rb +29 -0
- data/lib/rest_model/key/href/response.rb +10 -0
- data/lib/rest_model/key/href.rb +10 -0
- data/lib/rest_model/key/property/builder.rb +27 -0
- data/lib/rest_model/key/property/response.rb +9 -0
- data/lib/rest_model/key/property/retriever.rb +24 -0
- data/lib/rest_model/key/property/sender.rb +41 -0
- data/lib/rest_model/key/property.rb +22 -0
- data/lib/rest_model/key/relation/builder.rb +24 -0
- data/lib/rest_model/key/relation/response.rb +38 -0
- data/lib/rest_model/key/relation.rb +23 -0
- data/lib/rest_model/key.rb +44 -0
- data/lib/rest_model/response.rb +42 -0
- data/lib/rest_model/serialization/boolean.rb +48 -0
- data/lib/rest_model/serialization/date.rb +19 -0
- data/lib/rest_model/serialization/enumerable.rb +15 -0
- data/lib/rest_model/serialization/float.rb +17 -0
- data/lib/rest_model/serialization/integer.rb +17 -0
- data/lib/rest_model/serialization/string.rb +13 -0
- data/lib/rest_model/source/path.rb +25 -0
- data/lib/rest_model/source/retriever.rb +48 -0
- data/lib/rest_model/source/sender.rb +38 -0
- data/lib/rest_model/version.rb +3 -0
- data/lib/rest_model.rb +101 -0
- data/rest_model.gemspec +26 -0
- data/spec/.DS_Store +0 -0
- data/spec/integration/embeds_many_spec.rb +57 -0
- data/spec/integration/embeds_one_spec.rb +41 -0
- data/spec/integration/has_many_spec.rb +27 -0
- data/spec/integration/property_spec.rb +69 -0
- data/spec/integration/summarization_spec.rb +12 -0
- data/spec/spec_helper.rb +26 -0
- data/spec/support/examples.rb +29 -0
- data/spec/support/out.rb +17 -0
- data/spec/support/shared_examples.rb +20 -0
- data/spec/unit/configuration_spec.rb +24 -0
- data/spec/unit/key/association_spec.rb +69 -0
- data/spec/unit/key/embeddable/builder_spec.rb +26 -0
- data/spec/unit/key/embeddable/response_spec.rb +55 -0
- data/spec/unit/key/embeddable/retriever_spec.rb +38 -0
- data/spec/unit/key/embeddable_spec.rb +7 -0
- data/spec/unit/key/property/builder_spec.rb +154 -0
- data/spec/unit/key/property/response_spec.rb +22 -0
- data/spec/unit/key/property/retriever_spec.rb +31 -0
- data/spec/unit/key/property_spec.rb +39 -0
- data/spec/unit/key/relation/builder_spec.rb +53 -0
- data/spec/unit/key/relation/response_spec.rb +105 -0
- data/spec/unit/key/relation_spec.rb +71 -0
- data/spec/unit/key_spec.rb +101 -0
- data/spec/unit/response_spec.rb +44 -0
- data/spec/unit/rest_model_spec.rb +46 -0
- data/spec/unit/serialization/boolean_spec.rb +25 -0
- data/spec/unit/serialization/date_spec.rb +13 -0
- data/spec/unit/serialization/float_spec.rb +13 -0
- data/spec/unit/serialization/integer_spec.rb +13 -0
- data/spec/unit/serialization/string_spec.rb +11 -0
- data/spec/unit/source/path_spec.rb +62 -0
- data/spec/unit/source/retriever_spec.rb +85 -0
- metadata +260 -0
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
def it_parses_property
|
4
|
+
it 'parses property' do
|
5
|
+
root.login.should == 'jackiechan2010'
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
describe_example 'properties/simple' do
|
10
|
+
it_parses_property
|
11
|
+
end
|
12
|
+
|
13
|
+
describe_example 'properties/with_field' do
|
14
|
+
context 'when input has a different name for the property' do
|
15
|
+
it_parses_property
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe_example 'properties/with_field_path' do
|
20
|
+
context 'when input has a different path for the property' do
|
21
|
+
it_parses_property
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe_example 'properties/with_id' do
|
26
|
+
context 'when property is an id' do
|
27
|
+
it 'parses id' do
|
28
|
+
root.id.should == "2000"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe_example 'properties/with_if' do
|
34
|
+
context 'when a property has a conditional proc (:if)' do
|
35
|
+
context 'and it evaluates to true' do
|
36
|
+
it 'parses property' do
|
37
|
+
root_with_description.description.should == "description"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'and it evaluates to false' do
|
42
|
+
it "doesn't parse property" do
|
43
|
+
root_without_description.description.should_not be
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe_example 'properties/with_key_converter' do
|
50
|
+
after do
|
51
|
+
RestModel::Configuration.configure do |c|
|
52
|
+
c.convert_input_keys = nil
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
context 'when a diferent key converter is configured on rest model' do
|
57
|
+
it_parses_property
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe_example 'properties/with_two_key_converters' do
|
62
|
+
context 'when key converters are configured for each class' do
|
63
|
+
it_parses_property
|
64
|
+
|
65
|
+
it 'parses other property with different key converter' do
|
66
|
+
root.product.unit_price.should == 29.9
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe_example 'summarization/simple' do
|
4
|
+
subject {root[:entries].first}
|
5
|
+
|
6
|
+
it 'summarizes resources' do
|
7
|
+
subject.keys.count.should == 3
|
8
|
+
subject[:id].should == '138911938'
|
9
|
+
subject[:login].should == 'jackiechan2010'
|
10
|
+
subject[:href].should == 'http://example.com/customers/138911938'
|
11
|
+
end
|
12
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
$:.push '../lib'
|
2
|
+
|
3
|
+
require 'rest_model'
|
4
|
+
require 'ostruct'
|
5
|
+
require 'json'
|
6
|
+
require 'stringio'
|
7
|
+
require 'active_support/core_ext/hash/indifferent_access'
|
8
|
+
|
9
|
+
require 'support/shared_examples'
|
10
|
+
require 'support/examples'
|
11
|
+
require 'support/out'
|
12
|
+
|
13
|
+
RSpec.configure do |config|
|
14
|
+
config.before do
|
15
|
+
[:Example, :ExampleChild].each do |klass|
|
16
|
+
Object.send(:remove_const, klass) if Object.const_defined? klass
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
include Output
|
22
|
+
include Examples
|
23
|
+
|
24
|
+
RestModel::Configuration.configure do |c|
|
25
|
+
c.host = 'http://example.com'
|
26
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Examples
|
2
|
+
def describe_example(file, &block)
|
3
|
+
describe "example #{file}" do
|
4
|
+
[:Root, :Item, :Customer, :Entry].each do |klass|
|
5
|
+
Examples.send(:remove_const, klass) if Examples.const_defined?(klass)
|
6
|
+
end
|
7
|
+
|
8
|
+
# silently {
|
9
|
+
|
10
|
+
eval File.read("examples/#{file}.rb")
|
11
|
+
|
12
|
+
# }
|
13
|
+
|
14
|
+
variables = instance_variables.reject {|var| var.to_s =~ /metadata$/}
|
15
|
+
|
16
|
+
values = variables.inject({}) do |buffer, name|
|
17
|
+
method_name = name.to_s.gsub(/@/, "")
|
18
|
+
let(method_name) {instance_variable_get(name)}
|
19
|
+
buffer.merge method_name => instance_variable_get(name)
|
20
|
+
end
|
21
|
+
|
22
|
+
before :all do
|
23
|
+
values.each {|name, value| instance_variable_set "@#{name}", value}
|
24
|
+
end
|
25
|
+
|
26
|
+
instance_eval &block if block
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/spec/support/out.rb
ADDED
@@ -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
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe RestModel::Configuration do
|
4
|
+
after(:all) do
|
5
|
+
RestModel::Configuration.convert_input_keys = nil
|
6
|
+
end
|
7
|
+
|
8
|
+
context "when no custom input keys converter is set" do
|
9
|
+
it "returns default key converter" do
|
10
|
+
default_handler = RestModel::Configuration::DefaultHandler
|
11
|
+
RestModel::Configuration.convert_input_keys.should == default_handler
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context "when a custom input keys converter is set" do
|
16
|
+
let(:custom_convert_input_keys) {lambda {|keys| keys}}
|
17
|
+
|
18
|
+
before {RestModel::Configuration.convert_input_keys = custom_convert_input_keys}
|
19
|
+
|
20
|
+
it "returns custom key converter" do
|
21
|
+
RestModel::Configuration.convert_input_keys.should == custom_convert_input_keys
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe RestModel::Association do
|
4
|
+
describe "#initialize" do
|
5
|
+
context "class_name" do
|
6
|
+
context "when class_name option is passed" do
|
7
|
+
subject do
|
8
|
+
RestModel::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 {RestModel::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
|
+
RestModel::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
|
+
RestModel::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
|
+
RestModel::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,26 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe RestModel::Embeddable::Builder do
|
4
|
+
before do
|
5
|
+
class Example < RestModel
|
6
|
+
embeds_one :contract, some_option: 'contract options'
|
7
|
+
embeds_many :invoice_items, some_option: 'invoice options'
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe ".embeds_one" do
|
12
|
+
let(:field) {:contract}
|
13
|
+
let(:many) {false}
|
14
|
+
let(:options) {{some_option: 'contract options', many: many}}
|
15
|
+
|
16
|
+
it_behaves_like "an association"
|
17
|
+
end
|
18
|
+
|
19
|
+
describe ".embeds_many" do
|
20
|
+
let(:field) {:invoice_items}
|
21
|
+
let(:many) {true}
|
22
|
+
let(:options) {{some_option: 'invoice options', many: many}}
|
23
|
+
|
24
|
+
it_behaves_like "an association"
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe RestModel::Embeddable::Response do
|
4
|
+
before do
|
5
|
+
class ExampleChild < RestModel
|
6
|
+
property :id
|
7
|
+
end
|
8
|
+
|
9
|
+
class Example < RestModel
|
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 visible on resource" do
|
21
|
+
it "returns an empty hash" do
|
22
|
+
subject.should_receive(:visible?).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
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe RestModel::Embeddable::Retriever do
|
4
|
+
context "when it embeds one" do
|
5
|
+
before do
|
6
|
+
class ExampleChild < RestModel
|
7
|
+
property :id
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
let(:value) {{id: "7000"}}
|
12
|
+
|
13
|
+
subject {RestModel::Embeddable.new(:example_child, many: false, start_key: "")}
|
14
|
+
|
15
|
+
it "parses child" do
|
16
|
+
child = subject.from_source(value)
|
17
|
+
child.id.should == "7000"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context "when it embeds many" do
|
22
|
+
before do
|
23
|
+
class ExampleChild < RestModel
|
24
|
+
property :id
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
let(:value) {{example_children: [{id: "7000"}, {id: "7001"}]}}
|
29
|
+
|
30
|
+
subject {RestModel::Embeddable.new(:example_children, many: true)}
|
31
|
+
|
32
|
+
it "parses children" do
|
33
|
+
children = subject.from_source(value)
|
34
|
+
children[0].id.should == "7000"
|
35
|
+
children[1].id.should == "7001"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,154 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe RestModel::Property::Builder 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 < RestModel
|
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 < RestModel
|
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 < RestModel
|
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 < RestModel
|
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 < RestModel
|
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 < RestModel
|
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 < RestModel
|
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 < RestModel
|
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,22 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe RestModel::Property::Response do
|
4
|
+
class Customer
|
5
|
+
attr_accessor :login
|
6
|
+
end
|
7
|
+
|
8
|
+
subject {RestModel::Property.new(:login)}
|
9
|
+
|
10
|
+
it "returns a pair with property name and value" do
|
11
|
+
customer = Customer.new.tap {|c| c.login = "jackiechan2010"}
|
12
|
+
subject.to_resource(customer).should == {login: "jackiechan2010"}
|
13
|
+
end
|
14
|
+
|
15
|
+
context "when this key shouldn't be visible on resource" do
|
16
|
+
it "returns an empty hash" do
|
17
|
+
subject.should_receive(:visible?).and_return false
|
18
|
+
customer = Customer.new.tap {|c| c.login = "jackiechan2010"}
|
19
|
+
subject.to_resource(customer).should == {}
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe RestModel::Property::Retriever do
|
4
|
+
subject {RestModel::Property.new(:login)}
|
5
|
+
|
6
|
+
let(:item) {{login: "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(item[:login]).and_return(item[:login])
|
15
|
+
subject.from_source(item)
|
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
|
+
RestModel::Property.new(:payment_status, options)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "maps value" do
|
27
|
+
serializer_mock.should_receive(:serialize).with("02").and_return "02"
|
28
|
+
subject.from_source({payment_status: "02"}).should == :unpaid
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe RestModel::Property do
|
4
|
+
describe "#initialize" do
|
5
|
+
context "serializer" do
|
6
|
+
context "when custom is provided" do
|
7
|
+
subject do
|
8
|
+
options = {type: RestModel::Serialization::Boolean}
|
9
|
+
RestModel::Property.new(:haz_bool?, options)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "configures custom" do
|
13
|
+
subject.serializer.should == RestModel::Serialization::Boolean
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
context "when it isn't provided" do
|
18
|
+
subject {RestModel::Property.new(:login)}
|
19
|
+
|
20
|
+
it "uses String serializer" do
|
21
|
+
subject.serializer.should == RestModel::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
|
+
RestModel::Property.new(:payment_status, options)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "configures values" do
|
35
|
+
subject.translations.should == values
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|