representative_view 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -8,8 +8,8 @@ if ENV["ACTIONPACK_VERSION"]
8
8
  end
9
9
 
10
10
  group :test do
11
- gem "rake"
12
- gem "rspec", "~> 2.0.1"
13
- gem "rr", "~> 1.0.0"
11
+ gem "rake", "~> 0.8.7"
12
+ gem "rspec", "~> 2.5.0"
13
+ gem "rr", "~> 1.0.2"
14
14
  gem "minstrel"
15
15
  end
data/README.markdown CHANGED
@@ -55,3 +55,14 @@ Representative View happily supports the use of partials, as long as they're als
55
55
  r.element :published do
56
56
  r.element :by
57
57
  end
58
+
59
+ Configuration
60
+ -------------
61
+
62
+ Output can be controlled somewhat setting `json_options` or `xml_options` on the RepresentativeView module; these options will be passed when initialising the appropriate Representative::Nokogiri or Representative::JSON object.
63
+
64
+ For example, setting:
65
+
66
+ RepresentativeView.json_options = {:naming_strategy => :camelCase}
67
+
68
+ causes JSON to be output with camelCase (rather than snake_case) labels.
@@ -10,7 +10,7 @@ module RepresentativeView
10
10
  require 'representative/json'
11
11
  require 'representative/nokogiri'
12
12
  <<-RUBY
