Class: HtmlTag::Outbound

Inherits:
Object
  • Object
show all
Defined in:
lib/html-tag/outbound.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOutbound

Returns a new instance of Outbound.



111
112
113
# File 'lib/html-tag/outbound.rb', line 111

def initialize
  @data = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(tag_name, *args, &block) ⇒ Object (private)

forward to class



123
124
125
# File 'lib/html-tag/outbound.rb', line 123

def method_missing tag_name, *args, &block
  @data.push self.class.tag(tag_name, args[0], args[1], &block)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



109
110
111
# File 'lib/html-tag/outbound.rb', line 109

def data
  @data
end

Class Method Details

.__add_opts(opts, key, value) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/html-tag/outbound.rb', line 98

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 = {}, text = nil) ⇒ Object

build html node



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/html-tag/outbound.rb', line 69

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)
    text ? %{<#{node}#{opts}>#{text}</#{node}>} : %{<#{node}#{opts} />}
  else
    opts
  end
end

.call(class_name, &block) ⇒ Object

tag.div(class: ‘klasa’) do -> tag.(‘klasa’) do



94
95
96
# File 'lib/html-tag/outbound.rb', line 94

def call class_name, &block
  tag(:div, class_name, &block)
end

.method_missing(name, *args, &block) ⇒ Object

tag.div -> tag.tag :div



5
6
7
# File 'lib/html-tag/outbound.rb', line 5

def method_missing name, *args, &block
  tag name, args[0], args[1], &block
end

.tag(name, *args) ⇒ Object

tag :div, { ‘class’=>‘iform’ } do



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/html-tag/outbound.rb', line 10

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, attributes 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] == '_'
    classes = name
      .sub('_', '')
      .split('__')
      .map{|it| it.gsub('_', '-') }
      .join(' ')

    opts ||= {}
    opts[:class] = "#{classes} #{opts[:class]}".sub(/\s+$/, '')
    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

Instance Method Details

#push(data = nil) ⇒ Object

push data to stack



116
117
118
# File 'lib/html-tag/outbound.rb', line 116

def push data=nil
  @data.push block_given? ? yield : data
end