Class: Props::PartialRenderer
- Inherits:
-
ActionView::AbstractRenderer
- Object
- ActionView::AbstractRenderer
- Props::PartialRenderer
- Defined in:
- lib/props_template/extensions/partial_renderer.rb
Constant Summary collapse
- OPTION_AS_ERROR_MESSAGE =
"The value (%s) of the option `as` is not a valid Ruby identifier; " \ "make sure it starts with lowercase letter, " \ "and is followed by any combination of letters, numbers and underscores."
- IDENTIFIER_ERROR_MESSAGE =
"The partial name (%s) is not a valid Ruby identifier; " \ "make sure your partial name starts with underscore."
- INVALID_PARTIAL_MESSAGE =
"The partial name must be a string, but received (%s)."
Instance Attribute Summary collapse
-
#template ⇒ Object
readonly
Returns the value of attribute template.
Class Method Summary collapse
- .find_and_add_template(builder, context, all_options) ⇒ Object
- .raise_invalid_identifier(path) ⇒ Object
- .raise_invalid_option_as(as) ⇒ Object
- .refine_options(options, item = nil) ⇒ Object
- .retrieve_variable(path) ⇒ Object
Instance Method Summary collapse
-
#initialize(context, options) ⇒ PartialRenderer
constructor
A new instance of PartialRenderer.
- #render(template, options) ⇒ Object
Constructor Details
#initialize(context, options) ⇒ PartialRenderer
Returns a new instance of PartialRenderer.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/props_template/extensions/partial_renderer.rb', line 124 def initialize(context, ) @context = context super(@context.lookup_context) = .merge(formats: [:json]) .delete(:handlers) @details = extract_details() partial = [:partial] if !(String === partial) raise_invalid_partial(partial.inspect) end @path = partial @context_prefix = @lookup_context.prefixes.first template_keys = retrieve_template_keys() @template = find_template(@path, template_keys) end |
Instance Attribute Details
#template ⇒ Object (readonly)
Returns the value of attribute template.
122 123 124 |
# File 'lib/props_template/extensions/partial_renderer.rb', line 122 def template @template end |
Class Method Details
.find_and_add_template(builder, context, all_options) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/props_template/extensions/partial_renderer.rb', line 60 def self.find_and_add_template(builder, context, ) first_opts = [0] if first_opts[:partial] partial_opts = block_opts_to_render_opts(builder, first_opts) renderer = new(context, partial_opts) .map do |opts| opts[:_template] = renderer.template opts end else end end |
.raise_invalid_identifier(path) ⇒ Object
80 81 82 |
# File 'lib/props_template/extensions/partial_renderer.rb', line 80 def self.raise_invalid_identifier(path) raise ArgumentError.new(IDENTIFIER_ERROR_MESSAGE % (path)) end |
.raise_invalid_option_as(as) ⇒ Object
76 77 78 |
# File 'lib/props_template/extensions/partial_renderer.rb', line 76 def self.raise_invalid_option_as(as) raise ArgumentError.new(OPTION_AS_ERROR_MESSAGE % (as)) end |
.refine_options(options, item = nil) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/props_template/extensions/partial_renderer.rb', line 91 def self.(, item = nil) return if ![:partial] partial, rest = [*[:partial]] rest = (rest || {}).clone locals = rest[:locals] || {} rest[:locals] = locals if item as = if !rest[:as] retrieve_variable(partial) else rest[:as].to_sym end raise_invalid_option_as(as) unless /\A[a-z_]\w*\z/.match?(as.to_s) locals[as] = item if fragment_name = rest[:fragment] fragment_name = Proc === fragment_name ? fragment_name.call(item) : fragment_name.to_s rest[:fragment] = fragment_name end end pass_opts = .clone pass_opts[:partial] = [partial, rest] pass_opts end |
.retrieve_variable(path) ⇒ Object
85 86 87 88 89 |
# File 'lib/props_template/extensions/partial_renderer.rb', line 85 def self.retrieve_variable(path) base = path[-1] == "/" ? "" : File.basename(path) raise_invalid_identifier(path) unless base =~ /\A_?(.*?)(?:\.\w+)*\z/ $1.to_sym end |
Instance Method Details
#render(template, options) ⇒ Object
143 144 145 146 147 |
# File 'lib/props_template/extensions/partial_renderer.rb', line 143 def render(template, ) #remove this later render_partial(template, @context, ) end |