Module: WrapIt::Sections::ClassMethods

Included in:
Base, WrapIt::Sections
Defined in:
lib/wrap_it/sections.rb

Overview

WrapIt::Sections class methods

Instance Method Summary collapse

Instance Method Details

#place(src, to) ⇒ void #place(src, at, dst) ⇒ void

This method returns an undefined value.

Places specific section in specified place

Overloads:

  • #place(src, to) ⇒ void

    Parameters:

    • src (Symbol)

      section name to place

    • to (Hash)

      single key-value hash. Key can be :before or after, value can be :begin, :end or section name

  • #place(src, at, dst) ⇒ void

    Parameters:

    • src (Symbol)

      section name to place

    • at (Symbol)

      can be :before or :after

    • dst (Symbol)

      can be :begin, :end or section name



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/wrap_it/sections.rb', line 150

def place(src, at, dst = nil)
  if dst == nil && at.is_a?(Hash) && at.keys.size == 1
    dst = at.values[0]
    at = at.keys[0]
  end
  return unless placement.include?(src) &&
    (dst == :begin || dst == :end || placement.include?(dst)) &&
    (at == :before || at == :after)
  item = placement.delete_at(placement.index(src))
  case dst
  when :begin then placement.unshift(item)
  when :end then placement.push(item)
  else
    x = at == :before ? 0 : 1
    placement.insert(placement.index(dst) + x, item)
  end
end

#placementArray<Symbol>

Retrieves section names in current order

Returns:

  • (Array<Symbol>)

    ordered sections array



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

def placement
  @placement ||=
    if self == Base
      sections.clone
    else
      parent = ancestors[1..-1].find { |a| a.respond_to?(:placement) }
      parent.nil? ? sections.clone : parent.placement.clone
    end
end

#section([name, ...]) ⇒ void

This method returns an undefined value.

Defines new section or sections. Places its to end of section list

Parameters:

  • name (Symbol, String)

    section name



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/wrap_it/sections.rb', line 110

def section(*args)
  @sections ||= []
  args.flatten.each do |name|
    name.is_a?(String) && name = name.to_sym
    next unless name.is_a?(Symbol)
    next if (sections + [:begin, :end]).include?(name)
    @sections << name
    placement << name unless placement.include?(name)
    place name, before: :end
  end
end

#sectionsArray<Symbol>

Retrieves all sections, including ancestors

Returns:

  • (Array<Symbol>)

    array of sections



99
100
101
# File 'lib/wrap_it/sections.rb', line 99

def sections
  collect_derived(:@sections)
end