render 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MDFmMmYyYWI1MWI3ODE2YjRkNDAwOGEzODAwYWVkYTBiOTYwZjhkOA==
5
+ data.tar.gz: !binary |-
6
+ ZTI2MzAwODM2ODAwOGQ3OWM5MmI2ODViNmIxMTg5YmJkM2IwOWY0ZQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ NzFjZWJiMDVjMjNkZTczODBhZTA3OWRiOGEzYTJhYjc3YzBkZTI3OWRkYzUz
10
+ NDUzZjMxYzBiYjM5MzAxM2Q4YzVjYjVjYzlmYjIwNjhhOTVlNGQwOWRjYzhk
11
+ NTJkN2UzYjAwMzBjMDE3MGMxNTllYzVhYjQwMTI5NGRlMzRlM2E=
12
+ data.tar.gz: !binary |-
13
+ MjJlZGMxYmNhMWMxMmU1ZjdlYWI2ODMxMWU0M2M1ZDJlM2U2NGZlZTgyYTlh
14
+ ZDgxOGNjOTVhODk4YzNlZjA4YmI3NTA1ZjVmZTYxZjk1OGE1NDFlYjljY2Jm
15
+ ZTUxYzU3NmM1NzdkNmQ4ZjMwMzY1ZDFjZDZmNmI0MjFjMWZkZjk=
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- render (0.0.3)
4
+ render (0.0.4)
5
5
  uuid (= 2.3.7)
6
6
 
7
7
  GEM
@@ -10,7 +10,7 @@ module Render
10
10
  super
11
11
 
12
12
  self.name = options.fetch(:title, :render_array_attribute_untitled).to_sym
13
- options = options[:items]
13
+ options = options.fetch(:items)
14
14
  self.type = Render.parse_type(options[:type])
15
15
  self.format = Render.parse_type(options[:format]) rescue nil
16
16
  self.enums = options[:enum]
@@ -44,8 +44,7 @@ module Render
44
44
  end
45
45
 
46
46
  def lower_limit
47
- # lower_limit = (required ? 1 : 0)
48
- 1
47
+ required ? 1 : 0
49
48
  end
50
49
 
51
50
  end
@@ -19,6 +19,12 @@ module Render
19
19
  self.required = false
20
20
  end
21
21
 
22
+ def generator
23
+ @generator ||= Render.generators.detect do |generator|
24
+ generator.type == type && name.match(generator.matcher)
25
+ end
26
+ end
27
+
22
28
  def bias_type
23
29
  format || type
24
30
  end
@@ -33,29 +39,21 @@ module Render
33
39
 
34
40
  private
35
41
 
36
- # TODO implement better #faux_value
37
42
  def faux_value
38
43
  return enums.sample if enums
39
- return generator_value if generator_value # todo optimize generator_value call
44
+ return generator.algorithm.call if generator
40
45
 
41
46
  case(bias_type.name)
42
- when("String") then "A String"
47
+ when("String") then "#{name} (generated)"
43
48
  when("Integer") then rand(1000)
44
49
  when("UUID") then UUID.generate
45
50
  when("Boolean") then [true, false].sample
46
- when("Float") then rand(0.1..99)
51
+ when("Float") then rand(0.1..99).round(2)
47
52
  when("Time")
48
53
  time = Time.now
49
54
  (type == String) ? time.to_s : time
50
55
  end
51
56
  end
52
57
 
53
- def generator_value
54
- generator = Render.generators.detect do |generator|
55
- generator.type == type && name.match(generator.matcher)
56
- end
57
- generator.algorithm.call if generator
58
- end
59
58
  end
60
-
61
59
  end
data/lib/render/graph.rb CHANGED
@@ -49,12 +49,18 @@ module Render
49
49
  uri.to_s
50
50
  end
51
51
 
52
- def render(inherited_properties = {})
52
+ def render(inherited_properties = nil)
53
53
  self.inherited_data = inherited_properties
54
+ if (schema.type == Array)
55
+ explicit_data = inherited_data
56
+ else
57
+ explicit_data = inherited_data.is_a?(Hash) ? inherited_data : {}
58
+ explicit_data = explicit_data.merge!(relationship_data_from_parent)
59
+ end
54
60
 
