Class: Blocks::Builder
- Inherits:
-
Object
- Object
- Blocks::Builder
- Includes:
- CallWithParams
- Defined in:
- lib/blocks/builders/builder.rb
Constant Summary collapse
- CONTENT_TAG_WRAPPER_BLOCK =
:content_tag_wrapper
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_set ⇒ 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
-
#concatenating_merge(options, options2, *args) ⇒ Object
TODO: move this logic elsewhere.
-
#define(*args, &block) ⇒ Object
Define a block, unless a block by the same name is already defined.
- #define_each(collection, block_name_proc, *args, &block) ⇒ Object
-
#initialize(view, options = {}) ⇒ Builder
constructor
A new instance of Builder.
- #renderer ⇒ 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 = {}) ⇒ Builder
Returns a new instance of Builder.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/blocks/builders/builder.rb', line 32 def initialize(view, ={}) if defined?(::Haml) && !view.instance_variables.include?(:@haml_buffer) class << view include Haml::Helpers end view.init_haml_helpers end 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. = OptionsSet.new("Builder Options", ) define_helper_blocks end |
Instance Attribute Details
#anonymous_block_number ⇒ Object
Returns the value of attribute anonymous_block_number.
16 17 18 |
# File 'lib/blocks/builders/builder.rb', line 16 def anonymous_block_number @anonymous_block_number end |
#block_definitions ⇒ Object
A HashWithIndifferentAccess of block names to BlockDefinition mappings
11 12 13 |
# File 'lib/blocks/builders/builder.rb', line 11 def block_definitions @block_definitions end |
#options_set ⇒ Object
Options provided during initialization of builder
14 15 16 |
# File 'lib/blocks/builders/builder.rb', line 14 def @options_set end |
#view ⇒ Object
A pointer to the view context
8 9 10 |
# File 'lib/blocks/builders/builder.rb', line 8 def view @view end |
Instance Method Details
#block_defined?(block_name) ⇒ Boolean
56 57 58 |
# File 'lib/blocks/builders/builder.rb', line 56 def block_defined?(block_name) block_definitions.key?(block_name) end |
#block_for(block_name) ⇒ Object
52 53 54 |
# File 'lib/blocks/builders/builder.rb', line 52 def block_for(block_name) block_definitions[block_name] if block_defined?(block_name) end |
#concatenating_merge(options, options2, *args) ⇒ Object
TODO: move this logic elsewhere
131 132 133 134 135 136 137 138 139 140 141 142 143 |
# 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) || {} .symbolize_keys.merge(.symbolize_keys) do |key, v1, v2| if v1.is_a?(String) && v2.is_a?(String) "#{v1} #{v2}" else v2 end end 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.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/blocks/builders/builder.rb', line 79 def define(*args, &block) = args. name, anonymous = if args.first [args.shift, false] else self.anonymous_block_number += 1 ["anonymous_block_#{anonymous_block_number}", true] end block_definitions[name].tap do |block_definition| block_definition. , &block block_definition.anonymous = anonymous end end |
#define_each(collection, block_name_proc, *args, &block) ⇒ Object
60 61 62 63 64 |
# File 'lib/blocks/builders/builder.rb', line 60 def define_each(collection, block_name_proc, *args, &block) collection.map do |object| define(call_with_params(block_name_proc, object, *args), object, *args, &block) end end |
#renderer ⇒ Object
48 49 50 |
# File 'lib/blocks/builders/builder.rb', line 48 def renderer @renderer ||= Blocks.renderer_class.new(self) 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 |