rabl-rails 0.4.3 → 0.5.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/test/test_helpers.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'helper'
2
2
  require 'set'
3
3
 
4
- class TestHelpers < MINITEST_TEST_CLASS
4
+ class TestHelpers < Minitest::Test
5
5
  include RablRails::Helpers
6
6
 
7
7
  def test_collection_with_default
data/test/test_library.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require 'helper'
2
2
 
3
- class TestLibrary < MINITEST_TEST_CLASS
3
+ class TestLibrary < Minitest::Test
4
4
  RablRails::Library.send(:attr_reader, :cached_templates)
5
5
 
6
6
  describe 'library' do
@@ -62,7 +62,7 @@ class TestLibrary < MINITEST_TEST_CLASS
62
62
 
63
63
  it 'compiles source without caching it if options is not set' do
64
64
  @context.virtual_path = 'users/base'
65
- template = with_configuration :cache_templates, false do
65
+ with_configuration :cache_templates, false do
66
66
  @library.compile_template_from_source("attribute :id", @context)
67
67
  end
68
68
 
@@ -70,4 +70,4 @@ class TestLibrary < MINITEST_TEST_CLASS
70
70
  end
71
71
  end
72
72
  end
73
- end
73
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rabl-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christopher Cocchi-Perrier
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-08 00:00:00.000000000 Z
11
+ date: 2017-08-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -16,57 +16,71 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '3.1'
19
+ version: '4.2'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '3.1'
26
+ version: '4.2'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: railties
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '3.1'
33
+ version: '4.2'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '3.1'
40
+ version: '4.2'
41
41
  - !ruby/object:Gem::Dependency
42
- name: thread_safe
42
+ name: concurrent-ruby
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: 0.3.1
47
+ version: 1.0.0
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: 0.3.1
54
+ version: 1.0.0
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: actionpack
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: '3.1'
61
+ version: '4.2'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: '3.1'
69
- description: Fast Rails 3+ templating system with JSON, XML and PList support
68
+ version: '4.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: actionview
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '4.2'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '4.2'
83
+ description: Fast Rails 4+ templating system with JSON, XML and PList support
70
84
  email:
71
85
  - cocchi.c@gmail.com
72
86
  executables: []
@@ -83,6 +97,7 @@ files:
83
97
  - lib/rabl-rails.rb
84
98
  - lib/rabl-rails/compiler.rb
85
99
  - lib/rabl-rails/configuration.rb
100
+ - lib/rabl-rails/exceptions.rb
86
101
  - lib/rabl-rails/handler.rb
87
102
  - lib/rabl-rails/helpers.rb
88
103
  - lib/rabl-rails/library.rb
@@ -91,15 +106,13 @@ files:
91
106
  - lib/rabl-rails/nodes/child.rb
92
107
  - lib/rabl-rails/nodes/code.rb
93
108
  - lib/rabl-rails/nodes/condition.rb
109
+ - lib/rabl-rails/nodes/extend.rb
94
110
  - lib/rabl-rails/nodes/glue.rb
95
- - lib/rabl-rails/nodes/node.rb
96
111
  - lib/rabl-rails/railtie.rb
97
- - lib/rabl-rails/renderer.rb
98
112
  - lib/rabl-rails/renderers/hash.rb
99
113
  - lib/rabl-rails/renderers/json.rb
100
114
  - lib/rabl-rails/renderers/plist.rb
101
115
  - lib/rabl-rails/renderers/xml.rb
102
- - lib/rabl-rails/responder.rb
103
116
  - lib/rabl-rails/template.rb
104
117
  - lib/rabl-rails/version.rb
105
118
  - lib/rabl-rails/visitors.rb
@@ -117,7 +130,6 @@ files:
117
130
  - test/test_hash_visitor.rb
118
131
  - test/test_helpers.rb
119
132
  - test/test_library.rb
120
- - test/test_render.rb
121
133
  homepage: https://github.com/ccocchi/rabl-rails
122
134
  licenses:
123
135
  - MIT
@@ -130,7 +142,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
130
142
  requirements:
131
143
  - - ">="
132
144
  - !ruby/object:Gem::Version
133
- version: '0'
145
+ version: 2.2.0
134
146
  required_rubygems_version: !ruby/object:Gem::Requirement
135
147
  requirements:
136
148
  - - ">="
