Class: Blocks::Builder
- Inherits:
-
Object
- Object
- Blocks::Builder
- Includes:
- HamlCapture
- Defined in:
- lib/blocks/builders/builder.rb
Instance Attribute Summary collapse
-
#anonymous_block_number ⇒ Object
Returns the value of attribute anonymous_block_number.
-
#block_definitions ⇒ Object
A HashWithIndifferentAccess of block names to BlockDefinition mappings.
-
#options ⇒ Object
Options provided during initialization of builder.
-
#view ⇒ Object
A pointer to the view context.
Instance Method Summary collapse
- #block_defined?(block_name) ⇒ Boolean
- #block_for(block_name) ⇒ Object
- #capture(*args, &block) ⇒ Object
-
#concatenating_merge(options, options2, *args) ⇒ Object
TODO: move this logic elsewhere.
-
#content_tag(*args, &block) ⇒ Object
Blocks::Builder#content_tag extends ActionView’s content_tag method by allowing itself to be used as a wrapper, hook, or called directly, while also not requiring the content tag name (defaults to :div).
- #deferred_render(*args, &block) ⇒ Object
-
#define(*args, &block) ⇒ Object
Define a block, unless a block by the same name is already defined.
- #hooks_for(block_name, hook_name) ⇒ Object
-
#initialize(view, options = nil) ⇒ Builder
constructor
A new instance of Builder.
- #render(*args, &block) ⇒ Object
- #render_with_overrides(*args, &block) ⇒ Object
-
#replace(name, options = {}, &block) ⇒ Object
Define a block, replacing an existing block by the same name if it is already defined.
- #skip(name, completely = false) ⇒ Object
- #skip_completely(name) ⇒ Object
Constructor Details
#initialize(view, options = nil) ⇒ Builder
Returns a new instance of Builder.
22 23 24 25 26 27 28 29 |
# File 'lib/blocks/builders/builder.rb', line 22 def initialize(view, =nil) self.view = view self.block_definitions = HashWithIndifferentAccess.new do |hash, key| hash[key] = BlockDefinition.new(key); hash[key] end self.anonymous_block_number = 0 self. = end |
Instance Attribute Details
#anonymous_block_number ⇒ Object
Returns the value of attribute anonymous_block_number.
18 19 20 |
# File 'lib/blocks/builders/builder.rb', line 18 def anonymous_block_number @anonymous_block_number end |
#block_definitions ⇒ Object
A HashWithIndifferentAccess of block names to BlockDefinition mappings
13 14 15 |
# File 'lib/blocks/builders/builder.rb', line 13 def block_definitions @block_definitions end |
#options ⇒ Object
Options provided during initialization of builder
16 17 18 |
# File 'lib/blocks/builders/builder.rb', line 16 def @options end |
#view ⇒ Object
A pointer to the view context
10 11 12 |
# File 'lib/blocks/builders/builder.rb', line 10 def view @view end |
Instance Method Details
#block_defined?(block_name) ⇒ Boolean
61 62 63 |
# File 'lib/blocks/builders/builder.rb', line 61 def block_defined?(block_name) block_definitions.key?(block_name) end |
#block_for(block_name) ⇒ Object
44 45 46 |
# File 'lib/blocks/builders/builder.rb', line 44 def block_for(block_name) block_definitions[block_name] if block_defined?(block_name) end |
#capture(*args, &block) ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/blocks/builders/builder.rb', line 52 def capture(*args, &block) if block.arity >= 0 args = args[0, block.arity] end with_output_buffer do output_buffer << view.capture(*args, &block) end end |
#concatenating_merge(options, options2, *args) ⇒ Object
TODO: move this logic elsewhere
131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/blocks/builders/builder.rb', line 131 def concatenating_merge(, , *args) = call_each_hash_value_with_params(, *args) || {} = call_each_hash_value_with_params(, *args) || {} .merge() do |key, v1, v2| if v1.is_a?(String) && v2.is_a?(String) "#{v1} #{v2}" else v2 end end end |
#content_tag(*args, &block) ⇒ Object
Blocks::Builder#content_tag extends ActionView’s content_tag method
by allowing itself to be used as a wrapper, hook, or called directly,
while also not requiring the content tag name (defaults to :div).
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/blocks/builders/builder.rb', line 147 def content_tag(*args, &block) = args. escape = .key?(:escape) ? .delete(:escape) : true if wrapper_type = .delete(:wrapper_type) html_option = ["#{wrapper_type}_html_option".to_sym] || [:html_option] wrapper_html = if html_option.is_a?(Array) html_option.map { |html_attribute| [html_attribute] }.compact.first elsif html_option.present? [html_option] end wrapper_html = concatenating_merge(["#{wrapper_type}_html".to_sym] || [:html], wrapper_html, *args, ) wrapper_tag = ["#{wrapper_type}_tag".to_sym] end wrapper_html ||= call_each_hash_value_with_params([:html], ).presence || wrapper_tag ||= .delete(:tag) if !wrapper_tag first_arg = args.first wrapper_tag = if first_arg.is_a?(String) || first_arg.is_a?(Symbol) args.shift else :div end end content_tag_args = [wrapper_tag] if !block_given? content_tag_args << (wrapper_html.delete(:content) || [:content] || args.shift) end content_tag_args << wrapper_html content_tag_args << escape view.content_tag *content_tag_args, &block end |
#deferred_render(*args, &block) ⇒ Object
40 41 42 |
# File 'lib/blocks/builders/builder.rb', line 40 def deferred_render(*args, &block) renderer_class.deferred_render(self, *args, &block) end |
#define(*args, &block) ⇒ Object
Define a block, unless a block by the same name is already defined.
<%= blocks.define :some_block_name, :parameter1 => "1", :parameter2 => "2" do |options| %>
<%= options[:parameter1] %> and <%= options[:parameter2] %>
<% end %>
Options:
name
-
The name of the block being defined (either a string or a symbol)
options
-
The default options for the block definition. Any or all of these options may be overridden by whomever calls “blocks.render” on this block.
block
-
The block that is to be rendered when “blocks.render” is called for this block.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/blocks/builders/builder.rb', line 78 def define(*args, &block) = args. name = if args.first args.shift else anonymous = true self.anonymous_block_number += 1 "anonymous_block_#{anonymous_block_number}" end block_definitions[name].tap do |block_definition| block_definition.reverse_merge! , &block block_definition.anonymous = !!anonymous end end |
#hooks_for(block_name, hook_name) ⇒ Object
48 49 50 |
# File 'lib/blocks/builders/builder.rb', line 48 def hooks_for(block_name, hook_name) block_for(block_name).try(:hooks_for, hook_name) || [] end |
#render(*args, &block) ⇒ Object
31 32 33 |
# File 'lib/blocks/builders/builder.rb', line 31 def render(*args, &block) renderer_class.render(self, *args, &block) end |
#render_with_overrides(*args, &block) ⇒ Object
35 36 37 38 |
# File 'lib/blocks/builders/builder.rb', line 35 def render_with_overrides(*args, &block) warn "[DEPRECATION] `render_with_overrides` is deprecated. Please use `render` instead." render(*args, &block) end |
#replace(name, options = {}, &block) ⇒ Object
Define a block, replacing an existing block by the same name if it is already defined.
<%= blocks.define :some_block_name, :parameter1 => "1", :parameter2 => "2" do |options| %>
<%= options[:parameter1] %> and <%= options[:parameter2] %>
<% end %>
<%= blocks.replace :some_block_name, :parameter3 => "3", :parameter4 => "4" do |options| %>
<%= options[:parameter3] %> and <%= options[:parameter4] %>
<% end %>
Options:
name
-
The name of the block being defined (either a string or a symbol)
options
-
The default options for the block definition. Any or all of these options may be overridden by whomever calls “blocks.render” on this block.
block
-
The block that is to be rendered when “blocks.render” is called for this block.
111 112 113 114 |
# File 'lib/blocks/builders/builder.rb', line 111 def replace(name, ={}, &block) block_definitions.delete(name) define(name, , &block) end |
#skip(name, completely = false) ⇒ Object
116 117 118 |
# File 'lib/blocks/builders/builder.rb', line 116 def skip(name, completely=false) block_definitions[name].skip(completely) end |
#skip_completely(name) ⇒ Object
120 121 122 |
# File 'lib/blocks/builders/builder.rb', line 120 def skip_completely(name) skip(name, true) end |