Class: HtmlTag::Inbound

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Inbound

Returns a new instance of Inbound.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/html-tag/inbound.rb', line 30

def initialize context
  # copy all instance varialbes from context
  for el in context.instance_variables
    unless el.to_s.include?('@_')
      val = context.instance_variable_get el
      instance_variable_set el, val
    end
  end

  # lets keep all instance vars in one object
  @_iv = IVARS.new
  @_iv.context = context
  @_iv.data    = []
  @_iv.depth   = 0
  @_iv.inbound = true
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/html-tag/inbound.rb', line 122

def method_missing name, *args, &block
  klass = name.to_s

  if klass.start_with?('_')
    # _foo__bar-baz class: 'dux' ->  <div class="foo bar-baz dux"></div>
    classes = klass
      .sub('_', '')
      .split('__')
      .map{|it| it.gsub('_', '-') }
      .join(' ')

    prepared = _prepare_tag_params args

    prepared[0] ||= {}
    prepared[0][:class] = "#{classes} #{prepared[0][:class]}".sub(/\s+$/, '')

    tag :div, *prepared, &block
  else
    message = [
      %{HTML tag "#{name}" not found.},
      "Use this.#{name}() to call method in parent context",
      "or use tag(:#{name}, params, data) to add custom html node."
    ]
    raise NoMethodError.new(message.join(' '))
  end
end

Class Method Details

.define(tag, empty: false) ⇒ Object

allows to add cusom tags if needed HtmlTag::Inbound.define :foo



15
16
17
18
19
20
21
22
23
# File 'lib/html-tag/inbound.rb', line 15

def self.define tag, empty: false
  if empty
    EMPTY_TAGS.add tag
  end

  define_method tag do |*args, &block|
    tag tag, *args, &block
  end
end

Instance Method Details

#parent(&block) ⇒ Object Also known as: context, this

access parent context via parent / context / this h1 class: this.class_name



49
50
51
52
53
54
55
# File 'lib/html-tag/inbound.rb', line 49

def parent &block
  if block
    @_iv.context.instance_exec(&block)
  else
    @_iv.context
  end
end

#push(data) ⇒ Object



118
119
120
# File 'lib/html-tag/inbound.rb', line 118

def push data
  @_iv.data << data
end

#renderObject

export renderd data



60
61
62
63
64
65
# File 'lib/html-tag/inbound.rb', line 60

def render
  @_iv.data
    .join('')
    .gsub(/\n+/, $/)
    .gsub(/([\w>])[[:blank:]]+</, '\1<')
end

#tag(name, *args, &block) ⇒ Object

render single node



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/html-tag/inbound.rb', line 68

def tag name, *args, &block
  opt_hash, opt_data = _prepare_tag_params args

  tag_data = "%s%s<%s" % [_depth_new_line, _depth_spaces, name]

  # if tag params given, add them
  if opt_hash
    tag_data += ' '
    tag_data += opt_hash.inject([]) do |t, el|
      key, value = el

      case value
      when Array
        value = value.join(' ')
      when Hash
        for el in value
          t.push '%s-%s="%s"' % [key, el[0], _escape_param(el[1])]
        end
      else
        t.push '%s="%s"' % [key, _escape_param(value)]
      end
      t
    end.join(' ')
  end

  unless EMPTY_TAGS.include?(name)
    tag_data += '>'
  end

  @_iv.data << tag_data

  # nested blocks
  if block
    @_iv.depth += 1
    instance_exec(@_iv.context, &block)
    # block.call(self) # for outbound render
    @_iv.depth -= 1
  end

  if EMPTY_TAGS.include?(name)
    @_iv.data << ' />'
  else
    unless opt_data
      @_iv.data << _depth_spaces
    end

    @_iv.data << '%s</%s>%s' % [opt_data, name, _depth_new_line]
  end
end