json-crud-api 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/json-crud-api/presenter.rb +22 -13
- data/spec/unit/presenter_spec.rb +60 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b011194209c43c09fab09e549d9e88b12854f4ff
|
4
|
+
data.tar.gz: 7a62d339247b3a0bc48b6f2ee96a34392d307219
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b4d3d94ff25dc21acc2fa5f963d0f20ae87a5704a680dcd10e9996e52b223325438d2da5e6794b760a2e0f8362fa597da18fa27fa27f8b828ca2a0c6dd0f7e9d
|
7
|
+
data.tar.gz: 94dfbd31daa4d202dae135edd442dd0ca9bcc180e56bcff55332fd43bcf5b588f450a284b6568f136aaa0f948eeeaf352e4fbac3d30d22dfb1b9e28159bdeb38
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module JsonCrudApi
|
2
2
|
class Presenter
|
3
3
|
|
4
|
-
attr_accessor :model, :exclude
|
4
|
+
attr_accessor :model, :include, :exclude
|
5
5
|
|
6
6
|
def initialize(options)
|
7
7
|
@model = options[:model]
|
@@ -13,25 +13,34 @@ module JsonCrudApi
|
|
13
13
|
def render(data, operation = nil)
|
14
14
|
return data.map {|d| render(d, operation) } if data.is_a?(Array)
|
15
15
|
|
16
|
-
|
17
|
-
unless @exclude.nil? or @exclude[:render].nil?
|
18
|
-
properties -= @exclude[:render][:all] unless @exclude[:render][:all].nil?
|
19
|
-
properties -= @exclude[:render][operation] unless @exclude[:render][operation].nil?
|
20
|
-
end
|
21
|
-
|
22
|
-
Hash[properties.map { |p| [p, data.send(p)] }]
|
16
|
+
Hash[get_properties(:render, operation).map { |p| [p, data.send(p)] }]
|
23
17
|
end
|
24
18
|
|
25
19
|
def parse(data, operation = nil)
|
26
20
|
return data.map {|d| parse(d, operation) } if data.is_a?(Array)
|
27
21
|
|
22
|
+
Hash[get_properties(:parse, operation).map { |p| [p,data[p]] }]
|
23
|
+
end
|
24
|
+
|
25
|
+
def get_properties(method, operation)
|
28
26
|
properties = @model.properties.map { |p| p.name.to_sym }
|
29
|
-
unless @exclude.nil?
|
30
|
-
properties -= @exclude[:
|
31
|
-
properties -= @exclude[
|
27
|
+
unless @exclude.nil?
|
28
|
+
properties -= @exclude[:all] unless @exclude[:all].nil?
|
29
|
+
properties -= @exclude[operation] unless @exclude[operation].nil?
|
30
|
+
unless @exclude[method].nil?
|
31
|
+
properties -= @exclude[method][:all] unless @exclude[method][:all].nil?
|
32
|
+
properties -= @exclude[method][operation] unless @exclude[method][operation].nil?
|
33
|
+
end
|
32
34
|
end
|
33
|
-
|
34
|
-
|
35
|
+
unless @include.nil?
|
36
|
+
properties += @include[:all] unless @include[:all].nil?
|
37
|
+
properties += @include[operation] unless @include[operation].nil?
|
38
|
+
unless @include[method].nil?
|
39
|
+
properties += @include[method][:all] unless @include[method][:all].nil?
|
40
|
+
properties += @include[method][operation] unless @include[method][operation].nil?
|
41
|
+
end
|
42
|
+
end
|
43
|
+
properties
|
35
44
|
end
|
36
45
|
|
37
46
|
end
|
data/spec/unit/presenter_spec.rb
CHANGED
@@ -55,6 +55,46 @@ describe JsonCrudApi::Presenter do
|
|
55
55
|
@presenter.render(data).should eq([{ :one => "Test" }, { :one => "TEST2" }])
|
56
56
|
end
|
57
57
|
|
58
|
+
it 'should include render:all properties' do
|
59
|
+
@presenter.include = { :render => { :all => [:five] } }
|
60
|
+
@mock_model.stub :properties do
|
61
|
+
[OpenStruct.new(:name => :two)]
|
62
|
+
end
|
63
|
+
data = OpenStruct.new(:two => "Two",:five=>"Five")
|
64
|
+
|
65
|
+
@presenter.render(data).should eq({ :two => "Two", :five=>"Five" })
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'should include global:all properties' do
|
69
|
+
@presenter.include = { :all => [:five] }
|
70
|
+
@mock_model.stub :properties do
|
71
|
+
[ OpenStruct.new(:name => :two)]
|
72
|
+
end
|
73
|
+
data = OpenStruct.new(:two => "Two",:five=>"Five")
|
74
|
+
|
75
|
+
@presenter.render(data).should eq({ :two => "Two", :five=>"Five" })
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'should include global:operation properties' do
|
79
|
+
@presenter.include = { :test => [:five] }
|
80
|
+
@mock_model.stub :properties do
|
81
|
+
[ OpenStruct.new(:name => :two)]
|
82
|
+
end
|
83
|
+
data = OpenStruct.new(:two => "Two",:five=>"Five")
|
84
|
+
|
85
|
+
@presenter.render(data, :test).should eq({ :two => "Two", :five=>"Five" })
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'should include render:operation properties' do
|
89
|
+
@presenter.include = { :render => { :test => [:five] } }
|
90
|
+
@mock_model.stub :properties do
|
91
|
+
[ OpenStruct.new(:name => :two)]
|
92
|
+
end
|
93
|
+
data = OpenStruct.new(:two => "Two",:five=>"Five")
|
94
|
+
|
95
|
+
@presenter.render(data, :test).should eq({ :two => "Two", :five=>"Five" })
|
96
|
+
end
|
97
|
+
|
58
98
|
it 'should exclude render:all properties' do
|
59
99
|
@presenter.exclude = { :render => { :all => [:one] } }
|
60
100
|
@mock_model.stub :properties do
|
@@ -65,6 +105,16 @@ describe JsonCrudApi::Presenter do
|
|
65
105
|
@presenter.render(data).should eq({ :two => "Two" })
|
66
106
|
end
|
67
107
|
|
108
|
+
it 'should exclude global:all properties' do
|
109
|
+
@presenter.exclude = { :all => [:one] }
|
110
|
+
@mock_model.stub :properties do
|
111
|
+
[OpenStruct.new(:name => :one), OpenStruct.new(:name => :two)]
|
112
|
+
end
|
113
|
+
data = OpenStruct.new(:one => "Test",:two => "Two")
|
114
|
+
|
115
|
+
@presenter.render(data).should eq({ :two => "Two" })
|
116
|
+
end
|
117
|
+
|
68
118
|
it 'should exclude render:operation properties' do
|
69
119
|
@presenter.exclude = { :render => { :test => [:one] } }
|
70
120
|
@mock_model.stub :properties do
|
@@ -75,6 +125,16 @@ describe JsonCrudApi::Presenter do
|
|
75
125
|
@presenter.render(data, :test).should eq({ :two => "Two" })
|
76
126
|
end
|
77
127
|
|
128
|
+
it 'should exclude global:operation properties' do
|
129
|
+
@presenter.exclude = { :test => [:one] }
|
130
|
+
@mock_model.stub :properties do
|
131
|
+
[OpenStruct.new(:name => :one), OpenStruct.new(:name => :two)]
|
132
|
+
end
|
133
|
+
data = OpenStruct.new(:one => "Test",:two => "Two")
|
134
|
+
|
135
|
+
@presenter.render(data, :test).should eq({ :two => "Two" })
|
136
|
+
end
|
137
|
+
|
78
138
|
it 'should exclude combinations of render:all and render:operation properties' do
|
79
139
|
@presenter.exclude = { :render => { :all => [:two] , :test => [:one] } }
|
80
140
|
@mock_model.stub :properties do
|