Class: Mab::Mixin::Tag
- Inherits:
-
Object
- Object
- Mab::Mixin::Tag
- Defined in:
- lib/mab/mixin.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#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.
-
#has_content ⇒ Object
Returns the value of attribute has_content.
-
#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.
Instance Method Summary collapse
- #attrs_to_s ⇒ Object
-
#initialize(name, options, context, instance = nil) ⇒ Tag
constructor
A new instance of Tag.
- #insert(*args, &blk) ⇒ Object
- #merge_attributes(*args) ⇒ Object
- #method_missing(name, *args, &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.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/mab/mixin.rb', line 10 def initialize(name, , context, instance = nil) @name = name @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
#attributes ⇒ Object
Returns the value of attribute attributes.
7 8 9 |
# File 'lib/mab/mixin.rb', line 7 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 |
#has_content ⇒ Object
Returns the value of attribute has_content.
7 8 9 |
# File 'lib/mab/mixin.rb', line 7 def has_content @has_content 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 |
Instance Method Details
#attrs_to_s ⇒ Object
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
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_ary ⇒ Object
101 |
# File 'lib/mab/mixin.rb', line 101 def to_ary() nil end |
#to_s ⇒ Object
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_str ⇒ Object
102 |
# File 'lib/mab/mixin.rb', line 102 def to_str() to_s end |