rabl-rails 0.3.4 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. data/.travis.yml +2 -2
  2. data/CHANGELOG.md +10 -0
  3. data/Gemfile +5 -9
  4. data/README.md +5 -3
  5. data/Rakefile +2 -2
  6. data/lib/rabl-rails.rb +21 -74
  7. data/lib/rabl-rails/compiler.rb +28 -38
  8. data/lib/rabl-rails/configuration.rb +48 -0
  9. data/lib/rabl-rails/handler.rb +3 -1
  10. data/lib/rabl-rails/helpers.rb +7 -0
  11. data/lib/rabl-rails/library.rb +43 -16
  12. data/lib/rabl-rails/nodes.rb +6 -0
  13. data/lib/rabl-rails/nodes/attribute.rb +17 -0
  14. data/lib/rabl-rails/nodes/child.rb +12 -0
  15. data/lib/rabl-rails/nodes/code.rb +19 -0
  16. data/lib/rabl-rails/nodes/condition.rb +14 -0
  17. data/lib/rabl-rails/nodes/glue.rb +25 -0
  18. data/lib/rabl-rails/nodes/node.rb +9 -0
  19. data/lib/rabl-rails/railtie.rb +0 -2
  20. data/lib/rabl-rails/renderer.rb +15 -13
  21. data/lib/rabl-rails/renderers/hash.rb +85 -0
  22. data/lib/rabl-rails/renderers/json.rb +9 -5
  23. data/lib/rabl-rails/renderers/plist.rb +6 -4
  24. data/lib/rabl-rails/renderers/xml.rb +6 -3
  25. data/lib/rabl-rails/responder.rb +1 -1
  26. data/lib/rabl-rails/template.rb +11 -5
  27. data/lib/rabl-rails/version.rb +1 -1
  28. data/lib/rabl-rails/visitors.rb +2 -0
  29. data/lib/rabl-rails/visitors/to_hash.rb +131 -0
  30. data/lib/rabl-rails/visitors/visitor.rb +17 -0
  31. data/rabl-rails.gemspec +3 -5
  32. data/test/helper.rb +75 -0
  33. data/test/renderers/test_hash_renderer.rb +90 -0
  34. data/test/renderers/test_json_renderer.rb +46 -0
  35. data/test/renderers/test_plist_renderer.rb +42 -0
  36. data/test/renderers/test_xml_renderer.rb +37 -0
  37. data/test/test_compiler.rb +283 -0
  38. data/test/test_configuration.rb +31 -0
  39. data/test/test_hash_visitor.rb +224 -0
  40. data/test/test_library.rb +85 -0
  41. data/test/{render_test.rb → test_render.rb} +18 -24
  42. metadata +99 -108
  43. data/lib/rabl-rails/condition.rb +0 -10
  44. data/lib/rabl-rails/renderers/base.rb +0 -171
  45. data/test/base_renderer_test.rb +0 -67
  46. data/test/cache_templates_test.rb +0 -35
  47. data/test/compiler_test.rb +0 -233
  48. data/test/deep_nesting_test.rb +0 -56
  49. data/test/keyword_test.rb +0 -47
  50. data/test/non_restful_response_test.rb +0 -35
  51. data/test/renderers/json_renderer_test.rb +0 -189
  52. data/test/renderers/plist_renderer_test.rb +0 -135
  53. data/test/renderers/xml_renderer_test.rb +0 -137
  54. data/test/test_helper.rb +0 -68