55
61
  graph_data = DottableHash.new
56
- inherited_data = relationship_data_from_parent.merge({ endpoint: endpoint })
57
- rendered_data = schema.render!(inherited_data) do |parent_data|
62
+
63
+ rendered_data = schema.render!(explicit_data, endpoint) do |parent_data|
58
64
  loop_with_configured_threading(graphs) do |graph|
59
65
  if parent_data.is_a?(Array)
60
66
  graph_data[graph.title] = parent_data.inject([]) do |nested_data, element|
@@ -119,7 +125,7 @@ module Render
119
125
  if !inherited_data.is_a?(Hash)
120
126
  return inherited_data
121
127
  elsif (child_key == key)
122
- return inherited_data.fetch(parent_key)
128
+ return inherited_data.fetch(parent_key, nil)
123
129
  end
124
130
  end
125
131
  nil
@@ -24,12 +24,17 @@ module Render
24
24
  self.schema = Schema.new(schema_options.merge(options))
25
25
  end
26
26
 
27
- def serialize(explicit_value)
27
+ def serialize(explicit_value, maintain_nil = false)
28
28
  if !!schema
29
29
  value = schema.serialize!(explicit_value)
30
30
  { name.to_sym => value }
31
31
  else
32
- value = (explicit_value || default_value)
32
+ if (maintain_nil && !explicit_value)
33
+ value = explicit_value
34
+ else
35
+ value = (explicit_value || default_value)
36
+ end
37
+
33
38
  { name.to_sym => value }
34
39
  end
35
40
  end
data/lib/render/schema.rb CHANGED
@@ -49,15 +49,16 @@ module Render
49
49
  self.serialized_data = hash_attributes.inject({}) do |processed_explicit_data, attribute|
50
50
  explicit_data ||= {}
51
51
  value = explicit_data.fetch(attribute.name, nil)
52
- serialized_attribute = attribute.serialize(value)
52
+ maintain_nil = explicit_data.has_key?(attribute.name)
53
+
54
+ serialized_attribute = attribute.serialize(value, maintain_nil)
53
55
  processed_explicit_data.merge!(serialized_attribute)
54
56
  end
55
57
  end
56
58
  end
57
59
 
58
- def render!(options_and_explicit_data = nil)
59
- endpoint = options_and_explicit_data.delete(:endpoint) if options_and_explicit_data.is_a?(Hash)
60
- self.raw_data = Render.live ? request(endpoint) : options_and_explicit_data
60
+ def render!(explicit_data = nil, endpoint = nil)
61
+ self.raw_data = Render.live ? request(endpoint) : explicit_data
61
62
  serialize!(raw_data)
62
63
  yield serialized_data if block_given?
63
64
  self.rendered_data = DottableHash.new(hash_with_title_prefixes(serialized_data))
@@ -1,3 +1,3 @@
1
1
  module Render
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -2,14 +2,12 @@ module Render
2
2
  describe Attribute do
3
3
  context "generators" do
4
4
  before(:each) do
5
- @original_generators = Render.generators
6
- @original_live = Render.live
7
- Render.live = false
5
+ @original_generators = Render.generators.dup
6
+ Render.stub({ live: false })
8
7
  end
9
8
 
10
9
  after(:each) do
11
10
  Render.generators = @original_generators
12
- Render.live = @original_live
13
11
  end
14
12
 
15
13
  it "uses matching generator for #faux_value" do
@@ -20,9 +18,9 @@ module Render
20
18
  HashAttribute.new({ name: { type: String } }).default_value.should == name
21
19
  end
22
20
 
23
- it "uses really bare-boned type if no generator is found" do
24
- bare_boned_string = "A String"
25
- HashAttribute.new({ foo: { type: String } }).default_value.should == bare_boned_string
21
+ it "uses bare-boned type if no generator is found" do
22
+ bare_boned_string = "the_attribute_name (generated)"
23
+ HashAttribute.new({ the_attribute_name: { type: String } }).default_value.should == bare_boned_string
26
24
  end
27
25
  end
28
26
  end
@@ -5,5 +5,65 @@ module Render
5
5
  describe "relationships" do
6
6
  #
7
7
  end
