Class: WrapIt::Base

Inherits:
Object
  • Object
show all
Includes:
Callbacks, DerivedAttributes, Enums, HTMLClass, HTMLData, Renderer, Sections, Switches
Defined in:
lib/wrap_it/base.rb

Overview

Base class for all HTML helper classes

Examples:

Prevent user from changing element tag

class Helper < WrapIt::Base
  after_initialize { @tag = 'table' }
end

Including some simple HTML into content

class Helper < WrapIt::Base
  after_initialize do
    @icon = optioins.delete(:icon)
  end
  after_capture do
    unless @icon.nil?
    @content = html_safe("<i class=\"#{@icon}\"></i>") + @content
  end
end

Author:

Direct Known Subclasses

Container, Link

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Renderer

included

Methods included from Enums

included

Methods included from DerivedAttributes

included

Methods included from Switches

included

Methods included from HTMLData

included, #remove_html_data, #set_html_data

Methods included from HTMLClass

#add_html_class, #html_class, #html_class=, #html_class?, #html_class_prefix, included, #no_html_class?, #remove_html_class, sanitize

Methods included from Sections

#[], #[]=, included

Methods included from Callbacks

included, #run_callbacks

Constructor Details

#initialize(template, *args, &block) ⇒ Base

Returns a new instance of Base.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/wrap_it/base.rb', line 44

def initialize(template, *args, &block)
  @template, @arguments, @block = template, args, block
  self.options = @arguments.extract_options!

  @helper_name = @options.delete(:helper_name)
  @helper_name.is_a?(String) && @helper_name = @helper_name.to_sym

  @arguments.extend ArgumentsArray
  add_default_classes

  run_callbacks :initialize do
    @tag = @options.delete(:tag) ||
      self.class.get_derived(:@default_tag) || 'div'
    @tag = @tag.to_s
  end
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



37
38
39
# File 'lib/wrap_it/base.rb', line 37

def options
  @options
end

#tagObject (readonly)

Returns the value of attribute tag.



36
37
38
# File 'lib/wrap_it/base.rb', line 36

def tag
  @tag
end

Instance Method Details

#omit_content?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/wrap_it/base.rb', line 61

def omit_content?
  self.class.get_derived(:@omit_content)
end

#render(*args) {|element| ... } ⇒ String

Renders element to template

Parameters:

  • content (String)

    additional content that will be appended to element content

Yields:

  • (element)

    Runs block after capturing element content and before rendering it. Returned value appended to content.

Yield Parameters:

  • element (Base)

    rendering element.

Yield Returns:

  • (String, nil)

    content to append to HTML

Returns:

  • (String)

    rendered HTML for element



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/wrap_it/base.rb', line 77

def render(*args, &render_block)
  # return cached copy if it available
  return @rendered unless @rendered.nil?

  capture_sections

  # add to content string args and block result if its present
  args.flatten.each { |a| self[:render_arguments] << a if a.is_a? String }
  if block_given?
    result = instance_exec(self, &render_block) || empty_html
    result.is_a?(String) && self[:render_block] << result
  end

  do_render
  do_wrap

  if @template.output_buffer.nil?
    # when render called from code, just return content as a String
    @rendered
  else
    # in template context, write content to templates buffer
    concat(@rendered)
    empty_html
  end
end

#unwrapObject



135
136
137
# File 'lib/wrap_it/base.rb', line 135

def unwrap
  @wrapper = nil
end

#wrap(*args, &block) ⇒ void

This method returns an undefined value.

Wraps element with another.

You can provide wrapper directly or specify wrapper class as first argument. In this case wrapper will created with specified set of arguments and options. If wrapper class ommited, WrapIt::Base will be used.

If block present, it will be called when wrapper will rendered.

Parameters:

  • wrapper (Base)

    wrapper instance.

  • wrapper_class (Class)

    WrapIt::Base subclass for wrapper.

  • arg (String, Symbol)

    wrapper creation arguments.

  • options (Hash)

    wrapper creation options.

  • arg (String, Symbol)

    wrapper creation arguments.

  • options (Hash)

    wrapper creation options.



126
127
128
129
130
131
132
133
# File 'lib/wrap_it/base.rb', line 126

def wrap(*args, &block)
  if args.first.is_a?(Base)
    @wrapper = args.shift
  else
    wrapper_class = args.first.is_a?(Class) ? args.shift : Base
    @wrapper = wrapper_class.new(@template, *args, &block)
  end
end