class HtmlTagBuilder

Attributes

data[R]

Public Class Methods

__add_opts(opts, key, value) click to toggle source
# File lib/html-tag/html_tag_builder.rb, line 96
def __add_opts opts, key, value
  unless value.to_s == ''
    value = value.join(' ') if value.is_a?(Array)
    key   = key.to_s.gsub(/data_/,'data-')
    opts.push key+'="'+value.to_s.gsub(/"/,'"')+'"'
  end
end
build(node, attrs={}) click to toggle source

build html node

# File lib/html-tag/html_tag_builder.rb, line 66
def build node, attrs={}, text=nil
  node = node.to_s

  opts = []
  attrs.each do |attr_key, attr_value|
    if attr_value.is_a?(Hash)
      for data_key, data_value in attr_value
        __add_opts opts, "#{attr_key}-#{data_key}", data_value
      end
    else
      __add_opts opts, attr_key, attr_value
    end
  end

  opts = opts.first ? ' '+opts.join(' ') : ''

  if node
    text ||= '' unless EMPTY_TAGS.include?(node)
    out = text ? %{<#{node}#{opts}>#{text}</#{node}>} : %{<#{node}#{opts} />}
    out.respond_to?(:html_safe) ? out.html_safe : out
  else
    opts
  end
end
call(class_name, &block) click to toggle source

tag.div(class: 'klasa') do -> tag.('klasa') do

# File lib/html-tag/html_tag_builder.rb, line 92
def call class_name, &block
  tag(:div, class_name, &block)
end
method_missing(name, *args, &block) click to toggle source

tag.div -> tag.tag :div

# File lib/html-tag/html_tag_builder.rb, line 8
def method_missing name, *args, &block
  tag name, args[0], args[1], &block
end
new() click to toggle source
# File lib/html-tag/html_tag_builder.rb, line 109
def initialize
  @data = []
end
tag(name, *args) { |stack, opts| ... } click to toggle source

tag :div, { 'class'=>'iform' } do

# File lib/html-tag/html_tag_builder.rb, line 13
def tag name, *args
  data, opts =
  if args[1]
    args[0].class == Hash ? args.reverse : args
  elsif args[0]
    if args[0].class == Symbol
      # tag.div(:a) { 1 } -> <div class="a">1</div>
      [nil, { class: args[0].to_s.gsub('__', ' ').gsub('_', '-') }]
    elsif args[0].class == Hash
      [nil, args[0]]
    else
      [args[0], {}]
    end
  else
    [nil, {}]
  end

  opts ||= {}

  unless opts.class == Hash
    raise ArgumentError.new('HtmlTag: bad agrument, attriobutes are no a hash')
  end

  if data.class == Hash
    raise ArgumentError.new('HtmlTag: bad agrument, data sent as hash')
  end

  # covert n._row_foo to n(class: 'row-foo')
  name = name.to_s
  if name.to_s[0, 1] == '_'
    opts ||= {}
    opts[:class] = name.to_s.sub('_', '').gsub('_', '-')
    name = :div
  end

  if block_given?
    if data
      raise ArgumentError.new('HtmlTag: data is allreay defined and block is given')
    end

    stack = new
    data  = yield(stack, opts)

    # if data is pushed to passed node, use that data
    data = stack.data if stack.data.first
  end

  data = data.join('') if data.is_a?(Array)

  build name, opts, data
end

Public Instance Methods

push(data=nil) { |: data| ... } click to toggle source

push data to stack

# File lib/html-tag/html_tag_builder.rb, line 114
def push data=nil
  @data.push block_given? ? yield : data
end

Private Instance Methods

method_missing(tag_name, *args, &block) click to toggle source

forward to class

# File lib/html-tag/html_tag_builder.rb, line 121
def method_missing tag_name, *args, &block
  @data.push self.class.tag(tag_name, args[0], args[1], &block)
end