Class: Mab::Mixin::Tag

Inherits:
Object
  • Object
show all
Defined in:
lib/mab/mixin.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options, context, instance = nil) ⇒ Tag

Returns a new instance of Tag.



11
12
13
14
15
16
17
18
19
# File 'lib/mab/mixin.rb', line 11

def initialize(name, options, context, instance = nil)
  @name = name
  @options = options
  @context = context
  @instance = instance
  @done = false
  @content = false
  @pos = @context.size
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, content = nil, attrs = nil, &blk) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mab/mixin.rb', line 33

def method_missing(name, content = nil, attrs = nil, &blk)
  name = name.to_s

  if name[-1] == ?!
    attributes[:id] = name[0..-2]
  else
    if attributes.has_key?(:class)
      attributes[:class] += " #{name}"
    else
      attributes[:class] = name
    end
  end

  insert(content, attrs, &blk)
end

Instance Attribute Details

#attributesObject



21
22
23
# File 'lib/mab/mixin.rb', line 21

def attributes
  @attributes ||= {}
end

#blockObject

Returns the value of attribute block.



7
8
9
# File 'lib/mab/mixin.rb', line 7

def block
  @block
end

#contentObject

Returns the value of attribute content.



7
8
9
# File 'lib/mab/mixin.rb', line 7

def content
  @content
end

#contextObject (readonly)

Returns the value of attribute context.



8
9
10
# File 'lib/mab/mixin.rb', line 8

def context
  @context
end

#instanceObject (readonly)

Returns the value of attribute instance.



8
9
10
# File 'lib/mab/mixin.rb', line 8

def instance
  @instance
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/mab/mixin.rb', line 7

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/mab/mixin.rb', line 8

def options
  @options
end

#pos=(value) ⇒ Object (writeonly)

Sets the attribute pos

Parameters:

  • value

    the value to set the attribute pos to.



9
10
11
# File 'lib/mab/mixin.rb', line 9

def pos=(value)
  @pos = value
end

Instance Method Details

#attrs_to_sObject



90
91
92
93
94
95
96
97
98
# File 'lib/mab/mixin.rb', line 90

def attrs_to_s
  attributes.inject("") do |res, (name, value)|
    if value
      value = (value == true) ? name : CGI.escapeHTML(value.to_s)
      res << " #{name}=\"#{value}\""
    end
    res
  end
end

#insert(content = nil, attrs = nil, &blk) ⇒ Object

Raises:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/mab/mixin.rb', line 49

def insert(content = nil, attrs = nil, &blk)
  raise Error, "This tag is already closed" if @done

  if content.is_a?(Hash)
    attrs = content
    content = nil
  end

  merge_attributes(attrs) if attrs

  if block_given?
    @block = blk
    @done = true
  elsif content
    content = content.to_s
    @content = CGI.escapeHTML(content)
    @done = !content.empty?
  elsif attrs
    @done = true
  end

  @instance.mab_done(self) if @done

  if @block
    before = @context.children
    res = @block.call

    if before >= @context.children
      @content = res.to_s
    else
      @content = nil
      @instance.mab_insert("</#{@name}>")
    end
  end

  self
end

#merge_attributes(attrs) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/mab/mixin.rb', line 25

def merge_attributes(attrs)
  if defined?(@attributes)
    @attributes.merge!(attrs)
  else
    @attributes = attrs
  end
end

#to_aryObject



87
# File 'lib/mab/mixin.rb', line 87

def to_ary() nil end

#to_sObject



100
101
102
103
104
105
106
107
108
109
# File 'lib/mab/mixin.rb', line 100

def to_s
  if !@context.joining? && @context[@pos]
    @context[@pos] = nil
    @context.children -= 1
  end
  res = "<#{@name}#{attrs_to_s}"
  res << (@options[:xml] && @content == false ? ' />' : '>')
  res << "#{@content}</#{@name}>" if @content
  res
end

#to_strObject



88
# File 'lib/mab/mixin.rb', line 88

def to_str() to_s end