representative_view 1.1.0 → 1.2.0

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.
data/CHANGES.markdown ADDED
@@ -0,0 +1,10 @@
1
+ 1.2.0 - 2011-09-06
2
+ ------------------
3
+
4
+ ### Enhancements
5
+
6
+ - Addition of `representative(FORMAT)` method, allowing a format to be forced.
7
+
8
+ ### Removals
9
+
10
+ - We no longer attempt to infer an output format from the template format.
data/README.markdown CHANGED
@@ -37,7 +37,7 @@ Next, create a template with the suffix "`.rep`" (to select the Representative V
37
37
  r.list_of :authors
38
38
  end
39
39
 
40
- Note that it's "`index.rep`", not "`index.xml.rep`" or "`index.json.rep`"; by omitting the format specifier, the template will be used to render both formats.
40
+ Note that it's "`index.rep`", not "`index.xml.rep`" or "`index.json.rep`"; those will work, too, but by omitting the format specifier, the same template can be used to render both formats.
41
41
 
42
42
  ### Partials
43
43
 
@@ -56,6 +56,25 @@ Representative View happily supports the use of partials, as long as they're als
56
56
  r.element :by
57
57
  end
58
58
 
59
+ #### Forcing a format
60
+
61
+ It can occasionally be useful to include a ".rep" partial from within a non-Representative template, for instance to generate a sample JSON or XML representation in HTML-based API documentation. This is possible by forcing a format using the `representative` helper-method, as follows:
62
+
63
+ # app/views/doc/things.html.erb
64
+
65
+ <h2>Sample JSON output</h2>
66
+
67
+ <pre>
68
+ <%= representative(:json) { render :partial => 'sample_things' } %>
69
+ </pre>
70
+
71
+ # app/views/doc/_sample_things.rep
72
+
73
+ r.list_of :things, SampleThings.all do
74
+ r.element :name
75
+ r.element :description
76
+ end
77
+
59
78
  Configuration
60
79
  -------------
61
80
 
@@ -2,17 +2,13 @@ require 'representative_view/view_helpers'
2
2
 
3
3
  module RepresentativeView
4
4
 
5
- class ActionPack3Handler < ActionView::Template::Handler
5
+ class ActionPack3Handler
6
6
 
7
- include ActionView::Template::Handlers::Compilable
8
-
9
- self.default_format = nil
10
-
11
- def compile(template)
7
+ def self.call(template)
12
8
  require 'representative/json'
13
9
  require 'representative/nokogiri'
14
10
  <<-RUBY
15
- representative_view(#{template.formats.first.inspect}) do |r|
11
+ representative_view(formats.first) do |r|
16
12
  #{template.source}
17
13
  end
18
14
  RUBY
@@ -1,3 +1,3 @@
1
1
  module RepresentativeView
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0".freeze
3
3
  end
@@ -5,16 +5,36 @@ module RepresentativeView
5
5
  module ViewHelpers
6
6
 
7
7
  def representative_view(format)
8
- if defined?(@_representative)
9
- yield @_representative # included
8
+ if current_representative
9
+ yield current_representative
10
10
  else
11
- @_representative = create_representative(format)
12
- yield @_representative
13
- @_representative.to_s
11
+ representative(format) do
12
+ yield current_representative
13
+ end
14
14
  end
15
15
  end
16
16
 
17
- private
17
+ def representative(format, &block)
18
+ r = create_representative(format)
19
+ with_representative(r, &block)
20
+ r.to_s
21
+ end
22
+
23
+ private
24
+
25
+ def current_representative
26
+ @_current_representative
27
+ end
28
+
29
+ def with_representative(representative)
30
+ old_representative = @_current_representative
31
+ begin
32
+ @_current_representative = representative
33
+ yield representative
34
+ ensure
35
+ @_current_representative = old_representative
36
+ end
37
+ end
18
38
 
19
39
  def create_representative(format)
20
40
  mime_type = Mime::Type.lookup_by_extension(format) || begin
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
  require "representative_view/version"
4
4
 
5
5
  Gem::Specification.new do |gem|
6
-
6
+
7
7
  gem.name = "representative_view"
8
8
  gem.summary = "Builds XML and JSON from a single view template"
9
9
  gem.homepage = "http://github.com/mdub/representative_view"
@@ -13,13 +13,11 @@ Gem::Specification.new do |gem|
13
13
  gem.version = RepresentativeView::VERSION.dup
14
14
  gem.platform = Gem::Platform::RUBY
15
15
 
16
- gem.add_runtime_dependency("representative", "~> 0.3.1")
16
+ gem.add_runtime_dependency("representative", "~> 1.0.2")
17
17
  gem.add_runtime_dependency("actionpack", "> 2.3.0", "< 4.0.0")
18
- gem.add_runtime_dependency("nokogiri", ">= 1.4.2")
19
- gem.add_runtime_dependency("json", ">= 1.4.5")
20
18
 
21
19
  gem.require_paths = ["lib"]
22
-
20
+
23
21
  gem.files = `git ls-files`.split("\n")
24
22
  gem.test_files = `git ls-files -- spec/*`.split("\n")
25
23
 
@@ -102,30 +102,67 @@ describe "a Representative template" do
102
102
 
103
103
  end
104
104
 
105
- it "can infer the format from the file" do
106
- write_template 'more_books.json.rep', <<-RUBY
105
+ it "can force a format for partials" do
106
+
107
+ write_template '_books.rep', <<-RUBY
107
108
  r.list_of :books, @books do
108
109
  r.element :title
109
110
  end
110
111
  RUBY
111
112
 
112
- render("more_books.json", :html, :books => Books.all).should == undent(<<-JSON)
113
+ write_template 'books.html.erb', undent(<<-HTML)
114
+ <h2>JSON REPRESENTATION</h2>
115
+
116
+ <pre>
117
+ <%= h representative(:json) { render :partial => 'books' } %></pre>
118
+
119
+ <h2>XML REPRESENTATION</h2>
120
+
121
+ <pre>
122
+ <%= h representative(:xml) { render :partial => 'books' } %></pre>
123
+ HTML
124
+
125
+ render("books", :html, :books => Books.all).should == undent(<<-HTML)
126
+ <h2>JSON REPRESENTATION</h2>
127
+
128
+ <pre>
113
129
  [
114
130
  {
115
- "title": "Sailing for old dogs"
131
+ &quot;title&quot;: &quot;Sailing for old dogs&quot;
116
132
  },
117
133
  {
118
- "title": "On the horizon"
134
+ &quot;title&quot;: &quot;On the horizon&quot;
119
135
  },
120
136
  {
121
- "title": "The Little Blue Book of VHS Programming"
137
+ &quot;title&quot;: &quot;The Little Blue Book of VHS Programming&quot;
122
138
  }
123
139
  ]
124
- JSON
140
+ </pre>
141
+
142
+ <h2>XML REPRESENTATION</h2>
143
+
144
+ <pre>
145
+ &lt;?xml version=&quot;1.0&quot;?&gt;
146
+ &lt;books type=&quot;array&quot;&gt;
147
+ &lt;book&gt;
148
+ &lt;title&gt;Sailing for old dogs&lt;/title&gt;
149
+ &lt;/book&gt;
150
+ &lt;book&gt;
151
+ &lt;title&gt;On the horizon&lt;/title&gt;
152
+ &lt;/book&gt;
153
+ &lt;book&gt;
154
+ &lt;title&gt;The Little Blue Book of VHS Programming&lt;/title&gt;
155
+ &lt;/book&gt;
156
+ &lt;/books&gt;
157
+ </pre>
158
+ HTML
159
+
125
160
  end
126
161
 
127
162
  it "allows configuration of json_options" do
163
+
128
164
  RepresentativeView.json_options = {:naming_strategy => :upcase}
165
+
129
166
  render("books", :json, :books => Books.all).should == undent(<<-JSON)
130
167
  [
131
168
  {
@@ -139,11 +176,13 @@ describe "a Representative template" do
139
176
  }
140
177
  ]
141
178
  JSON
179
+
142
180
  end
143
181
 
144
182
  it "allows configuration of xml_options" do
145
183
 
146
184
  RepresentativeView.xml_options = {:naming_strategy => :upcase}
185
+
147
186
  render("books", :xml, :books => Books.all).should == undent(<<-XML)
148
187
  <?xml version="1.0"?>
149
188
  <BOOKS TYPE="array">
@@ -158,6 +197,7 @@ describe "a Representative template" do
158
197
  </BOOK>
159
198
  </BOOKS>
160
199
  XML
200
+
161
201
  end
162
202
 
163
- end
203
+ end
metadata CHANGED
@@ -1,74 +1,49 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: representative_view
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.0
4
5
  prerelease:
5
- version: 1.1.0
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Mike Williams
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2011-05-28 00:00:00 Z
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
12
+ date: 2011-09-06 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
16
15
  name: representative
17
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &70240665487840 !ruby/object:Gem::Requirement
18
17
  none: false
19
- requirements:
18
+ requirements:
20
19
  - - ~>
21
- - !ruby/object:Gem::Version
22
- version: 0.3.1
20
+ - !ruby/object:Gem::Version
21
+ version: 1.0.2
23
22
  type: :runtime
24
23
  prerelease: false
25
- version_requirements: *id001
26
- - !ruby/object:Gem::Dependency
24
+ version_requirements: *70240665487840
25
+ - !ruby/object:Gem::Dependency
27
26
  name: actionpack
28
- requirement: &id002 !ruby/object:Gem::Requirement
27
+ requirement: &70240665487140 !ruby/object:Gem::Requirement
29
28
  none: false
30
- requirements:
31
- - - ">"
32
- - !ruby/object:Gem::Version
29
+ requirements:
30
+ - - ! '>'
31
+ - !ruby/object:Gem::Version
33
32
  version: 2.3.0
34
33
  - - <
35
- - !ruby/object:Gem::Version
34
+ - !ruby/object:Gem::Version
36
35
  version: 4.0.0
37
36
  type: :runtime
38
37
  prerelease: false
39
- version_requirements: *id002
40
- - !ruby/object:Gem::Dependency
41
- name: nokogiri
42
- requirement: &id003 !ruby/object:Gem::Requirement
43
- none: false
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: 1.4.2
48
- type: :runtime
49
- prerelease: false
50
- version_requirements: *id003
51
- - !ruby/object:Gem::Dependency
52
- name: json
53
- requirement: &id004 !ruby/object:Gem::Requirement
54
- none: false
55
- requirements:
56
- - - ">="
57
- - !ruby/object:Gem::Version
58
- version: 1.4.5
59
- type: :runtime
60
- prerelease: false
61
- version_requirements: *id004
38
+ version_requirements: *70240665487140
62
39
  description:
63
40
  email: mdub@dogbiscuit.org
64
41
  executables: []
65
-
66
42
  extensions: []
67
-
68
43
  extra_rdoc_files: []
69
-
70
- files:
44
+ files:
71
45
  - .gitignore
46
+ - CHANGES.markdown
72
47
  - Gemfile
73
48
  - README.markdown
74
49
  - Rakefile
@@ -84,38 +59,35 @@ files:
84
59
  - spec/spec_helper.rb
85
60
  homepage: http://github.com/mdub/representative_view
86
61
  licenses: []
87
-
88
62
  post_install_message:
89
63
  rdoc_options: []
90
-
91
- require_paths:
64
+ require_paths:
92
65
  - lib
93
- required_ruby_version: !ruby/object:Gem::Requirement
66
+ required_ruby_version: !ruby/object:Gem::Requirement
94
67
  none: false
95
- requirements:
96
- - - ">="
97
- - !ruby/object:Gem::Version
98
- hash: 738900437358486891
99
- segments:
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ segments:
100
73
  - 0
101
- version: "0"
102
- required_rubygems_version: !ruby/object:Gem::Requirement
74
+ hash: 2640363158941591145
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
76
  none: false
104
- requirements:
105
- - - ">="
106
- - !ruby/object:Gem::Version
107
- hash: 738900437358486891
108
- segments:
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ segments:
109
82
  - 0
110
- version: "0"
83
+ hash: 2640363158941591145
111
84
  requirements: []
112
-
113
85
  rubyforge_project:
114
- rubygems_version: 1.7.2
86
+ rubygems_version: 1.8.10
115
87
  signing_key:
116
88
  specification_version: 3
117
89
  summary: Builds XML and JSON from a single view template
118
- test_files:
90
+ test_files:
119
91
  - spec/fixtures/books.rb
120
92
  - spec/representative_view/template_handler_spec.rb
121
93
  - spec/spec_helper.rb