Class: WrapIt::Container

Inherits:
Base
  • Object
show all
Defined in:
lib/wrap_it/container.rb

Overview

TODO:

single_child realization

TODO:

refactor code for more clearness

Describes elements that can contain other elements

Author:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

This class inherits a constructor from WrapIt::Base

Instance Attribute Details

#childrenObject (readonly)

list of children elements



20
21
22
# File 'lib/wrap_it/container.rb', line 20

def children
  @children
end

#extract_children=(value) ⇒ Object (writeonly)

children can be extracted from normal template flow and rendered in separate section.



24
25
26
# File 'lib/wrap_it/container.rb', line 24

def extract_children=(value)
  @extract_children = value
end

Class Method Details

.child(name, class_name = nil, [args, ...], opts = {}, &block) ⇒ String

Defines helper for child elements creation.

Examples:

simple usage

class Item < WrapIt::Base
  include TextContainer
end

class List < WrapIt::Container
  default_tag 'ul'
  child :item, tag: 'li'
end

list = List.new(template)
list.item 'list item 1'
list.item 'list item 2'
list.render # => '<ul><li>list item 1'</li><li>list item 2</li></ul>'

with option

class Button < WrapIt::Container
  include TextContainer
  html_class 'btn'
  child :icon, tag: 'i', option: true
end

btn = Button.new(template, 'Home', icon: { class: 'i-home' })
btn.render # => '<div class="btn">Home<i class="i-home"></i></div>'

Parameters:

  • name (Symbol, String)

    helper method name

  • class_name (String, Base) (defaults to: nil)

    class for child elements. If ommited WrapIt::Base will be used

  • args (Object)

    any arguments that will be passed to child element constructor

  • opts (Hash) (defaults to: {})

    options

Options Hash (opts):

  • :option (true, Symbol)

    if specified, child can be created via option with same name (if :option is true) or with specified name

  • :section (Symbol)

    section to that this children will be rendered. By default children rendered to children. Refer to Sections module for details.

Returns:

  • (String)


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

def self.child(name, *args, option: nil, **opts, &block)
  name.is_a?(String) && name.to_sym
  name.is_a?(Symbol) || fail(ArgumentError, 'Wrong child name')
  child_class =
    if args.first.is_a?(String) || args.first.is_a?(Class)
      args.shift
    else
      'WrapIt::Base'
    end
  child_class = child_class.name if child_class.is_a?(Class)

  define_method name do |*hargs, extracted: false, **hopts, &hblock|
    hargs += args
    # TODO: merge html class correctly. Now it just overrided by opts
    hopts.merge!(opts)
    hopts[:helper_name] = name
    child = prepare_child(child_class, block, *hargs, **hopts, &hblock)
    add_children(name, child, extracted: extracted)
  end

  add_child_option(name, option)
end

Instance Method Details

#extract_children?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/wrap_it/container.rb', line 28

def extract_children?
  @extract_children == true
end