rabl-rails 0.4.3 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dfd78996e96f1bf4dc99f2ffe80c69b57e1d7c72
4
- data.tar.gz: e7c0c4bcf0d10beba1a814962ac15fd5c55434e7
3
+ metadata.gz: de487a654a879918209db30bfb6d384b26e8bc0e
4
+ data.tar.gz: 54e73001a009a25491238d246da5e0a09b79d66f
5
5
  SHA512:
6
- metadata.gz: b75c2a1b05cd16037797aa5a2eedb63041be6e0670cef0e0658cadd709e514e96d718d52f6d4e7b616873e91e280439eb08f7a09c4409eebc65ebe5c95f8a704
7
- data.tar.gz: 87a2345a2b7b02dda10ceadfedbe6e231f3af9fd365fd4ab4d35019184ce7318420d5c92cb02b0013b2a591d0f6765c24cc75b50ea5cf4258d161665fd28bd76
6
+ metadata.gz: 21fdf8c8e8f80b1d6807dc0f082e3db64b29c24329010c63f4979245bbe10752c8122f527728311113befde3f766a2e981639473d879d7888d584e53dc70a24c
7
+ data.tar.gz: 26511a368b88f055e7c9f8a63253239be920714f1d19740ac82cc060899f5aa8686fbf51062be76c1c8819d8ac7139350809a76f5c2e09b83718c7e8ba37220a
data/.travis.yml CHANGED
@@ -1,14 +1,21 @@
1
1
  language: ruby
2
+ cache: bundler
3
+ dist: trusty
2
4
  env:
3
- - "RAILS_VERSION=3.2.0"
4
- - "RAILS_VERSION=4.0.0"
5
- - "RAILS_VERSION=4.1.0"
6
- - "RAILS_VERSION=4.2.0"
5
+ - "RAILS_VERSION=4.2.6"
6
+ - "RAILS_VERSION=master"
7
7
  rvm:
8
- - 1.9.3
9
- - jruby-19mode
10
- - rbx-2
11
- - 2.0.0
12
- - 2.1.8
8
+ - 2.2.7
9
+ - 2.3.4
10
+ - 2.4.1
11
+ - jruby-9.1.12.0
12
+ - rbx-3.71
13
13
  before_install:
14
14
  - gem update bundler
15
+ matrix:
16
+ fast_finish: true
17
+ allow_failures:
18
+ - env: RAILS_VERSION=master
19
+ exclude:
20
+ - rvm: 2.1.8
21
+ env: "RAILS_VERSION=master"
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.5.0
4
+ * Add requirement ruby >= 2.2
5
+ * Drop support for Rails < 4.2
6
+ * Replace `thread_safe` with `concurrent_ruby`
7
+ * Remove custom responder
8
+ * Remove rendering outside of Rails
9
+ * Improve Rails 5 compatibility
10
+
3
11
  ## 0.4.3
4
12
  * Fix custom responder compatibility with responders 2.1 (itkin)
5
13
  * Fix bug when template was already loaded by ActionView and causing a nil
data/Gemfile CHANGED
@@ -8,33 +8,25 @@ rails = case rails_version
8
8
  when 'master'
9
9
  { github: 'rails/rails' }
10
10
  when "default"
11
- '~> 4.2.0'
11
+ '~> 4.2.5'
12
12
  else
13
13
  "~> #{rails_version}"
14
14
  end
15
15
 
16
- minitest_version = rails_version == '4.0.0' ? '~> 4.7' : '~> 5.4'
17
-
18
16
  gem 'activesupport', rails
19
17
  gem 'railties', rails
20
18
 
21
19
  group :test do
22
- gem 'minitest', minitest_version
20
+ gem 'minitest', '~> 5.8'
23
21
  gem 'actionpack', rails
24
-
25
- if rails_version > '4.0.0'
26
- gem 'actionview', rails
27
- end
22
+ gem 'actionview', rails
28
23
  end
29
24
 
30
25
  gem 'plist'
31
26
 
32
- platforms :ruby do
33
- gem 'oj'
34
- end
35
-
36
27
  platforms :mri do
37
28
  gem 'libxml-ruby'
29
+ gem 'oj'
38
30
  end
39
31
 
40
32
  platforms :jruby do
data/README.md CHANGED
@@ -4,7 +4,7 @@ RABL (Ruby API Builder Language) is a ruby templating system for rendering resou
4
4
 
5
5
  rabl-rails is **faster** and uses **less memory** than the standard rabl gem while letting you access the same features. There are some slight changes to do on your templates to get this gem to work but it should't take you more than 5 minutes.
6
6
 
