Class: Htmless::Abstract

Inherits:
Object
  • Object
show all
Extended by:
DynamicClasses
Defined in:
lib/htmless/abstract.rb

Overview

Abstract implementation of Builder

Direct Known Subclasses

Standard

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from DynamicClasses

create_dynamic_classes, dynamic_classes, extended, inherited

Constructor Details

- (Abstract) initialize

creates a new builder This is quite expensive, HammerBuilder::Pool should be used



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/htmless/abstract.rb', line 87

def initialize()
  @_output  = ""
  @_stack   = []
  @_current = nil

  self.class.strings_injector.inject_to self

  # tag classes initialization
  tags.each do |klass|
    instance_variable_set(:@_#{klass}", self.class.dynamic_classes[camelize_string(klass).to_sym].new(self))
  end
end

Instance Attribute Details

- (Object) _current Also known as: current

current tag being builded



80
81
82
# File 'lib/htmless/abstract.rb', line 80

def _current
  @_current
end

Class Method Details

+ (Object) camelize_string(term) (private)



239
240
241
242
243
244
245
# File 'lib/htmless/abstract.rb', line 239

def self.camelize_string(term)
  term.
      to_s.
      sub(/^[a-z\d]*/) { $&.capitalize }.
      gsub(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{$2.capitalize}" }.
      gsub('/', '::')
end

+ (Object) define_tag(tag) (protected)

defines instance method for +tag+ in builder



65
66
67
68
69
70
71
72
73
74
# File 'lib/htmless/abstract.rb', line 65

def self.define_tag(tag)
  tag = tag.to_s
  class_eval <<-RUBY, __FILE__, __LINE__ + 1
    def #{tag}(*args, &block)
      flush
      @_#{tag}.open(*args, &block)
    end
  RUBY
  self.tags += [tag]
end

+ (Object) strings_injector



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/htmless/abstract.rb', line 21

def self.strings_injector
  @strings_injector ||= StringsInjector.new do
    add :lt, '<'
    add :gt, '>'
    add :slash_lt, '</'
    add :slash_gt, ' />'
    add :dash, '-'
    add :underscore, '_'
    add :space, ' '
    add :spaces, Array.new(300) { |i| ('  ' * i).freeze }
    add :newline, "\n"
    add :quote, '"'
    add :eql, '='
    add :eql_quote, self[:eql] + self[:quote]
    add :comment_start, '<!--'
    add :comment_end, '-->'
    add :cdata_start, '<![CDATA['
    add :cdata_end, ']]>'
  end
end

+ (Object) tags



52
53
54
# File 'lib/htmless/abstract.rb', line 52

def self.tags
  @tags or (superclass.tags if superclass.respond_to? :tags)
end

+ (Object) tags=(tags)

<< faster then + yield faster then block.call accessing ivar and constant is faster then accesing hash or cvar class_eval faster then define_method beware of strings in methods -> creates a lot of garbage



48
49
50
# File 'lib/htmless/abstract.rb', line 48

def self.tags=(tags)
  @tags = tags
end

Instance Method Details

- (Object) camelize_string(term) (private)



247
248
249
# File 'lib/htmless/abstract.rb', line 247

def camelize_string(term)
  self.class.camelize_string term
end

- (Object) cdata(content)

insersts CDATA with +content+



119
120
121
122
# File 'lib/htmless/abstract.rb', line 119

def cdata(content)
  flush
  @_output << @_str_cdata_start << content.to_s << @_str_cdata_end
end

- (Object) comment(comment)

inserts +comment+



113
114
115
116
# File 'lib/htmless/abstract.rb', line 113

def comment(comment)
  flush
  @_output << @_str_comment_start << comment.to_s << @_str_comment_end
end

- (Object) flush

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

flushes open tag



183
184
185
186
187
188
# File 'lib/htmless/abstract.rb', line 183

def flush
  if @_current
    @_current.flush
    @_current = nil
  end
end

- (Object) go_in(*variables, &block) Also known as: dive

enables you to evaluate +block+ inside the builder with +variables+

Examples:

HammerBuilder::Formatted.new.go_in('asd') do |string|
  div string
end.to_html #=> "<div>asd</div>"


158
159
160
161
# File 'lib/htmless/abstract.rb', line 158

def go_in(*variables, &block)
  instance_exec *variables, &block
  self
end

- (Object) html5

renders html5 doc type

Examples:

html5 # => <!DOCTYPE html>


127
128
129
# File 'lib/htmless/abstract.rb', line 127

def html5
  raw "<!DOCTYPE html>\n"
end

- (Object) join(collection, glue = nil) { ... }

joins and renders +collection+ with +glue+

Examples:

join([1, 1.2], lambda { text ', ' }) {|o| text o }        # => "1, 1.2"
join([1, 1.2], ', ') {|o| text o }                        # => "1, 1.2"
join([->{ text 1 }, 1.2], ', ') {|o| text o }             # => "1, 1.2"

Parameters:

  • collection (Array<Proc, Object>)

    of objects or lambdas

  • glue (Proc, String) (defaults to: nil)

    can be String which is rendered with #text or block to render

Yields:

  • how to render objects from +collection+, Proc in collection does not use this block



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/htmless/abstract.rb', line 219

def join(collection, glue = nil, &it)
  # TODO as helper? two block method call #join(collection, &item).with(&glue)
  glue_block = case glue
                 when String
                   lambda { text glue }
                 when Proc
                   glue
                 else
                   lambda {}
               end

  collection.each_with_index do |obj, i|
    glue_block.call() if i > 0
    obj.is_a?(Proc) ? obj.call : it.call(obj)
  end
end

- (Object) js(js, options = {})

renders js

Examples:

js 'a_js_function();' #=> <script type="text/javascript">a_js_function();</script>

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :cdata (Boolean) — default: false

    should cdata be used?



206
207
208
209
# File 'lib/htmless/abstract.rb', line 206

def js(js, options = {})
  use_cdata = options.delete(:cdata) || false
  script({ :type => "text/javascript" }.merge(options)) { use_cdata ? cdata(js) : text(js) }
end

- (Object) raw(text)

unescaped +text+ to output



107
108
109
110
# File 'lib/htmless/abstract.rb', line 107

def raw(text)
  flush
  @_output << text.to_s
end

- (Object) render(object, method, *args) { ... } Also known as: r

renders +object+ with +method+

Parameters:

  • object (Object)

    an object to render

  • method (Symbol)

    a method name which is used for rendering

  • args

    arguments passed to rendering method

Yields:

  • block passed to rendering method



195
196
197
198
# File 'lib/htmless/abstract.rb', line 195

def render(object, method, *args, &block)
  object.__send__ method, self, *args, &block
  self
end

- (Object) reset

resets the builder to the state after creation - much faster then creating a new one



132
133
134
135
136
137
# File 'lib/htmless/abstract.rb', line 132

def reset
  flush
  @_output.clear
  @_stack.clear
  self
end

- (Object) set_variables(instance_variables) { ... }

sets instance variables when block is yielded

Parameters:

  • instance_variables (Hash{String => Object})

    hash of names and values to set

Yields:

  • block when variables are set, variables are cleaned up afterwards



168
169
170
171
172
173
# File 'lib/htmless/abstract.rb', line 168

def set_variables(instance_variables)
  instance_variables.each { |name, value| instance_variable_set("@#{name}", value) }
  yield(self)
  instance_variables.each { |name, _| remove_instance_variable("@#{name}") }
  self
end

- (Object) tags



56
57
58
# File 'lib/htmless/abstract.rb', line 56

def tags
  self.class.tags
end

- (Object) text(text)

escapes +text+ to output



101
102
103
104
# File 'lib/htmless/abstract.rb', line 101

def text(text)
  flush
  @_output << CGI.escapeHTML(text.to_s)
end

- (String) to_html

Output

Returns:

  • (String)

    output



176
177
178
179
# File 'lib/htmless/abstract.rb', line 176

def to_html()
  flush
  @_output.clone
end