Class: Markababy::Builder

Inherits:
BasicObject
Defined in:
lib/markababy/builder.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, &block) ⇒ Builder

Returns a new instance of Builder.



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/markababy/builder.rb', line 3

def initialize(options, &block)
  @options = options

  @output = @options[:output]

  @escape = @options[:escape]

  @context = @options[:context]

  instance_eval(&block)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/markababy/builder.rb', line 19

def method_missing(sym, *args, &block)
  if @context && context_responds_to?(sym)
    return @context.send(sym, *args, &block)
  end

  attributes, content = [], []

  args.flatten.each do |arg|
    if arg.respond_to?(:to_hash)
      arg.to_hash.each { |k, v| attributes << ' %s="%s"' % [@escape[k.to_s], @escape[v.to_s]] }
    elsif arg.respond_to?(:id2name)
      attributes << ' %s' % @escape[arg.to_s]
    elsif arg.respond_to?(:html_safe?) && arg.html_safe?
      content << arg.to_s
    else
      content << @escape[arg.to_s]
    end
  end

  @output << (attributes.empty? ? "<#{sym}>" : "<#{sym}#{attributes.join}>")

  @output << content.join unless content.empty?

  instance_eval(&block) unless block.nil?

  @output << "</#{sym}>" unless content.empty? && block.nil?
end

Class Method Details

.const_missing(name) ⇒ Object



55
56
57
# File 'lib/markababy/builder.rb', line 55

def self.const_missing(name)
  ::Object.const_get(name)
end

Instance Method Details

#context_responds_to?(name) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/markababy/builder.rb', line 15

def context_responds_to?(name)
  @context.respond_to?(name)
end

#text(content) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/markababy/builder.rb', line 47

def text(content)
  if content.respond_to?(:html_safe?) && content.html_safe?
    @output << content.to_s
  else
    @output << @escape[content.to_s]
  end
end