8
+
9
+ context "not live" do
10
+ describe "#render" do
11
+ before(:each) do
12
+ Render.stub({ live: false })
13
+ @hash_definition = {
14
+ title: :book,
15
+ type: Object,
16
+ properties: {
17
+ title: { type: String }
18
+ }
19
+ }
20
+ @array_definition = {
21
+ title: :books,
22
+ type: Array,
23
+ items: {
24
+ type: UUID
25
+ }
26
+ }
27
+ end
28
+
29
+ it "generates random number of array elements" do
30
+ graph = Graph.new(@array_definition)
31
+ generated_book_sizes = 5.times.collect { graph.render.books.size }
32
+ generated_book_sizes.compact.size.should > 1
33
+ end
34
+
35
+ context "explicit data" do
36
+ it "uses explicit data for hashes" do
37
+ graph = Render::Graph.new(@hash_definition)
38
+ green_eggs_and_ham = "Green Eggs and Ham"
39
+ data = graph.render({ title: green_eggs_and_ham })
40
+ data.book.title.should == green_eggs_and_ham
41
+ end
42
+
43
+ it "uses explicit nil data for hashes" do
44
+ graph = Render::Graph.new(@hash_definition)
45
+ data = graph.render({ title: nil })
46
+ data.book.title.should == nil
47
+ end
48
+
49
+ it "uses explicit data for arrays" do
50
+ graph = Render::Graph.new(@array_definition)
51
+ id = UUID.generate
52
+ graph.render([id]).books.should == [id]
53
+ graph.render([]).books.should == []
54
+ end
55
+
56
+ it "uses explicit data for nested data" do
57
+ @array_definition[:items] = @hash_definition
58
+ nested_graph = Graph.new(@array_definition)
59
+ tell_tale_heart = "The Tell-Tale Heart"
60
+ data = nested_graph.render([{ title: tell_tale_heart }])
61
+ data.books.size.should == 1
62
+ data.books.first.title.should == tell_tale_heart
63
+ end
64
+
65
+ end
66
+ end
67
+ end
8
68
  end
9
69
  end
@@ -38,8 +38,14 @@ module Render
38
38
  end
39
39
 
40
40
  it "generates fake number of elements" do
41
+ lower_limit = 6
42
+ upper_limit = 9
43
+ @attribute.stub({ lower_limit: lower_limit })
44
+ stub_const("Render::ArrayAttribute::FAUX_DATA_UPPER_LIMIT", upper_limit)
45
+
41
46
  faux_data = @attribute.serialize
42
- faux_data.size.should be_between(0, ArrayAttribute::FAUX_DATA_UPPER_LIMIT)
47
+ faux_data.size.should >= lower_limit
48
+ faux_data.size.should <= upper_limit
43
49
  faux_data.sample.class.should == Float
44
50
  end
45
51
  end
@@ -0,0 +1,17 @@
1
+ module Render
2
+ describe Attribute do
3
+ describe "#default_value" do
4
+ before(:each) do
5
+ Render.stub({ live: false })
6
+ end
7
+
8
+ describe "String" do
9
+ it "uses attribute name for context" do
10
+ name = "some identifier"
11
+ faux_value = HashAttribute.new({ name => { type: String } }).default_value
12
+ faux_value.should include(name)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -7,7 +7,7 @@ module Render
7
7
  before(:each) do
8
8
  Render.stub({ live: false })
9
9
  @definition = double(:definition)
10
- @schema = double(:schema)
10
+ @schema = double(:schema, { type: Hash })
11
11
  Schema.stub(:new).with(@definition).and_return(@schema)
12
12
  end
13
13
 
@@ -108,7 +108,7 @@ module Render
108
108
  client_id = UUID.generate
109
109
  graph = Graph.new(@definition, { endpoint: endpoint, client_id: client_id })
110
110
 
111
- @schema.should_receive(:render!).with({ endpoint: graph.endpoint }).and_return({})
111
+ @schema.should_receive(:render!).with(anything, graph.endpoint).and_return({})
112
112
  graph.render
113
113
  end
114
114
 
@@ -146,9 +146,8 @@ module Render
146
146
  film_data = { director_id: @director_id }
147
147
  @film_schema.should_receive(:render!).and_yield(film_data).and_return(film_data)
148
148
 
