Class: HtmlTag::Inbound
- Inherits:
-
Object
- Object
- HtmlTag::Inbound
- Defined in:
- lib/html-tag/inbound.rb
Class Method Summary collapse
-
.define(name, empty: false) ⇒ Object
allows to add cusom tags if needed HtmlTag::Inbound.define :foo.
Instance Method Summary collapse
-
#initialize(context = nil) ⇒ Inbound
constructor
A new instance of Inbound.
- #method_missing(name, *args, &block) ⇒ Object
-
#parent(&block) ⇒ Object
(also: #context, #this)
access parent context via parent / context / this h1 class: this.class_name.
- #push(data = nil) ⇒ Object
-
#render ⇒ Object
export renderd data.
-
#tag(name, *args, &block) ⇒ Object
render single node.
Constructor Details
#initialize(context = nil) ⇒ Inbound
Returns a new instance of Inbound.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/html-tag/inbound.rb', line 32 def initialize context = nil # 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 end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/html-tag/inbound.rb', line 150 def method_missing name, *args, &block klass = name.to_s if klass.start_with?('_') tag klass, *args, &block elsif @_iv.context @_iv.context.send name, *args, &block else = [ %{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(.join(' ')) end end |
Class Method Details
.define(name, empty: false) ⇒ Object
allows to add cusom tags if needed HtmlTag::Inbound.define :foo
17 18 19 20 21 22 23 24 25 |
# File 'lib/html-tag/inbound.rb', line 17 def self.define name, empty: false if empty EMPTY_TAGS.add name end define_method name do |*args, &block| tag name, *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
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/html-tag/inbound.rb', line 50 def parent &block unless @_iv.context raise 'Host scope is not available' end if block @_iv.context.instance_exec(&block) else @_iv.context end end |
#push(data = nil) ⇒ Object
142 143 144 145 146 147 148 |
# File 'lib/html-tag/inbound.rb', line 142 def push data = nil if block_given? data = yield end @_iv.data << data end |
#render ⇒ Object
export renderd data
65 66 67 68 69 70 |
# File 'lib/html-tag/inbound.rb', line 65 def render @_iv.data .join('') .gsub(/\n+/, $/) #.gsub(/([\w>])[[:blank:]]+</, '\1<') end |
#tag(name, *args, &block) ⇒ Object
render single node
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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/html-tag/inbound.rb', line 73 def tag name, *args, &block name, opt_hash, opt_data = _prepare_tag_params name, 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 if value.class == Hash for el in value t.push '%s-%s=%s' % [key, el[0], _escape_param(el[1])] end else if value.class == Array value = value.join(' ') end key = key.to_s.sub(/^data_/, 'data-') 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 node_count = @_iv.data.length block_data = if @_iv.context # HtmlTag scope instance_exec(self, &block) else # outbound scope block.call(self) end if block_data.class == String && node_count == @_iv.data.length @_iv.data << block_data end @_iv.depth -= 1 end if EMPTY_TAGS.include?(name) @_iv.data << ' />' else unless opt_data @_iv.data << _depth_spaces end if opt_data.class == Array opt_data = opt_data.join('')\ end @_iv.data << '%s</%s>%s' % [opt_data, name, _depth_new_line] end end |