@@ -1,137 +0,0 @@
1
- require 'test_helper'
2
-
3
- class TestXmlRenderer < ActiveSupport::TestCase
4
- INDENT_REGEXP = /\n(\s)*/
5
- HEADER_REGEXP = /<[^>]+>/
6
-
7
- setup do
8
- @data = User.new(1, 'foobar', 'male')
9
-
10
- @context = Context.new
11
- @context.assigns['data'] = @data
12
-
13
- @template = RablRails::CompiledTemplate.new
14
- @template.data = :@data
15
- @template.root_name = :user
16
- end
17
-
18
- def render_xml_output
19
- RablRails::Renderers::XML.new(@context).render(@template).to_s.gsub!(INDENT_REGEXP, '').sub!(HEADER_REGEXP, '')
20
- end
21
-
22
- test "render object simple object" do
23
- @template.source = {}
24
- assert_equal %q(<user></user>), render_xml_output
25
- end
26
-
27
- test "render collection with empty template" do
28
- @context.assigns['data'] = [@data]
29
- @template.source = {}
30
- @template.root_name = :users
31
- assert_equal %q(<users type="array"><user></user></users>), render_xml_output
32
- end
33
-
34
- test "render object with local methods (used by decent_exposure)" do
35
- @context.stub(:user).and_return(@data)
36
- @template.source = { :id => :id }
37
- assert_equal %q(<user><id type="integer">1</id></user>), render_xml_output
38
- end
39
-
40
- test "render single object attributes" do
41
- @template.source = { :name => :name }
42
- assert_equal %q(<user><name>foobar</name></user>), render_xml_output
43
- end
44
-
45
- test "render child with arbitrary data source" do
46
- @template.source = { :author => { :_data => :@data, :name => :name } }
47
- @template.root_name = :post
48
- assert_equal %q(<post><author><name>foobar</name></author></post>), render_xml_output
49
- end
50
-
51
- test "render child with local methods (used by decent_exposure)" do
52
- @context.stub(:user).and_return(@data)
53
- @template.source = { :author => { :_data => :user, :name => :name } }
54
- @template.root_name = :post
55
- assert_equal %q(<post><author><name>foobar</name></author></post>), render_xml_output
56
- end
57
-
58
- test "render glued attributes from single object" do
59
- @template.source = { :_glue0 => { :_data => :@data, :name => :name } }
60
- assert_equal %q(<user><name>foobar</name></user>), render_xml_output
61
- end
62
-
63
- test "render collection with attributes" do
64
- @data = [User.new(1, 'foo', 'male'), User.new(2, 'bar', 'female')]
65
- @context.assigns['data'] = @data
66
- @template.root_name = :users
67
- @template.source = { :uid => :id, :name => :name }
68
- assert_equal %q(<users type="array"><user><uid type="integer">1</uid><name>foo</name></user><user><uid type="integer">2</uid><name>bar</name></user></users>), render_xml_output
69
- end
70
-
71
- test "render node property" do
72
- proc = lambda { |object| object.name }
73
- @template.source = { :name => proc }
74
- assert_equal %q(<user><name>foobar</name></user>), render_xml_output
75
- end
76
-
77
- test "render node property with true condition" do
78
- condition = lambda { |u| true }
79
- proc = lambda { |object| object.name }
80
- @template.source = { :name => [condition, proc] }
81
- assert_equal %q(<user><name>foobar</name></user>), render_xml_output
82
- end
83
-
84
- test "render node property with false condition" do
85
- condition = lambda { |u| false }
86
- proc = lambda { |object| object.name }
87
- @template.source = { :name => [condition, proc] }
88
- assert_equal %q(<user></user>), render_xml_output
89
- end
90
-
91
- test "node with context method call" do
92
- @context.stub(:respond_to?).with(:@data).and_return(false)
93
- @context.stub(:respond_to?).with(:context_method).and_return(true)
94
- @context.stub(:context_method).and_return('marty')
95
- proc = lambda { |object| context_method }
96
- @template.source = { :name => proc }
97
- assert_equal %q(<user><name>marty</name></user>), render_xml_output
98
- end
99
-
100
- test "partial should be evaluated at rendering time" do
101
- # Set assigns
102
- @context.assigns['user'] = @data
103
-
104
- # Stub Library#get
105
- t = RablRails::CompiledTemplate.new
106
- t.source = { :name => :name }
107
- RablRails::Library.reset_instance
108
- RablRails::Library.instance.should_receive(:compile_template_from_path).with('users/base').and_return(t)
109
-
110
- @template.data = false
111
- @template.root_name = :post
112
- @template.source = { :user => ->(s) { partial('users/base', :object => @user) } }
113
-
114
- assert_equal %q(<post><user><name>foobar</name></user></post>), render_xml_output
115
- end
116
-
117
- test "partial with no values should raise an error" do
118
- @template.data = false
119
- @template.source = { :user => ->(s) { partial('users/base') } }
120
-
121
- assert_raises(RablRails::Renderers::PartialError) { render_xml_output }
122
- end
123
-
124
- test "partial with empty values should not raise an error" do
125
- @template.data = false
126
- @template.root_name = :list
127
- @template.source = { :users => ->(s) { partial('users/base', :object => []) } }
128
-
129
- assert_equal %q(<list><users type="array"/></list>), render_xml_output
130
- end
131
-
132
- test "render underscorized xml" do
133
- RablRails.xml_options = { :dasherize => false, :skip_types => false }
134
- @template.source = { :first_name => :name }
135
- assert_equal %q(<user><first_name>foobar</first_name></user>), render_xml_output
136
- end
137
- end
@@ -1,68 +0,0 @@
1
- ENV["RAILS_ENV"] = "test"
2
- $:.unshift File.expand_path('../../lib', __FILE__)
3
-
4
- require 'rspec/mocks'
5
- require 'minitest/unit'
6
- MiniTest::Unit.autorun
7
-
8
- require 'active_support/test_case'
9
-
10
- require 'action_controller'
11
-
12
- require 'singleton'
13
- class <<Singleton
14
- def included_with_reset(klass)
15
- included_without_reset(klass)
16
- class <<klass
17
- def reset_instance
18
- Singleton.send :__init__, self
19
- self
20
- end
21
- end
22
- end
23
- alias_method_chain :included, :reset
24
- end
25
-
26
- require 'rabl-rails'
27
- require 'plist'
28
-
29
- if RUBY_ENGINE == 'jruby'
30
- require 'nokogiri'
31
- elsif RUBY_ENGINE == 'ruby'
32
- require 'libxml'
33
- end
34
-
35
- RablRails.load_default_engines!
36
-
37
- module ActiveSupport
38
- class TestCase
39
- RSpec::Mocks::setup(self)
40
- include RSpec::Mocks::ExampleMethods
41
- end
42
- end
43
-
44
- class Context
45
- attr_writer :virtual_path
46
-
47
- def initialize
48
- @_assigns = {}
49
- @virtual_path = nil
50
- end
51
-
52
- def assigns
53
- @_assigns
54
- end
55
-
56
- def params
57
- {}
58
- end
59
- end
60
-
61
- class User
62
- attr_accessor :id, :name, :sex
63
- def initialize(id=nil, name=nil, sex=nil)
64
- @id = id
65
- @name = name
66
- @sex = sex
67
- end
68
- end