representative_view 0.0.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,7 +10,7 @@ module RepresentativeView
10
10
  require 'representative/json'
11
11
  require 'representative/nokogiri'
12
12
  <<-RUBY
13
- representative_view do |r|
13
+ representative_view(#{template.format.inspect}) do |r|
14
14
  #{template.source}
15
15
  end
16
16
  RUBY
@@ -18,7 +18,7 @@ module RepresentativeView
18
18
 
19
19
  end
20
20
 
21
-
21
+
22
22
  end
23
23
 
24
24
  ActionView::Template.register_template_handler(:rep, RepresentativeView::ActionPack2Handler)
@@ -11,8 +11,12 @@ module RepresentativeView
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
+
14
18
  <<-RUBY
15
- representative_view do |r|
19
+ representative_view(#{format.inspect}) do |r|
16
20
  #{template.source}
17
21
  end
18
22
  RUBY
@@ -1,3 +1,3 @@
1
1
  module RepresentativeView
2
- VERSION = "0.0.2"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -14,9 +14,9 @@ module RepresentativeView
14
14
  raise "unrecognised format #{format_extension.inspect}"
15
15
  end
16
16
  end
17
-
18
- def appropriate_representative_class
19
- case mime_type.to_s
17
+
18
+ def appropriate_representative_class(format)
19
+ case (format || mime_type).to_s
20
20
  when /xml/
21
21
  ::Representative::Nokogiri
22
22
  when /json/
@@ -26,19 +26,19 @@ module RepresentativeView
26
26
  end
27
27
  end
28
28
 
29
- def representative_view
29
+ def representative_view(format = nil)
30
30
  included = defined?(@_representative)
31
- @_representative ||= appropriate_representative_class.new
31
+ @_representative ||= appropriate_representative_class(format).new
32
32
  yield @_representative
33
33
  @_representative.to_s unless included
34
34
  end
35
-
35
+
36
36
  end
37
-
37
+
38
38
  end
39
39
 
40
40
  class ActionView::Base
41
-
41
+
42
42
  include RepresentativeView::ViewHelpers
43
-
43
+
44
44
  end
@@ -10,7 +10,7 @@ describe "a Representative template" do
10
10
  end
11
11
  RUBY
12
12
  end
13
-
13
+
14
14
  it "can generate XML" do
15
15
  render("books", :xml, :books => Books.all).should == undent(<<-XML)
16
16
  <?xml version="1.0"?>
@@ -99,6 +99,27 @@ describe "a Representative template" do
99
99
  JSON
100
100
 
101
101
  end
102
-
103
-
102
+
103
+ it "can infer the format from the file" do
104
+ write_template 'more_books.json.rep', <<-RUBY
105
+ r.list_of :books, @books do
106
+ r.element :title
107
+ end
108
+ RUBY
109
+
110
+ render("more_books.json", :html, :books => Books.all).should == undent(<<-JSON)
111
+ [
112
+ {
113
+ "title": "Sailing for old dogs"
114
+ },
115
+ {
116
+ "title": "On the horizon"
117
+ },
118
+ {
119
+ "title": "The Little Blue Book of VHS Programming"
120
+ }
121
+ ]
122
+ JSON
123
+ end
124
+
104
125
  end
@@ -8,9 +8,9 @@ require 'fixtures/books'
8
8
 
9
9
  $tmp_dir = Pathname(__FILE__).parent.parent + "tmp"
10
10
  $template_dir = $tmp_dir + "templates"
11
-
11
+
12
12
  module Fixtures
13
-
13
+
14
14
  def write_template(name, content)
15
15
  template_path = $template_dir + name
16
16
  template_path.parent.mkpath
@@ -20,7 +20,7 @@ module Fixtures
20
20
  end
21
21
 
22
22
  def render(file, format = :xml, assigns = {})
23
- @base = ActionView::Base.new($template_dir.to_str, assigns)
23
+ @base = ActionView::Base.new($template_dir.to_s, assigns)
24
24
  if @base.respond_to?(:template_format=) # actionpack-2
25
25
  @base.template_format = format
26
26
  elsif @base.respond_to?(:lookup_context) # actionpack-3
@@ -32,10 +32,10 @@ module Fixtures
32
32
  end
33
33
 
34
34
  Rspec.configure do |config|
35
-
35
+
36
36
  config.mock_with :rr
37
37
  config.include(Fixtures)
38
-
38
+
39
39
  config.before do
40
40
  $template_dir.mkpath
41
41
  end
metadata CHANGED
@@ -1,13 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: representative_view
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
5
4
  prerelease:
6
- segments:
7
- - 0
8
- - 0
9
- - 2
10
- version: 0.0.2
5
+ version: 1.0.0
11
6
  platform: ruby
12
7
  authors:
13
8
  - Mike Williams
@@ -15,81 +10,56 @@ autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
12
 
18
- date: 2011-02-10 00:00:00 +11:00
13
+ date: 2011-04-02 00:00:00 +11:00
19
14
  default_executable:
20
15
  dependencies:
21
16
  - !ruby/object:Gem::Dependency
22
- version_requirements: &id001 !ruby/object:Gem::Requirement
17
+ name: representative
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
23
20
  none: false
24
21
  requirements:
25
22
  - - ~>
26
23
  - !ruby/object:Gem::Version
27
- hash: 17
28
- segments:
29
- - 0
30
- - 3
31
- - 1
32
24
  version: 0.3.1
33
- prerelease: false
34
25
  type: :runtime
35
- requirement: *id001
36
- name: representative
26
+ version_requirements: *id001
37
27
  - !ruby/object:Gem::Dependency
38
- version_requirements: &id002 !ruby/object:Gem::Requirement
28
+ name: actionpack
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
39
31
  none: false
40
32
  requirements:
41
33
  - - ">"
42
34
  - !ruby/object:Gem::Version
43
- hash: 3
44
- segments:
45
- - 2
46
- - 3
47
- - 0
48
35
  version: 2.3.0
49
36
  - - <
50
37
  - !ruby/object:Gem::Version
51
- hash: 63
52
- segments:
53
- - 4
54
- - 0
55
- - 0
56
38
  version: 4.0.0
57
- prerelease: false
58
39
  type: :runtime
59
- requirement: *id002
60
- name: actionpack
40
+ version_requirements: *id002
61
41
  - !ruby/object:Gem::Dependency
62
- version_requirements: &id003 !ruby/object:Gem::Requirement
42
+ name: nokogiri
43
+ prerelease: false
44
+ requirement: &id003 !ruby/object:Gem::Requirement
63
45
  none: false
64
46
  requirements:
65
47
  - - ">="
66
48
  - !ruby/object:Gem::Version
67
- hash: 3
68
- segments:
69
- - 1
70
- - 4
71
- - 2
72
49
  version: 1.4.2
73
- prerelease: false
74
50
  type: :runtime
75
- requirement: *id003
76
- name: nokogiri
51
+ version_requirements: *id003
77
52
  - !ruby/object:Gem::Dependency
78
- version_requirements: &id004 !ruby/object:Gem::Requirement
53
+ name: json
54
+ prerelease: false
55
+ requirement: &id004 !ruby/object:Gem::Requirement
79
56
  none: false
80
57
  requirements:
81
58
  - - ">="
82
59
  - !ruby/object:Gem::Version
83
- hash: 13
84
- segments:
85
- - 1
86
- - 4
87
- - 5
88
60
  version: 1.4.5
89
- prerelease: false
90
61
  type: :runtime
91
- requirement: *id004
92
- name: json
62
+ version_requirements: *id004
93
63
  description:
94
64
  email: mdub@dogbiscuit.org
95
65
  executables: []
@@ -126,23 +96,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
126
96
  requirements:
127
97
  - - ">="
128
98
  - !ruby/object:Gem::Version
129
- hash: 3
130
- segments:
131
- - 0
132
99
  version: "0"
133
100
  required_rubygems_version: !ruby/object:Gem::Requirement
134
101
  none: false
135
102
  requirements:
136
103
  - - ">="
137
104
  - !ruby/object:Gem::Version
138
- hash: 3
139
- segments:
140
- - 0
141
105
  version: "0"
142
106
  requirements: []
143
107
 
144
108
  rubyforge_project:
145
- rubygems_version: 1.5.0
109
+ rubygems_version: 1.6.2
146
110
  signing_key:
147
111
  specification_version: 3
148
112
  summary: Builds XML and JSON from a single view template