Class: Mab::Mixin::Tag
- Inherits:
-
Object
- Object
- Mab::Mixin::Tag
- Defined in:
- lib/mab/mixin.rb
Instance Attribute Summary collapse
- #attributes ⇒ Object
-
#block ⇒ Object
Returns the value of attribute block.
-
#content ⇒ Object
Returns the value of attribute content.
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#instance ⇒ Object
readonly
Returns the value of attribute instance.
-
#name ⇒ Object
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#pos ⇒ Object
writeonly
Sets the attribute pos.
Instance Method Summary collapse
- #attrs_to_s ⇒ Object
-
#initialize(name, options, context, instance = nil) ⇒ Tag
constructor
A new instance of Tag.
- #insert(content = nil, attrs = nil, &blk) ⇒ Object
- #merge_attributes(attrs) ⇒ Object
- #method_missing(name, content = nil, attrs = nil, &blk) ⇒ Object
- #to_ary ⇒ Object
- #to_s ⇒ Object
- #to_str ⇒ Object
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, , context, instance = nil) @name = name @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
#attributes ⇒ Object
21 22 23 |
# File 'lib/mab/mixin.rb', line 21 def attributes @attributes ||= {} end |
#block ⇒ Object
Returns the value of attribute block.
7 8 9 |
# File 'lib/mab/mixin.rb', line 7 def block @block end |
#content ⇒ Object
Returns the value of attribute content.
7 8 9 |
# File 'lib/mab/mixin.rb', line 7 def content @content end |
#context ⇒ Object (readonly)
Returns the value of attribute context.
8 9 10 |
# File 'lib/mab/mixin.rb', line 8 def context @context end |
#instance ⇒ Object (readonly)
Returns the value of attribute instance.
8 9 10 |
# File 'lib/mab/mixin.rb', line 8 def instance @instance end |
#name ⇒ Object
Returns the value of attribute name.
7 8 9 |
# File 'lib/mab/mixin.rb', line 7 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/mab/mixin.rb', line 8 def @options end |
#pos=(value) ⇒ Object (writeonly)
Sets the attribute pos
9 10 11 |
# File 'lib/mab/mixin.rb', line 9 def pos=(value) @pos = value end |
Instance Method Details
#attrs_to_s ⇒ Object
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
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_ary ⇒ Object
87 |
# File 'lib/mab/mixin.rb', line 87 def to_ary() nil end |
#to_s ⇒ Object
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_str ⇒ Object
88 |
# File 'lib/mab/mixin.rb', line 88 def to_str() to_s end |