149
- @director_schema.should_receive(:render!).with do |args|
150
- args[:endpoint].should == "http://endpoint.local/directors/#{@director_id}"
151
- end.and_return({})
149
+ endpoint = "http://endpoint.local/directors/#{@director_id}"
150
+ @director_schema.should_receive(:render!).with(anything, endpoint).and_return({})
152
151
  film.render
153
152
  end
154
153
 
@@ -86,12 +86,23 @@ module Render
86
86
  end
87
87
  end
88
88
 
89
- it "uses faux data when offline" do
90
- type = [String, Integer].sample
91
- Render.stub({ live: false })
89
+ context "offline" do
90
+ before(:each) do
91
+ Render.stub({ live: false })
92
+ end
93
+
94
+ it "uses faux data when offline" do
95
+ type = [String, Integer].sample
96
+ data = HashAttribute.new({ title: { type: type } }).serialize(nil)
97
+ data[:title].should be_a(type)
98
+ end
92
99
 
93
- data = HashAttribute.new({ title: { type: type } }).serialize(nil)
94
- data[:title].should be_a(type)
100
+ it "maintains falsy-ie values when instructed" do
101
+ data = HashAttribute.new({ title: { type: Integer } }).serialize(nil, true)
102
+ data[:title].should == nil
103
+ data = HashAttribute.new({ title: { type: Integer } }).serialize(false, true)
104
+ data[:title].should == false
105
+ end
95
106
  end
96
107
  end
97
108
 
@@ -90,7 +90,7 @@ module Render
90
90
  }
91
91
  }
92
92
  schema = Schema.new(definition)
93
- schema.hash_attributes.first.should_receive(:serialize).with(nil).and_return({ title: "foo" })
93
+ schema.hash_attributes.first.should_receive(:serialize).with(nil, anything).and_return({ title: "foo" })
94
94
  schema.serialize!.should == { title: "foo" }
95
95
  end
96
96
  end
@@ -146,7 +146,7 @@ module Render
146
146
 
147
147
  expect {
148
148
  schema = Schema.new(:film)
149
- schema.render!({ endpoint: endpoint })
149
+ schema.render!(nil, endpoint)
150
150
  }.to raise_error(Errors::Schema::RequestError)
151
151
  end
152
152
 
@@ -155,7 +155,7 @@ module Render
155
155
  stub_request(:get, endpoint).to_return({ body: "Server Error: 500" })
156
156
 
157
157
  expect {
158
- Schema.new(:film).render!({ endpoint: endpoint })
158
+ Schema.new(:film).render!(nil, endpoint)
159
159
  }.to raise_error(Errors::Schema::InvalidResponse)
160
160
  end
161
161
 
@@ -171,9 +171,9 @@ module Render
171
171
  stub_request(:get, endpoint).to_return(response)
172
172
 
173
173
  schema = Schema.new(:film)
174
- schema.hash_attributes.first.should_receive(:serialize).with(genre).and_return({ genre: genre })
174
+ schema.hash_attributes.first.should_receive(:serialize).with(genre, anything).and_return({ genre: genre })
175
175
 
176
- schema.render!({ endpoint: endpoint }).should == { film: data }
176
+ schema.render!(nil, endpoint).should == { film: data }
177
177
  end
178
178
 
179
179
  it "is serialized value nested under #universal_title" do
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: render
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
5
- prerelease:
4
+ version: 0.0.4
6
5
  platform: ruby
7
6
  authors:
8
7
  - Steve Weber
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-12-17 00:00:00.000000000 Z
11
+ date: 2014-01-19 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: bundler
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ~>
20
18
  - !ruby/object:Gem::Version
@@ -22,7 +20,6 @@ dependencies:
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ~>
28
25
  - !ruby/object:Gem::Version
@@ -30,7 +27,6 @@ dependencies:
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rake
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
31
  - - ! '>='
36
32
  - !ruby/object:Gem::Version
@@ -38,7 +34,6 @@ dependencies:
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
38
  - - ! '>='
44
39
  - !ruby/object:Gem::Version
@@ -46,7 +41,6 @@ dependencies:
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: uuid
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
45
  - - '='
52
46
  - !ruby/object:Gem::Version
@@ -54,7 +48,6 @@ dependencies:
54
48
  type: :runtime
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
52
  - - '='
