Class: Ramaze::Gestalt
- Inherits:
-
Object
- Object
- Ramaze::Gestalt
- Defined in:
- lib/ramaze/gestalt.rb
Overview
Gestalt is the custom HTML/XML builder for Ramaze, based on a very simple DSL it will build your markup.
Instance Attribute Summary collapse
-
#out ⇒ Object
Returns the value of attribute out.
Class Method Summary collapse
-
.build(&block) ⇒ Object
The default way to start building your markup.
Instance Method Summary collapse
-
#_gestalt_build_tag(name, attr = {}, text = []) ⇒ Object
Build a tag for ‘name`, using `args` and an optional block that will be yielded.
-
#_gestalt_call_tag(name, args, &block) ⇒ Object
Calls a particular tag based on the specified parameters.
-
#_gestalt_escape_entities(s) ⇒ Object
Replace common HTML characters such as “ and < with their entities.
-
#initialize(&block) ⇒ Gestalt
constructor
Gestalt.new is like ::build but will return itself.
-
#method_missing(meth, *args, &block) ⇒ Object
Catching all the tags.
-
#p(*args, &block) ⇒ Object
Workaround for Kernel#p to make <p /> tags possible.
-
#select(*args, &block) ⇒ Object
Workaround for Kernel#select to make <select></select> work.
-
#tag(name, *args, &block) ⇒ Object
Shortcut for building tags,.
-
#to_s ⇒ String
(also: #to_str)
Convert the final output of Gestalt to a string.
Constructor Details
#initialize(&block) ⇒ Gestalt
Gestalt.new is like ::build but will return itself. you can either access #out or .to_s it, which will return the actual markup.
Useful for distributed building of one page.
44 45 46 47 |
# File 'lib/ramaze/gestalt.rb', line 44 def initialize(&block) @out = [] instance_eval(&block) if block_given? end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
Catching all the tags. passing it to _gestalt_build_tag
56 57 58 |
# File 'lib/ramaze/gestalt.rb', line 56 def method_missing(meth, *args, &block) _gestalt_call_tag meth, args, &block end |
Instance Attribute Details
#out ⇒ Object
Returns the value of attribute out.
23 24 25 |
# File 'lib/ramaze/gestalt.rb', line 23 def out @out end |
Class Method Details
.build(&block) ⇒ Object
The default way to start building your markup. Takes a block and returns the markup.
31 32 33 |
# File 'lib/ramaze/gestalt.rb', line 31 def self.build(&block) self.new(&block).to_s end |
Instance Method Details
#_gestalt_build_tag(name, attr = {}, text = []) ⇒ Object
Build a tag for ‘name`, using `args` and an optional block that will be yielded.
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/ramaze/gestalt.rb', line 110 def _gestalt_build_tag(name, attr = {}, text = []) @out << "<#{name}" @out << attr.map{|(k,v)| %[ #{k}="#{_gestalt_escape_entities(v)}"] }.join if text != [] or block_given? @out << ">" @out << _gestalt_escape_entities([text].join) if block_given? text = yield @out << text.to_str if text != @out and text.respond_to?(:to_str) end @out << "</#{name}>" else @out << ' />' end end |
#_gestalt_call_tag(name, args, &block) ⇒ Object
Calls a particular tag based on the specified parameters.
89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/ramaze/gestalt.rb', line 89 def _gestalt_call_tag(name, args, &block) if args.size == 1 and args[0].kind_of? Hash # args are just attributes, children in block... _gestalt_build_tag name, args[0], &block elsif args[1].kind_of? Hash # args are text and attributes ie. a('mylink', :href => '/mylink') _gestalt_build_tag(name, args[1], args[0], &block) else # no attributes, but text _gestalt_build_tag name, {}, args, &block end end |
#_gestalt_escape_entities(s) ⇒ Object
Replace common HTML characters such as “ and < with their entities.
131 132 133 134 135 136 137 |
# File 'lib/ramaze/gestalt.rb', line 131 def _gestalt_escape_entities(s) s.to_s.gsub(/&/, '&'). gsub(/"/, '"'). gsub(/'/, '''). gsub(/</, '<'). gsub(/>/, '>') end |
#p(*args, &block) ⇒ Object
Workaround for Kernel#p to make <p /> tags possible.
67 68 69 |
# File 'lib/ramaze/gestalt.rb', line 67 def p(*args, &block) _gestalt_call_tag :p, args, &block end |
#select(*args, &block) ⇒ Object
Workaround for Kernel#select to make <select></select> work.
78 79 80 |
# File 'lib/ramaze/gestalt.rb', line 78 def select(*args, &block) _gestalt_call_tag(:select, args, &block) end |
#tag(name, *args, &block) ⇒ Object
Shortcut for building tags,
146 147 148 |
# File 'lib/ramaze/gestalt.rb', line 146 def tag(name, *args, &block) _gestalt_call_tag(name.to_s, args, &block) end |
#to_s ⇒ String Also known as: to_str
Convert the final output of Gestalt to a string. This method has the following alias: “to_str”.
156 157 158 |
# File 'lib/ramaze/gestalt.rb', line 156 def to_s @out.join end |