Class: Erector::Promise

Inherits:
Object
  • Object
show all
Extended by:
Attributes, Text
Defined in:
lib/erector/promise.rb

Instance Method Summary collapse

Methods included from Attributes

format_attributes, format_sorted, sort_attributes

Methods included from Text

character, h, nbsp, raw, text, text!

Constructor Details

#initialize(output, tag_name, attributes = {}, self_closing = false, newliney = true, &inside_renderer) ⇒ Promise

Returns a new instance of Promise.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/erector/promise.rb', line 9

def initialize(output, tag_name, attributes = {}, self_closing = false, newliney = true, &inside_renderer)
  raise "bad output: #{output.inspect}" unless output.is_a? Output
  raise "forgot self-closing" unless [false, true].include? self_closing

  @output = output

  # todo: pointer to Tag object?
  @tag_name = tag_name
  @self_closing = self_closing
  @newliney = newliney

  @attributes = {}
  _set_attributes attributes
  @text = nil
  @inside_renderer = inside_renderer
  _mark
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



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/erector/promise.rb', line 85

def method_missing(method_name, *args, &block)
  method_name = method_name.to_s
  if method_name =~ /\!$/
    id_str = method_name[0...-1]
    raise ArgumentError, "setting id #{id_str} but id #{@attributes["id"]} already present" if @attributes["id"]
    @attributes["id"] = id_str
  else
    if @attributes["class"]
      @attributes["class"] += " "
    else
      @attributes["class"] = ""
    end
    @attributes["class"] += method_name.to_s
  end

  if block_given?
    @inside_renderer = block
  end

  if args.last.is_a? Hash
    attributes = args.pop
    _set_attributes attributes
  end

  # todo: allow multiple args
  # todo: allow promise args
  @text = args.first

  _render

  self
end

Instance Method Details

#_attributesObject



124
125
126
# File 'lib/erector/promise.rb', line 124

def _attributes
  @attributes
end

#_close_tagObject



132
133
134
# File 'lib/erector/promise.rb', line 132

def _close_tag
  @close_tag
end

#_markObject



33
34
35
# File 'lib/erector/promise.rb', line 33

def _mark
  @mark = @output.mark
end

#_open_tagObject



128
129
130
# File 'lib/erector/promise.rb', line 128

def _open_tag
  @open_tag
end

#_renderObject



41
42
43
44
45
46
47
48
49
# File 'lib/erector/promise.rb', line 41

def _render
  _rewind
  _render_open_tag
  begin
    _render_inside_tag
  ensure
    _render_close_tag
  end
end

#_render_close_tagObject



75
76
77
78
79
80
81
82
83
# File 'lib/erector/promise.rb', line 75

def _render_close_tag
  return if @self_closing

  @output.undent
  @output<< RawString.new("</#{@tag_name}>")
  if @newliney
    @output.newline
  end
end

#_render_inside_tagObject



65
66
67
68
69
70
71
72
73
# File 'lib/erector/promise.rb', line 65

def _render_inside_tag
  return if @self_closing
  if @text
    @output << @text
  end
  if @inside_renderer
    @inside_renderer.call
  end
end

#_render_open_tagObject



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/erector/promise.rb', line 51

def _render_open_tag

  @output.newline if !@self_closing and @newliney and !@output.at_line_start?

  @output << RawString.new( "<#{@tag_name}#{Promise.format_attributes(@attributes)}")
  if @self_closing
    @output << RawString.new( " />")
    @output.newline if @newliney
  else
    @output << RawString.new( ">")
    @output.indent
  end
end

#_rewindObject



37
38
39
# File 'lib/erector/promise.rb', line 37

def _rewind
  @output.rewind @mark
end

#_set_attributes(attributes) ⇒ Object



27
28
29
30
31
# File 'lib/erector/promise.rb', line 27

def _set_attributes attributes
  attributes.each_pair do |k,v|
    @attributes[k.to_s] = v
  end
end

#_tag_nameObject

are these accessors necessary?



120
121
122
# File 'lib/erector/promise.rb', line 120

def _tag_name
  @tag_name
end