7
- rabl-rails only targets **Rails 3.2+ application** and is compatible with mri 1.9.3+, jRuby and rubinius.
7
+ rabl-rails only targets **Rails 4.2+ application** and is compatible with mri 2.2+, jRuby and rubinius.
8
8
 
9
9
  ## Installation
10
10
 
@@ -93,8 +93,6 @@ RablRails.configure do |config|
93
93
  # config.include_json_root = true
94
94
  # config.json_engine = ::Oj
95
95
  # config.xml_options = { :dasherize => true, :skip_types => false }
96
- # config.use_custom_responder = false
97
- # config.default_responder_template = 'show'
98
96
  # config.enable_jsonp_callbacks = false
99
97
  # config.replace_nil_values_with_empty_strings = false
100
98
  # config.replace_empty_string_values_with_nil = false
@@ -296,28 +294,17 @@ child :posts do
296
294
  end
297
295
  ```
298
296
 
299
- ### Render object directly
300
-
301
- There are cases when you want to render object outside Rails view context. For instance to render objects in the console or to create message queue payloads. For these situations, you can use `RablRails.render` as show below:
302
-
303
- ```ruby
304
- RablRails.render(object, template, :view_path => 'app/views', :format => :json) #=> "{...}"
305
- ```
306
-
307
- You can find more informations about how to use this method in the [wiki](http://github.com/ccocchi/rabl-rails/wiki/Render-object-directly)
308
-
309
297
  ### Other features
310
298
 
311
299
  * [Caching](https://github.com/ccocchi/rabl-rails/wiki/Caching)
312
- * [Custom responder](https://github.com/ccocchi/rabl-rails/wiki/Using-custom-responder)
313
300
 
314
301
  And more in the [WIKI](https://github.com/ccocchi/rabl-rails/wiki)
315
302
 
316
303
  ## Performance
317
304
 
318
- Benchmarks have been made using this [application](http://github.com/ccocchi/rabl-benchmark), with rabl 0.7.6 and rabl-rails 0.3.0
305
+ Benchmarks have been made using this [application](http://github.com/ccocchi/rabl-benchmark), with rabl 0.13.1 and rabl-rails 0.5.0
319
306
 
320
- Overall, Rabl-rails is **20% faster and use 10% less memory**, even **twice faster** when using extends.
307
+ Overall, rabl-rails is **10% faster and use 10% less memory**, but these numbers skyrockets to **50%** when using `extends` with collection of objects.
321
308
 
322
309
  You can see full tests on test application repository.
323
310
 
@@ -330,9 +317,8 @@ Want to make another change ? Just fork and contribute, any help is very much ap
330
317
 
331
318
  ## Original idea
332
319
 
333
- * [RABL](http://github.com/nesquena/rabl) Standart RABL gem. I used it a lot but I needed to improve my API response time, and
334
- since most of the time was spent in view rendering, I decided to implement a faster rabl gem.
320
+ * [RABL](http://github.com/nesquena/rabl) Standart RABL gem. I used it a lot but I needed to improve my API response time, and since most of the time was spent in view rendering, I decided to implement a faster rabl gem.
335
321
 
336
322
  ## Copyright
337
323
 
338
- Copyright © 2011-2012 Christopher Cocchi-Perrier. See [MIT-LICENSE](http://github.com/ccocchi/rabl-rails/blob/master/MIT-LICENSE) for details.
324
+ Copyright © 2012-2017 Christopher Cocchi-Perrier. See [MIT-LICENSE](http://github.com/ccocchi/rabl-rails/blob/master/MIT-LICENSE) for details.
data/lib/rabl-rails.rb CHANGED
@@ -1,19 +1,25 @@
1
- require 'rails/railtie'
2
-
3
1
  require 'active_support'
4
2
 
5
3
  require 'rabl-rails/version'
6
4
  require 'rabl-rails/helpers'
5
+ require 'rabl-rails/exceptions'
7
6
  require 'rabl-rails/template'
8
7
  require 'rabl-rails/nodes'
9
8
  require 'rabl-rails/compiler'
10
9
 
11
10
  require 'rabl-rails/visitors'
12
- require 'rabl-rails/renderer'
11
+ require 'rabl-rails/renderers/hash'
12
+ require 'rabl-rails/renderers/json'
13
+ require 'rabl-rails/renderers/xml'
14
+ require 'rabl-rails/renderers/plist'
13
15
  require 'rabl-rails/library'
14
16
 
15
17
  require 'rabl-rails/handler'
16
- require 'rabl-rails/railtie'
18
+
19
+ if defined?(Rails)
20
+ require 'rails/railtie'
21
+ require 'rabl-rails/railtie'
22
+ end
17
23
 
18
24
  require 'rabl-rails/configuration'
19
25
 
@@ -25,8 +31,6 @@ rescue LoadError
25
31
  end
26
32
 
27
33
  module RablRails
28
- extend Renderer
29
-
30
34
  class << self
31
35
  def configure
32
36
  yield configuration
@@ -68,16 +68,9 @@ module RablRails
68
68
  # child(:posts, :partial => 'posts/base')
69
69
  #
70
70
  def child(name_or_data, options = {})
71
- data, name = extract_data_and_name(name_or_data)
72
- name = options[:root] if options.has_key? :root
73
-
74
- if options.key?(:partial)
75
- template = Library.instance.compile_template_from_path(options[:partial], @view)
76
- template.data = data
77
- elsif block_given?
78
- template = sub_compile(data) { yield }
79
- end
80
-
71
+ data, name = extract_data_and_name(name_or_data)
72
+ name = options[:root] if options.has_key? :root
73
+ template = partial_or_block(data, options) { yield }
81
74
  @template.add_node Nodes::Child.new(name, template)
82
75
  end
83
76
 
@@ -86,10 +79,8 @@ module RablRails
86
79
  # Example:
87
80
  # glue(:@user) { attribute :name }
88
81
  #
89
- def glue(data)
90
- return unless block_given?
91
-
92
- template = sub_compile(data) { yield }
82
+ def glue(data, options = {})
83
+ template = partial_or_block(data, options) { yield }
93
84
  @template.add_node Nodes::Glue.new(template)
94
85
  end
95
86
 
@@ -113,18 +104,25 @@ module RablRails
113
104
  # Example:
114
105
  # merge { |item| partial("specific/#{item.to_s}", object: item) }
115
106
  #
116
- def merge
107
+ def merge(opts = {})
117
108
  return unless block_given?
118
- node(nil) { yield }
109
+ node(nil, opts) { yield }
119
110
  end
120
111
 
121
112
  #
122
113
  # Extends an existing rabl template
123
114
  # Example:
124
115
  # extends 'users/base'
116
+ # extends 'posts/base', locals: { hide_comments: true }
125
117
  #
126
- def extends(path)
127
- @template.extends Library.instance.compile_template_from_path(path, @view)
118
+ def extends(path, options = nil)
119
+ other = Library.instance.compile_template_from_path(path, @view)
120
+
121
+ if options && options.is_a?(Hash)
122
+ @template.add_node Nodes::Extend.new(other.nodes, options[:locals])
123
+ else
124
+ @template.extends(other)
125
+ end
128
126
  end
129
127
 
130
128
  #
@@ -138,6 +136,7 @@ module RablRails
138
136
  return unless block_given?
139
137
  @template.add_node Nodes::Condition.new(proc, sub_compile(nil, true) { yield })
140
138
  end
139
+ alias_method :_if, :condition
141
140
 
142
141
  def cache(&block)
143
142
  @template.cache_key = block_given? ? block : nil
@@ -145,6 +144,16 @@ module RablRails
145
144
 
146
145
  protected
147
146
 
147
+ def partial_or_block(data, options)
148
+ if options.key?(:partial)
149
+ template = Library.instance.compile_template_from_path(options[:partial], @view)
150
+ template.data = data
151
+ template
152
+ elsif block_given?
153
+ sub_compile(data) { yield }
154
+ end
155
+ end
156
+
148
157
  #
149
158
  # Extract data root_name and root name
150
159
  # Example:
@@ -6,8 +6,6 @@ module RablRails
6
6
  attr_accessor :xml_options
7
7
  attr_accessor :plist_engine, :include_plist_root
8
8
  attr_accessor :cache_templates
9
- attr_reader :use_custom_responder
10
- attr_accessor :responder_default_template
11
9
  attr_accessor :replace_nil_values_with_empty_strings
12
10
  attr_accessor :replace_empty_string_values_with_nil
13
11
  attr_accessor :exclude_nil_values
@@ -25,9 +23,6 @@ module RablRails
25
23
 
26
24
  @cache_templates = ActionController::Base.perform_caching
27
25
 
28
- @use_custom_responder = false
29
- @responder_default_template = 'show'
30
-
31
26
  @replace_nil_values_with_empty_strings = false
32
27
  @replace_empty_string_values_with_nil = false
33
28
  @exclude_nil_values = false
@@ -35,11 +30,6 @@ module RablRails
35
30
  @non_collection_classes = Set.new(['Struct'])
36
31
  end
37
32
 
38
- def use_custom_responder=(value)
39
- @use_custom_responder = value
40
- require 'rabl-rails/responder' if value
41
- end
42
-
43
33
  def result_flags
44
34
  @result_flags ||= begin
45
35
  result = 0
@@ -0,0 +1,3 @@
1
+ module RablRails
2
+ class PartialError < StandardError; end
3
+ end
@@ -1,6 +1,5 @@
1
1
  require 'singleton'
2
- require 'monitor'
3
- require 'thread_safe'
2
+ require 'concurrent/map'
4
3
 
5
4
  module RablRails
6
5
  class Library
@@ -16,12 +15,11 @@ module RablRails
16
15
  }.freeze
17
16
 
18
17
  def initialize
19
- @cached_templates = ThreadSafe::Cache.new
20
- @mutex = Monitor.new
18
+ @cached_templates = Concurrent::Map.new
21
19
  end
22
20
 
23
21
  def reset_cache!
24
- @cached_templates = ThreadSafe::Cache.new
22
+ @cached_templates = Concurrent::Map.new
25
23
  end
26
24
 
27
25
  def get_rendered_template(source, view, locals = nil)
@@ -52,14 +50,9 @@ module RablRails
52
50
  private
53
51
 
54
52
  def synchronized_compile(path, source, view)
55
- @cached_templates[path] || @mutex.synchronize do
56
- # Any thread holding this lock will be compiling the template needed
57
- # by the threads waiting. So re-check the template presence to avoid
58
- # re-compilation
59
- @cached_templates.fetch(path) do
60
- source ||= fetch_source(path, view)
61
- @cached_templates[path] = compile(source, view)
62
- end
53
+ @cached_templates.compute_if_absent(path) do
54
+ source ||= fetch_source(path, view)
55
+ compile(source, view)
63
56
  end
64
57
  end
65
58
 
@@ -1,6 +1,6 @@
1
- require 'rabl-rails/nodes/node'
2
1
  require 'rabl-rails/nodes/attribute'
3
2
  require 'rabl-rails/nodes/glue'
4
3
  require 'rabl-rails/nodes/child'
5
4
  require 'rabl-rails/nodes/code'
6
5
  require 'rabl-rails/nodes/condition'
6
+ require 'rabl-rails/nodes/extend'
@@ -1,18 +1,20 @@
1
- require 'forwardable'
2
-
3
1
  module RablRails
4
2
  module Nodes
5
3
  class Attribute
6
- include Node
7
- extend Forwardable
8
-
9
- def_delegators :@hash, :[]=, :each
10
4
  attr_reader :hash
11
5
  attr_accessor :condition
12
6
 
13
7
  def initialize(hash = {})
14
8
  @hash = hash
15
9
  end
10
+
11
+ def []=(key, value)
12
+ @hash[key] = value
13
+ end
14
+
15
+ def each(&block)
16
+ @hash.each(&block)
17
+ end
16
18
  end
17
19
  end
18
20
  end
@@ -1,8 +1,6 @@
1
1
  module RablRails
2
2
  module Nodes
3
3
  class Code
4
- include Node
5
-
6
4
  attr_reader :name, :block, :condition
7
5
 
8
6
  def initialize(name, block, condition = nil)
@@ -16,4 +14,4 @@ module RablRails
16
14
  end
17
15
  end
18
16
  end
19
- end
17
+ end
@@ -1,8 +1,6 @@
1
1
  module RablRails
2
2
  module Nodes
3
3
  class Condition
4
- include Node
5
-
6
4
  attr_reader :condition, :nodes
7
5
 
8
6
  def initialize(condition, nodes)
@@ -11,4 +9,4 @@ module RablRails
11
9
  end
12
10
  end
13
11
  end
14
- end
12
+ end
@@ -0,0 +1,12 @@
1
+ module RablRails
2
+ module Nodes
3
+ class Extend
4
+ attr_reader :nodes, :locals
5
+
6
+ def initialize(nodes, locals)
7
+ @nodes = nodes
8
+ @locals = locals
9
+ end
10
+ end
11
+ end
12
+ end
@@ -1,25 +1,17 @@
1
1
  module RablRails
2
2
  module Nodes
3
3
  class Glue
4
- include Node
5
-
6
- attr_reader :template
4
+ attr_reader :nodes, :data
7
5
 
8
6
  def initialize(template)
9
- @template = template
10
- end
11
-
12
- def data
13
- @template.data
14
- end
15
-
16
- def nodes
17
- @template.nodes
7
+ @nodes = template.nodes
8
+ @data = template.data
9
+ @is_var = @data.to_s.start_with?('@')
18
10
  end
19
11
 
20
12
  def instance_variable_data?
21
- @instance_variable_data ||= data.to_s.start_with?('@')
13
+ @is_var
22
14
  end
23
15
  end
24
16
  end
25
- end
17
+ end