@@ -138,10 +150,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
138
150
  version: '0'
139
151
  requirements: []
140
152
  rubyforge_project:
141
- rubygems_version: 2.5.1
153
+ rubygems_version: 2.4.5.2
142
154
  signing_key:
143
155
  specification_version: 4
144
- summary: Fast Rails 3+ templating system with JSON, XML and PList support
156
+ summary: Fast Rails 4+ templating system with JSON, XML and PList support
145
157
  test_files:
146
158
  - test/helper.rb
147
159
  - test/renderers/test_hash_renderer.rb
@@ -153,4 +165,3 @@ test_files:
153
165
  - test/test_hash_visitor.rb
154
166
  - test/test_helpers.rb
155
167
  - test/test_library.rb
156
- - test/test_render.rb
@@ -1,9 +0,0 @@
1
- module RablRails
2
- module Nodes
3
- module Node
4
- def accept(visitor)
5
- visitor.visit(self)
6
- end
7
- end
8
- end
9
- end
@@ -1,97 +0,0 @@
1
- require 'rabl-rails/renderers/hash'
2
- require 'rabl-rails/renderers/json'
3
- require 'rabl-rails/renderers/xml'
4
- require 'rabl-rails/renderers/plist'
5
-
6
- module RablRails
7
- module Renderer
8
- class TemplateNotFound < StandardError; end
9
- class PartialError < StandardError; end
10
-
11
- class LookupContext
12
- T = Struct.new(:source)
13
-
14
- def initialize(view_path, format)
15
- @view_path = view_path || 'app/views'
16
- @format = format.downcase
17
- end
18
-
19
- def rendered_format
20
- @format.to_sym
21
- end
22
-
23
- #
24
- # Manually find given rabl template file with given format.
25
- # View path can be set via options, otherwise default Rails
26
- # path is used
27
- #
28
- def find_template(name, opt, partial = false)
29
- paths = Dir["#@view_path/#{name}{.#@format,}.rabl"]
30
- file_path = paths.find { |path| File.exist?(path) }
31
-
32
- if file_path
33
- T.new(File.read(file_path))
34
- else
35
- raise TemplateNotFound
36
- end
37
- end
38
- end
39
-
40
- #
41
- # Context class to emulate normal Rails view
42
- # context
43
- #
44
- class ViewContext
45
- attr_reader :format
46
-
47
- def initialize(path, options)
48
- @virtual_path = path
49
- @format = options.delete(:format) || :json
50
- @_assigns = {}
51
- @options = options
52
-
53
- options[:locals].each { |k, v| @_assigns[k.to_s] = v } if options[:locals]
54
- end
55
-
56
- def assigns
57
- @_assigns
58
- end
59
-
60
- def params
61
- { format: format }
62
- end
63
-
64
- def lookup_context
65
- @lookup_context ||= LookupContext.new(@options[:view_path], format)
66
- end
67
- end
68
-
69
- #
70
- # Renders object with the given rabl template.
71
- #
72
- # Object can also be passed as an option :
73
- # { locals: { object: obj_to_render } }
74
- #
75
- # Default render format is JSON, but can be changed via
76
- # an option: { format: 'xml' }
77
- #
78
- # If template includes uses of instance variables (usually
79
- # defined in the controller), you can passed them as locals
80
- # options.
81
- # For example, if you have this template:
82
- # object :@user
83
- # node(:read) { |u| u.has_read?(@post) }
84
- #
85
- # Your method call should look like this:
86
- # RablRails.render(user, 'users/show', locals: { post: Post.new })
87
- #
88
- def render(object, template, options = {})
89
- object = options[:locals].delete(:object) if !object && options[:locals]
90
-
91
- c = ViewContext.new(template, options)
92
- t = c.lookup_context.find_template(template, [], false)
93
-
94
- Library.instance.get_rendered_template(t.source, c, resource: object)
95
- end
96
- end
97
- end
@@ -1,47 +0,0 @@
1
- module RablRails
2
- #
3
- # Override default responder's api behavior to not
4
- # user to_format methods on a resource as a default
5
- # representation but instead use a rabl template
6
- #
7
- class Responder < ActionController::Responder
8
- def initialize(controller, resources, options = {})
9
- super
10
- if options[:locals]
11
- options[:locals][:resource] = resource
12
- else
13
- options[:locals] = { resource: resource }
14
- end
15
- end
16
-
17
- def to_format
18
- if get? || response_overridden?
19
- default_render
20
- elsif has_errors?
21
- display_errors
22
- else
23
- api_behavior
24
- end
25
- end
26
-
27
- protected
28
-
29
- def api_behavior
30
- if post?
31
- template = if controller.respond_to?(:responder_default_template, true)
32
- controller.send(:responder_default_template)
33
- else
34
- RablRails.configuration.responder_default_template
35
- end
36
- options[:prefixes] = controller._prefixes
37
- options[:template] ||= template
38
-
39
- controller.default_render options.merge(status: :created)
40
- else
41
- head :no_content
42
- end
43
- rescue ActionView::MissingTemplate => e
44
- super
45
- end
46
- end
47
- end
data/test/test_render.rb DELETED
@@ -1,86 +0,0 @@
1
- require 'helper'
2
- require 'pathname'
3
- require 'tmpdir'
4
-
5
- class TestRender < MINITEST_TEST_CLASS
6
- @@tmp_path = Pathname.new(Dir.mktmpdir)
7
-
8
- def setup
9
- @user = User.new(1, 'Marty')
10
- @tmp_path = @@tmp_path
11
- end
12
-
13
- def test_object_as_option
14
- File.open(@tmp_path + "nil.json.rabl", "w") do |f|
15
- f.puts %q{
16
- object :@user
17
- attributes :name
18
- }
19
- end
20
- assert_equal %q({"user":{"name":"Marty"}}), RablRails.render(nil, 'nil', locals: { object: @user }, view_path: @tmp_path)
21
- end
22
-
23
- def test_load_source_from_file
24
- File.open(@tmp_path + "show.json.rabl", "w") do |f|
25
- f.puts %q{
26
- object :@user
27
- attributes :id, :name
28
- }
29
- end
30
- assert_equal %q({"user":{"id":1,"name":"Marty"}}), RablRails.render(@user, 'show', view_path: @tmp_path)
31
- end
32
-
33
- def test_template_not_found
34
- assert_raises(RablRails::Renderer::TemplateNotFound) { RablRails.render(@user, 'not_found') }
35
- end
36
-
37
- def test_with_locals_options
38
- File.open(@tmp_path + "instance.json.rabl", "w") do |f|
39
- f.puts %q{
40
- object false
41
- node(:username) { |_| @user.name }
42
- }
43
- end
44
- assert_equal %q({"username":"Marty"}), RablRails.render(nil, 'instance', view_path: @tmp_path, locals: { user: @user })
45
- end
46
-
47
- def test_extend_with_view_path
48
- File.open(@tmp_path + "extend.json.rabl", "w") do |f|
49
- f.puts %q{
50
- object :@user
51
- extends 'base'
52
- }
53
- end
54
-
55
- File.open(@tmp_path + "base.json.rabl", "w") do |f|
56
- f.puts %q{
57
- attribute :name, as: :extended_name
58
- }
59
- end
60
-
61
- assert_equal %q({"user":{"extended_name":"Marty"}}), RablRails.render(@user, 'extend', view_path: @tmp_path)
62
- end
63
-
64
- def test_format_as_symbol_or_string
65
- File.open(@tmp_path + "show.json.rabl", "w") do |f|
66
- f.puts %q{
67
- object :@user
68
- attributes :id, :name
69
- }
70
- end
71
-
72
- assert_equal %q({"user":{"id":1,"name":"Marty"}}), RablRails.render(@user, 'show', view_path: @tmp_path, format: :json)
73
- assert_equal %q({"user":{"id":1,"name":"Marty"}}), RablRails.render(@user, 'show', view_path: @tmp_path, format: 'json')
74
- assert_equal %q({"user":{"id":1,"name":"Marty"}}), RablRails.render(@user, 'show', view_path: @tmp_path, format: 'JSON')
75
- end
76
-
77
- def test_format_omitted
78
- File.open(@tmp_path + "show.rabl", "w") do |f|
79
- f.puts %q{
80
- object :@user
81
- attributes :id, :name
82
- }
83
- end
84
- assert_equal %q({"user":{"id":1,"name":"Marty"}}), RablRails.render(@user, 'show', view_path: @tmp_path)
85
- end
86
- end