13
- representative_view(#{template.format.inspect}) do |r|
13
+ representative_view(#{template.format.inspect} || template_format) do |r|
14
14
  #{template.source}
15
15
  end
16
16
  RUBY
@@ -6,17 +6,13 @@ module RepresentativeView
6
6
 
7
7
  include ActionView::Template::Handlers::Compilable
8
8
 
9
- self.default_format = Mime::XML
9
+ self.default_format = nil
10
10
 
11
11
  def compile(template)
12
12
  require 'representative/json'
13
13
  require 'representative/nokogiri'
14
-
15
- format = template.formats.first
16
- format = nil if Mime[format] == default_format
17
-
18
14
  <<-RUBY
19
- representative_view(#{format.inspect}) do |r|
15
+ representative_view(#{template.formats.first.inspect}) do |r|
20
16
  #{template.source}
21
17
  end
22
18
  RUBY
@@ -0,0 +1,13 @@
1
+ module RepresentativeView
2
+
3
+ class << self
4
+
5
+ attr_accessor :xml_options
6
+ attr_accessor :json_options
7
+
8
+ end
9
+
10
+ self.xml_options = {}
11
+ self.json_options = {}
12
+
13
+ end
@@ -1,3 +1,3 @@
1
1
  module RepresentativeView
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -4,35 +4,32 @@ module RepresentativeView
4
4
 
5
5
  module ViewHelpers
6
6
 
7
- def mime_type
8
- if respond_to?(:template_format)
9
- format_extension = template_format
7
+ def representative_view(format)
8
+ if defined?(@_representative)
9
+ yield @_representative # included
10
10
  else
11
- format_extension = formats.first
12
- end
13
- Mime::Type.lookup_by_extension(format_extension.to_s) || begin
14
- raise "unrecognised format #{format_extension.inspect}"
11
+ @_representative = create_representative(format)
12
+ yield @_representative
13
+ @_representative.to_s
15
14
  end
16
15
  end
17
16
 
18
- def appropriate_representative_class(format)
19
- case (format || mime_type).to_s
17
+ private
18
+
19
+ def create_representative(format)
20
+ mime_type = Mime::Type.lookup_by_extension(format) || begin
21
+ raise ArgumentError, "unrecognised format: #{format.inspect}"
22
+ end
23
+ case mime_type.to_s
20
24
  when /xml/
21
- ::Representative::Nokogiri
25
+ ::Representative::Nokogiri.new(nil, RepresentativeView.xml_options)
22
26
  when /json/
23
- ::Representative::JSON
27
+ ::Representative::JSON.new(nil, RepresentativeView.json_options)
24
28
  else
25
- raise "cannot determine appropriate Representative class for #{mime_type.to_s.inspect}"
29
+ raise "Representative cannot generate #{format}"
26
30
  end
27
31
  end
28
32
 
29
- def representative_view(format = nil)
30
- included = defined?(@_representative)
31
- @_representative ||= appropriate_representative_class(format).new
32
- yield @_representative
33
- @_representative.to_s unless included
34
- end
35
-
36
33
  end
37
34
 
38
35
  end
@@ -1,2 +1,3 @@
1
1
  require "action_pack/version"
2
+ require "representative_view/configuration"
2
3
  require "representative_view/action_pack_#{ActionPack::VERSION::MAJOR}_handler"
@@ -9,6 +9,8 @@ describe "a Representative template" do
9
9
  r.element :title
10
10
  end
11
11
  RUBY
12
+ RepresentativeView.json_options = {}
13
+ RepresentativeView.xml_options = {}
12
14
  end
13
15
 
14
16
  it "can generate XML" do
@@ -122,4 +124,40 @@ describe "a Representative template" do
122
124
  JSON
123
125
  end
124
126
 
125
- end
127
+ it "allows configuration of json_options" do
128
+ RepresentativeView.json_options = {:naming_strategy => :upcase}
129
+ render("books", :json, :books => Books.all).should == undent(<<-JSON)
130
+ [
131
+ {
132
+ "TITLE": "Sailing for old dogs"
133
+ },
134
+ {
135
+ "TITLE": "On the horizon"
136
+ },
137
+ {
138
+ "TITLE": "The Little Blue Book of VHS Programming"
139
+ }
140
+ ]
141
+ JSON
142
+ end
143
+
144
+ it "allows configuration of xml_options" do
145
+
146
+ RepresentativeView.xml_options = {:naming_strategy => :upcase}
147
+ render("books", :xml, :books => Books.all).should == undent(<<-XML)
148
+ <?xml version="1.0"?>
149
+ <BOOKS TYPE="array">
150
+ <BOOK>
151
+ <TITLE>Sailing for old dogs</TITLE>
152
+ </BOOK>
153
+ <BOOK>
154
+ <TITLE>On the horizon</TITLE>
155
+ </BOOK>
156
+ <BOOK>
157
+ <TITLE>The Little Blue Book of VHS Programming</TITLE>
158
+ </BOOK>
159
+ </BOOKS>
160
+ XML
161
+ end
162
+
163
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: representative_view
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.0.0
5
+ version: 1.1.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Mike Williams
@@ -10,12 +10,10 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-04-02 00:00:00 +11:00
14
- default_executable:
13
+ date: 2011-05-28 00:00:00 Z
15
14
  dependencies:
16
15
  - !ruby/object:Gem::Dependency
17
16
  name: representative
18
- prerelease: false
19
17
  requirement: &id001 !ruby/object:Gem::Requirement
20
18
  none: false
21
19
  requirements:
@@ -23,10 +21,10 @@ dependencies:
23
21
  - !ruby/object:Gem::Version
24
22
  version: 0.3.1
25
23
  type: :runtime
24
+ prerelease: false
26
25
  version_requirements: *id001
27
26
  - !ruby/object:Gem::Dependency
28
27
  name: actionpack
29
- prerelease: false
30
28
  requirement: &id002 !ruby/object:Gem::Requirement
31
29
  none: false
32
30
  requirements:
@@ -37,10 +35,10 @@ dependencies:
37
35
  - !ruby/object:Gem::Version
38
36
  version: 4.0.0
39
37
  type: :runtime
38
+ prerelease: false
40
39
  version_requirements: *id002
41
40
  - !ruby/object:Gem::Dependency
42
41
  name: nokogiri
43
- prerelease: false
44
42
  requirement: &id003 !ruby/object:Gem::Requirement
45
43
  none: false
46
44
  requirements:
@@ -48,10 +46,10 @@ dependencies:
48
46
  - !ruby/object:Gem::Version
49
47
  version: 1.4.2
50
48
  type: :runtime
49
+ prerelease: false
51
50
  version_requirements: *id003
52
51
  - !ruby/object:Gem::Dependency
53
52
  name: json
54
- prerelease: false
55
53
  requirement: &id004 !ruby/object:Gem::Requirement
56
54
  none: false
57
55
  requirements:
@@ -59,6 +57,7 @@ dependencies:
59
57
  - !ruby/object:Gem::Version
60
58
  version: 1.4.5
61
59
  type: :runtime
60
+ prerelease: false
62
61
  version_requirements: *id004
63
62
  description:
64
63
  email: mdub@dogbiscuit.org
@@ -76,13 +75,13 @@ files:
76
75
  - lib/representative_view.rb
77
76
  - lib/representative_view/action_pack_2_handler.rb
78
77
  - lib/representative_view/action_pack_3_handler.rb
78
+ - lib/representative_view/configuration.rb
79
79
  - lib/representative_view/version.rb
80
80
  - lib/representative_view/view_helpers.rb
81
81
  - representative_view.gemspec
82
82
  - spec/fixtures/books.rb
83
83
  - spec/representative_view/template_handler_spec.rb
84
84
  - spec/spec_helper.rb
85
- has_rdoc: true
86
85
  homepage: http://github.com/mdub/representative_view
87
86
  licenses: []
88
87
 
@@ -96,17 +95,23 @@ required_ruby_version: !ruby/object:Gem::Requirement
96
95
  requirements:
97
96
  - - ">="
98
97
  - !ruby/object:Gem::Version
98
+ hash: 738900437358486891
99
+ segments:
100
+ - 0
99
101
  version: "0"
100
102
  required_rubygems_version: !ruby/object:Gem::Requirement
101
103
  none: false
102
104
  requirements:
103
105
  - - ">="
104
106
  - !ruby/object:Gem::Version
107
+ hash: 738900437358486891
108
+ segments:
109
+ - 0
105
110
  version: "0"
106
111
  requirements: []
107
112
 
108
113
  rubyforge_project:
109
- rubygems_version: 1.6.2
114
+ rubygems_version: 1.7.2
110
115
  signing_key:
111
116
  specification_version: 3
112
117
  summary: Builds XML and JSON from a single view template