60
53
  - !ruby/object:Gem::Version
@@ -62,7 +55,6 @@ dependencies:
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: rake
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
59
  - - ! '>='
68
60
  - !ruby/object:Gem::Version
@@ -70,7 +62,6 @@ dependencies:
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
66
  - - ! '>='
76
67
  - !ruby/object:Gem::Version
@@ -78,7 +69,6 @@ dependencies:
78
69
  - !ruby/object:Gem::Dependency
79
70
  name: debugger
80
71
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
72
  requirements:
83
73
  - - ! '>='
84
74
  - !ruby/object:Gem::Version
@@ -86,7 +76,6 @@ dependencies:
86
76
  type: :development
87
77
  prerelease: false
88
78
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
79
  requirements:
91
80
  - - ! '>='
92
81
  - !ruby/object:Gem::Version
@@ -94,7 +83,6 @@ dependencies:
94
83
  - !ruby/object:Gem::Dependency
95
84
  name: rspec
96
85
  requirement: !ruby/object:Gem::Requirement
97
- none: false
98
86
  requirements:
99
87
  - - ! '>='
100
88
  - !ruby/object:Gem::Version
@@ -102,7 +90,6 @@ dependencies:
102
90
  type: :development
103
91
  prerelease: false
104
92
  version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
93
  requirements:
107
94
  - - ! '>='
108
95
  - !ruby/object:Gem::Version
@@ -110,7 +97,6 @@ dependencies:
110
97
  - !ruby/object:Gem::Dependency
111
98
  name: webmock
112
99
  requirement: !ruby/object:Gem::Requirement
113
- none: false
114
100
  requirements:
115
101
  - - ! '>='
116
102
  - !ruby/object:Gem::Version
@@ -118,7 +104,6 @@ dependencies:
118
104
  type: :development
119
105
  prerelease: false
120
106
  version_requirements: !ruby/object:Gem::Requirement
121
- none: false
122
107
  requirements:
123
108
  - - ! '>='
124
109
  - !ruby/object:Gem::Version
@@ -166,6 +151,7 @@ files:
166
151
  - spec/support/helpers.rb
167
152
  - spec/unit/array_attribute_spec.rb
168
153
  - spec/unit/extensions/boolean_spec.rb
154
+ - spec/unit/render/attribute_spec.rb
169
155
  - spec/unit/render/dottable_hash_spec.rb
170
156
  - spec/unit/render/generator_spec.rb
171
157
  - spec/unit/render/graph_spec.rb
@@ -175,33 +161,26 @@ files:
175
161
  homepage: https://github.com/stevenweber/render
176
162
  licenses:
177
163
  - MIT
164
+ metadata: {}
178
165
  post_install_message:
179
166
  rdoc_options: []
180
167
  require_paths:
181
168
  - lib
182
169
  required_ruby_version: !ruby/object:Gem::Requirement
183
- none: false
184
170
  requirements:
185
171
  - - ! '>='
186
172
  - !ruby/object:Gem::Version
187
173
  version: '0'
188
- segments:
189
- - 0
190
- hash: 2584837128307883424
191
174
  required_rubygems_version: !ruby/object:Gem::Requirement
192
- none: false
193
175
  requirements:
194
176
  - - ! '>='
195
177
  - !ruby/object:Gem::Version
196
178
  version: '0'
197
- segments:
198
- - 0
199
- hash: 2584837128307883424
200
179
  requirements: []
201
180
  rubyforge_project:
202
- rubygems_version: 1.8.25
181
+ rubygems_version: 2.1.11
203
182
  signing_key:
204
- specification_version: 3
183
+ specification_version: 4
205
184
  summary: With a JSON schema, Render will manage requests, dependent request and build
206
185
  meaningful, extensible sample data for testing.
207
186
  test_files:
@@ -217,6 +196,7 @@ test_files:
217
196
  - spec/support/helpers.rb
218
197
  - spec/unit/array_attribute_spec.rb
219
198
  - spec/unit/extensions/boolean_spec.rb
199
+ - spec/unit/render/attribute_spec.rb
220
200
  - spec/unit/render/dottable_hash_spec.rb
221
201
  - spec/unit/render/generator_spec.rb
222
202
  - spec/unit/render/graph_spec.rb