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.



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

def initialize(name, options, context, instance = nil)
  @name = name
  @options = options
  @context = context
  @instance = instance
  @done = false

  @content = nil
  @has_content = nil

  @attributes = {}

  @pos = @context.size
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &blk) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mab/mixin.rb', line 37

def method_missing(name, *args, &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(*args, &blk)
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



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

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

#has_contentObject

Returns the value of attribute has_content.



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

def has_content
  @has_content
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

Instance Method Details

#attrs_to_sObject



104
105
106
107
108
109
110
111
112
# File 'lib/mab/mixin.rb', line 104

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(*args, &blk) ⇒ Object

Raises:



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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/mab/mixin.rb', line 53

def insert(*args, &blk)
  raise Error, "This tag is already closed" if @done

  if !args.empty? && !args[0].is_a?(Hash)
    content = args.shift
    raise Error, "Tag doesn't allow content" if @has_content == false
    @has_content = true
  end

  if content
    @content = CGI.escapeHTML(content.to_s)
    @done = true
  end

  if !args.empty?
    merge_attributes(*args)
    @done = true
  end

  if block_given?
    raise Error, "Tag doesn't allow content" if @has_content == false
    @has_content = true
    @block = blk
    @done = true
  end

  if @content && @block
    raise Error, "Both content and block is not allowed"
  end

  @instance.mab_done(self) if @done

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

    if before >= @context.children
      @content = res.to_s
    else
      # Turn the node into just an opening tag.
      @has_content = false
      @instance.mab_insert("</#{@name}>")
    end
  end

  self
end

#merge_attributes(*args) ⇒ Object



31
32
33
34
35
# File 'lib/mab/mixin.rb', line 31

def merge_attributes(*args)
  args.each do |attrs|
    @attributes.merge!(attrs)
  end
end

#to_aryObject



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

def to_ary() nil end

#to_sObject



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/mab/mixin.rb', line 114

def to_s
  if !@context.joining? && @context[@pos]
    @context[@pos] = nil
    @context.children -= 1
  end

  res = "<#{@name}#{attrs_to_s}"
  res << (@options[:xml] && !@block && !@has_content ? ' />' : '>')
  res << "#{@content}</#{@name}>" if @has_content
  res
end

#to_strObject



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

def to_str() to_s end