Class: Blocks::Builder

Inherits:
Object
  • Object
show all
Includes:
HamlCapture
Defined in:
lib/blocks/builders/builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options=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.options = options
end

Instance Attribute Details

#anonymous_block_numberObject

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_definitionsObject

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

#optionsObject

Options provided during initialization of builder



16
17
18
# File 'lib/blocks/builders/builder.rb', line 16

def options
  @options
end

#viewObject

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

Returns:

  • (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(options, options2, *args)
  options = call_each_hash_value_with_params(options, *args) || {}
  options2 = call_each_hash_value_with_params(options2, *args) || {}

  options.merge(options2) 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 (*args, &block)
  options = args.extract_options!
  escape = options.key?(:escape) ? options.delete(:escape) : true
  if wrapper_type = options.delete(:wrapper_type)

    html_option = options["#{wrapper_type}_html_option".to_sym] || options[:html_option]
    wrapper_html = if html_option.is_a?(Array)
      html_option.map { |html_attribute| options[html_attribute] }.compact.first
    elsif html_option.present?
      options[html_option]
    end

    wrapper_html = concatenating_merge(options["#{wrapper_type}_html".to_sym] || options[:html], wrapper_html, *args, options)

    wrapper_tag = options["#{wrapper_type}_tag".to_sym]
  end
  wrapper_html ||= call_each_hash_value_with_params(options[:html], options).presence || options
  wrapper_tag ||= options.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

   = [wrapper_tag]
  if !block_given?
     << (wrapper_html.delete(:content) || options[:content] || args.shift)
  end
   << wrapper_html
   << escape

  view. *, &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)
  options = args.extract_options!

  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! options, &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, options={}, &block)
  block_definitions.delete(name)
  define(name, options, &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