Class: WrapIt::Container

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

Overview

Describes elements that can contain other elements

TODO: single_child

Author:

Instance Attribute Summary collapse

Attributes inherited from Base

#options, #tag

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize, #omit_content?, #render, #unwrap, #wrap

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

This class inherits a constructor from WrapIt::Base

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



18
19
20
# File 'lib/wrap_it/container.rb', line 18

def children
  @children
end

#extract_children=(value) ⇒ Object (writeonly)

Sets the attribute extract_children



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

def extract_children=(value)
  @extract_children = value
end

Class Method Details

.child(name, *args, &block) ⇒ String

Defines child elements helper for creation of child items.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/wrap_it/container.rb', line 41

def self.child(name, *args, &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)
  @helpers ||= []
  @helpers << name
  define_method name do |*helper_args, &helper_block|
    # We should clone arguments becouse if we have loop in template,
    # `extract_options!` below works only for first iterration
    default_args = args.clone
    options = helper_args.extract_options!
    options[:helper_name] = name
    options.merge!(default_args.extract_options!)
    helper_args += default_args + [options]
    add_children(name, child_class, block, *helper_args, &helper_block)
  end
end

.extract_from_options(*args) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/wrap_it/container.rb', line 65

def self.extract_from_options(*args)
  return @extract_from_options || [] if args.size == 0
  hash = args.extract_options!
  args.size.odd? && fail(ArgumentError, 'odd arguments number')
  args.each_with_index { |arg, i| i.even? && hash[arg] = args[i + 1] }
  @helpers ||= []
  hash.symbolize_keys!
  @extract_from_options = Hash[
    hash.select do |k, v|
      (v.is_a?(String) || v.is_a?(Symbol)) && @helpers.include?(k)
    end.map { |k, v| [k, v.to_sym] }
  ]
end

Instance Method Details

#extract_children?Boolean



22
23
24
# File 'lib/wrap_it/container.rb', line 22

def extract_children?
  @extract_children == true
end