Module: Deklarativna

Included in:
BaseTemplate
Defined in:
lib/deklarativna.rb,
lib/deklarativna_core.rb

Overview

Module Deklarativna is a module thought for creating embedded HTML/XML templates fully written in ruby

It can provide its functionallity to any of your ruby existing code just by adding ‘include Deklarativna` inside your classes or modules

Defined Under Namespace

Classes: CommentRenderable, NestingRenderable, Renderable, SingleTagRenderable

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Private Method It metaprograms most of the module’s methods



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/deklarativna.rb', line 13

def self.included(base)
  nesting_renderables = ["html", "head", "body", "header", "footer",
                         "p", "div", "span", "table", "tr", "td",
                         "ul", "ol", "li", "center", "dd", "dl",
                         "dt", "i", "b", "em", "strong", "title", "label",
                         "pre", "script", "style", "a", "form", "textarea",
                         "select", "option", "article", "section", "code",
                         "abbr", "acronym", "address", "bdo", "address",
                         "big", "tt", "small", "blockquote", "button",
                         "caption", "dfn", "cite", "code", "samp", "kbd",
                         "var", "colgroup", "del", "ins", "dir", "fieldset",
                         "legend", "frameset", "iframe", "noframes",
                         "noscript", "object", "optgroup", "q","sub", "sup",
                         "thead", "tfoot", "tbody"]
  (1..6).each { |e| nesting_renderables.push "h#{e}" }

  nesting_renderables.each do
    |tag_name|
    send :define_method, tag_name do
      |*args, &block|
      attributes = args[0] if !args.nil?
      nesting_renderable_string tag_name, block, attributes
    end
  end

  single_tag_renderables = ["meta", "br", "hr",
                            "link", "input", "img",
                            "base", "col", "frame",
                            "param"]
  single_tag_renderables.each do
    |tag_name|
    send :define_method, tag_name do
      |*args|
      attributes = args[0] if !args.nil?
      single_tag_renderable_string tag_name, attributes
    end
  end

  form_input_renderables = ["text", "password", "checkbox",
                            "radio", "submit"]
  form_input_renderables.each do
    |type|
    send :define_method, type do
      |*args|
      attributes = args[0] if !args.nil?
      attributes ||= {}
      attributes["type"] = type
      input attributes
    end
  end
end

Instance Method Details

#comment(&comment_block) ⇒ Object

Public Method Helper for adding your comments to the template



81
82
83
# File 'lib/deklarativna.rb', line 81

def comment &comment_block
  comment_renderable_string comment_block
end

#comment_renderable_string(comment_block) ⇒ Object

Private Method Factory Method for creating comment renderables



94
95
96
# File 'lib/deklarativna_core.rb', line 94

def comment_renderable_string comment_block
  renderable_string CommentRenderable, comment_block
end

#css(attributes = {}, &style_text_block) ⇒ Object

Public Method Helper for adding your css to the template



74
75
76
77
# File 'lib/deklarativna.rb', line 74

def css attributes={}, &style_text_block
  attributes["type"] = "text/css"
  style attributes, &style_text_block
end

#javascript(attributes = {}, &script_text_block) ⇒ Object

Public Method Helper for adding your javascript to the template



67
68
69
70
# File 'lib/deklarativna.rb', line 67

def javascript attributes={}, &script_text_block
  attributes["type"] = "text/javascript"
  script attributes, &script_text_block
end

#nesting_renderable_string(tag_name, block, attributes = {}) ⇒ Object

Private Method Factory Method for creating nesting renderables



88
89
90
# File 'lib/deklarativna_core.rb', line 88

def nesting_renderable_string tag_name, block, attributes={}
  renderable_string NestingRenderable, block, attributes, tag_name
end

#renderable_string(renderable_class, block, attributes = {}, tag_name = "") ⇒ Object

Private Method This is a helper for factory methods



70
71
72
73
74
75
76
77
78
# File 'lib/deklarativna_core.rb', line 70

def renderable_string renderable_class, block, attributes={}, tag_name=""
  (renderable_class.new { |instance|
    instance.tag_name = tag_name
    instance.attributes = attributes
    if instance.respond_to? :content
      instance.content = block
    end
  }).to_s
end

#single_tag_renderable_string(tag_name, attributes = {}) ⇒ Object

Private Method Factory Method for creating single tag renderables



82
83
84
# File 'lib/deklarativna_core.rb', line 82

def single_tag_renderable_string tag_name, attributes={}
  renderable_string SingleTagRenderable, nil, attributes, tag_name
end

#xml_double_tag(tag_name, attributes = {}, &html_block) ⇒ Object

Public Method Helper for adding XML tags with the format ‘<tagname></tagname>’



93
94
95
# File 'lib/deklarativna.rb', line 93

def xml_double_tag tag_name, attributes={}, &html_block
  nesting_renderable_string tag_name.downcase, html_block, attributes
end

#xml_single_tag(tag_name, attributes = {}) ⇒ Object

Public Method Helper for adding XML tags with the format ‘<tagname />’



87
88
89
# File 'lib/deklarativna.rb', line 87

def xml_single_tag tag_name, attributes={}
  single_tag_renderable_string tag_name.downcase, attributes
end