Class: PbbuilderTemplate
- Defined in:
- lib/pbbuilder/template.rb
Overview
PbbuilderTemplate is an extension of Pbbuilder to be used as a Rails template It adds support for partials.
Class Attribute Summary collapse
-
.template_lookup_options ⇒ Object
Returns the value of attribute template_lookup_options.
Instance Method Summary collapse
-
#cache!(key = nil, options = {}) ⇒ Object
Caches a fragment of a message with a given cache key.
-
#cache_if!(condition, *args, &block) ⇒ Object
Conditionally caches the protobuf message depending on the condition given as first parameter.
-
#initialize(context, message) ⇒ PbbuilderTemplate
constructor
A new instance of PbbuilderTemplate.
-
#partial!(*args) ⇒ Object
Render a partial.
-
#set!(field, *args, **kwargs, &block) ⇒ Object
Sets the value in the message field.
Methods inherited from Pbbuilder
#attributes!, #extract!, #merge!, #method_missing, #new_message_for, #respond_to_missing?, #target!
Constructor Details
#initialize(context, message) ⇒ PbbuilderTemplate
Returns a new instance of PbbuilderTemplate.
14 15 16 17 |
# File 'lib/pbbuilder/template.rb', line 14 def initialize(context, ) @context = context super() end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Pbbuilder
Class Attribute Details
.template_lookup_options ⇒ Object
Returns the value of attribute template_lookup_options.
9 10 11 |
# File 'lib/pbbuilder/template.rb', line 9 def @template_lookup_options end |
Instance Method Details
#cache!(key = nil, options = {}) ⇒ Object
Caches a fragment of a message with a given cache key. Can be called like the following: ‘pb.cache! “cache-key” do; end’ ‘pb.cache! “cache-key”, expire_in: 1.min do; end’
83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/pbbuilder/template.rb', line 83 def cache!(key=nil, ={}) if @context.controller.perform_caching value = _cache_fragment_for(key, ) do _scope(target!) { yield self }.to_h.compact_blank end merge! value else yield end end |
#cache_if!(condition, *args, &block) ⇒ Object
Conditionally caches the protobuf message depending on the condition given as first parameter. Has the same signature as the cache helper method in ActionView::Helpers::CacheHelper and so can be used in the same way.
Example:
pb.cache_if! !admin?, @person, expires_in: 10.minutes do
pb.extract! @person, :name, :age
end
104 105 106 |
# File 'lib/pbbuilder/template.rb', line 104 def cache_if!(condition, *args, &block) condition ? cache!(*args, &block) : yield end |
#partial!(*args) ⇒ Object
Render a partial. Can be called as:
pb.partial! “name/of_partial”, argument: 123 pb.partial! “name/of_partial”, locals: 123 pb.partial! partial: “name/of_partial”, argument: 123 pb.partial! partial: “name/of_partial”, locals: 123 pb.partial! @model # @model is an ActiveModel value, it will use the name to look up a partial
26 27 28 29 30 31 32 |
# File 'lib/pbbuilder/template.rb', line 26 def partial!(*args) if args.one? && _is_active_model?(args.first) _render_active_model_partial args.first else _render_explicit_partial(*args) end end |
#set!(field, *args, **kwargs, &block) ⇒ Object
Sets the value in the message field.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/pbbuilder/template.rb', line 42 def set!(field, *args, **kwargs, &block) # If any partial options are being passed, we render a submessage with a partial if kwargs.has_key?(:partial) if args.one? && kwargs.has_key?(:as) # Example syntax that should end up here: # pb.friends @friends, partial: "friend", as: :friend # Call set! on the super class, passing in a block that renders a partial for every element super(field, *args) do |element| _set_inline_partial(element, kwargs) end elsif kwargs.has_key?(:collection) && kwargs.has_key?(:as) # Example syntax that should end up here: # pb.friends partial: "racers/racer", as: :racer, collection: [Racer.new(1, "Johnny Test", []), Racer.new(2, "Max Verstappen", [])] (field, kwargs[:collection], kwargs) else # Example syntax that should end up here: # pb.best_friend partial: "person", person: @best_friend super(field, *args) do (kwargs) end end else if args.one? && kwargs.has_key?(:collection) && kwargs.has_key?(:as) # Example syntax that should end up here: # pb.friends "racers/racer", as: :racer, collection: [Racer.new(1, "Johnny Test", []), Racer.new(2, "Max Verstappen", [])] (field, kwargs[:collection], kwargs.merge(partial: args.first)) else super end end end |