Class: Markaby::Builder

Inherits:
Object
  • Object
show all
Includes:
BuilderTags
Defined in:
lib/markaby/builder.rb

Overview

The Markaby::Builder class is the central gear in the system. When using from Ruby code, this is the only class you need to instantiate directly.

mab = Markaby::Builder.new
mab.html do
  head { title "Boats.com" }
  body do
    h1 "Boats.com has great deals"
    ul do
      li "$49 for a canoe"
      li "$39 for a raft"
      li "$29 for a huge boot that floats and can fit 5 people"
    end
  end
end
puts mab.to_s

Constant Summary collapse

GENERIC_OPTIONS =
{
  indent: 0,
  auto_validation: true
}
HTML5_OPTIONS =
HTML5.default_options.dup
DEFAULT_OPTIONS =
GENERIC_OPTIONS.merge(HTML5_OPTIONS)
@@options =
DEFAULT_OPTIONS.dup

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BuilderTags

#enable_html5!, #head, #html5, #html_tag, #xhtml_frameset, #xhtml_strict, #xhtml_transitional

Constructor Details

#initialize(assigns = {}, helper = nil, &block) ⇒ Builder

Create a Markaby builder object. Pass in a hash of variable assignments to assigns which will be available as instance variables inside tag construction blocks. If an object is passed in to helper, its methods will be available from those same blocks.

Pass in a block to new and the block will be evaluated.

mab = Markaby::Builder.new {
  html do
    body do
      h1 "Matching Mole"
    end
  end
}


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/markaby/builder.rb', line 73

def initialize(assigns = {}, helper = nil, &block)
  @streams = [Stream.new]
  @assigns = assigns.dup
  @_helper = helper
  @used_ids = {}

  @@options.each do |k, v|
    instance_variable_set("@#{k}", @assigns.delete(k) || v)
  end

  @assigns.each do |k, v|
    instance_variable_set("@#{k}", v)
  end

  helper&.instance_variables&.each do |iv|
    instance_variable_set(iv, helper.instance_variable_get(iv))
  end

  @builder = XmlMarkup.new(indent: @indent, target: @streams.last)

  text(capture(&block)) if block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

This method is used to intercept calls to helper methods and instance variables. Here is the order of interception:

  • If sym is a helper method, the helper method is called and output to the stream.

  • If sym is a Builder::XmlMarkup method, it is passed on to the builder object.

  • If sym is also the name of an instance variable, the value of the instance variable is returned.

  • If sym has come this far and no tagset is found, sym and its arguments are passed to tag!

  • If a tagset is found, the tagset is tole to handle sym

method_missing used to be the lynchpin in Markaby, but it’s no longer used to handle HTML tags. See html_tag for that.



171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/markaby/builder.rb', line 171

def method_missing(sym, *args, &block)
  case response_for(sym)
  when :helper then @_helper.send(sym, *args, &block)
  when :assigns then @assigns[sym]
  when :stringy_assigns then @assigns[sym.to_s]
  when :ivar then instance_variable_get(ivar)
  when :helper_ivar then @_helper.instance_variable_get(ivar)
  when :xml_markup then @builder.__send__(sym, *args, &block)
  when :tag then tag!(sym, *args, &block)
  when :tagset then @tagset.handle_tag sym, self, *args, &block
  else super
  end
end

Instance Attribute Details

#tagsetObject

Returns the value of attribute tagset.



48
49
50
# File 'lib/markaby/builder.rb', line 48

def tagset
  @tagset
end

Class Method Details

.get(option) ⇒ Object



44
45
46
# File 'lib/markaby/builder.rb', line 44

def self.get(option)
  @@options[option]
end

.restore_defaults!Object



36
37
38
# File 'lib/markaby/builder.rb', line 36

def self.restore_defaults!
  @@options = DEFAULT_OPTIONS.dup
end

.set(option, value) ⇒ Object



40
41
42
# File 'lib/markaby/builder.rb', line 40

def self.set(option, value)
  @@options[option] = value
end

Instance Method Details

#capture(&block) ⇒ Object

Captures the HTML code built inside the block. This is done by creating a new stream for the builder object, running the block and passing back its stream as a string.

>> Markaby::Builder.new.capture { h1 "TEST"; h2 "CAPTURE ME" }
=> "<h1>TEST</h1><h2>CAPTURE ME</h2>"


125
126
127
128
129
130
131
132
133
134
# File 'lib/markaby/builder.rb', line 125

def capture(&block)
  @streams.push(@builder.target = Stream.new)
  @builder.level += 1
  str = instance_eval(&block)
  str = @streams.last.join if @streams.last.any?
  @streams.pop
  @builder.level -= 1
  @builder.target = @streams.last
  str
end

#helper=(helper) ⇒ Object



96
97
98
# File 'lib/markaby/builder.rb', line 96

def helper=(helper)
  @_helper = helper
end

#locals=(locals) ⇒ Object



100
101
102
103
104
# File 'lib/markaby/builder.rb', line 100

def locals=(locals)
  locals.each do |key, value|
    define_singleton_method(key) { value }
  end
end

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

Create a tag named tag. Other than the first argument which is the tag name, the arguments are the same as the tags implemented via method_missing.

Raises:



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/markaby/builder.rb', line 138

def tag!(tag, *args, &block)
  attributes = {}
  if @auto_validation && @tagset
    attributes = @tagset.validate_and_transform_attributes!(tag, *args)
    tag = @tagset.validate_and_transform_tag_name! tag
  end
  element_id = attributes[:id].to_s
  raise InvalidXhtmlError, "id `#{element_id}' already used (id's must be unique)." if @used_ids.has_key?(element_id)
  if block
    str = capture(&block)
    block = proc { text(str) }
  end

  f = fragment { @builder.tag!(tag, *args, &block) }
  @used_ids[element_id] = f unless element_id.empty?
  f
end

#text(string) ⇒ Object Also known as: <<, concat

Write a string to the HTML stream without escaping it.



112
113
114
115
# File 'lib/markaby/builder.rb', line 112

def text(string)
  @builder << string.to_s
  nil
end

#to_sObject

Returns a string containing the HTML stream. Internally, the stream is stored as an Array.



107
108
109
# File 'lib/markaby/builder.rb', line 107

def to_s
  @streams.last.to_s
end