deklarativna 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +8 -1
- data/VERSION +1 -1
- data/deklarativna.gemspec +3 -2
- data/examples/more_interesting_sinatra_example/example.rb +62 -0
- data/lib/deklarativna.rb +28 -0
- data/lib/deklarativna_core.rb +20 -0
- data/run_test.sh +1 -7
- metadata +5 -4
data/README.md
CHANGED
@@ -8,7 +8,7 @@ to write *human readable code* with not much effort.
|
|
8
8
|
|
9
9
|
##Conception
|
10
10
|
This work has been started as a tool for teaching *declarative programming*
|
11
|
-
and *DSL creation* to
|
11
|
+
and *DSL creation* to universitary students.
|
12
12
|
This is a work in progress and *I hope* someday will have integration
|
13
13
|
with the most popular web frameworks
|
14
14
|
|
@@ -19,6 +19,13 @@ Support for *XML* tags with *xml_single_tag* and *xml_double_tag*
|
|
19
19
|
is added.
|
20
20
|
Core module classes and helpers have *full support* for attributes.
|
21
21
|
|
22
|
+
##Installation
|
23
|
+
You can download this gem from RubyGems.org by running
|
24
|
+
|
25
|
+
```bash
|
26
|
+
$ gem install deklarativna
|
27
|
+
```
|
28
|
+
|
22
29
|
##Usage
|
23
30
|
###Function Based Approach
|
24
31
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.2
|
data/deklarativna.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "deklarativna"
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["David Litvak"]
|
12
|
-
s.date = "2012-01-
|
12
|
+
s.date = "2012-01-04"
|
13
13
|
s.description = "A Declarative HTML embedded DSL for HTML/XML Templating in Ruby,\n This gem was intended to be use as teaching material for universitary students."
|
14
14
|
s.email = "david.litvakb@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -29,6 +29,7 @@ Gem::Specification.new do |s|
|
|
29
29
|
"deklarativna.gemspec",
|
30
30
|
"examples/class_based_example.rb",
|
31
31
|
"examples/dinamic_sinatra_example.rb",
|
32
|
+
"examples/more_interesting_sinatra_example/example.rb",
|
32
33
|
"examples/simple_example.rb",
|
33
34
|
"examples/static_sinatra_example.rb",
|
34
35
|
"lib/deklarativna.rb",
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'sinatra/base'
|
3
|
+
require 'deklarativna'
|
4
|
+
|
5
|
+
class DynamicTemplate < BaseTemplate
|
6
|
+
attr_accessor :content
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@content = {}
|
10
|
+
yield self if block_given?
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
def _head
|
15
|
+
[
|
16
|
+
title { @content["title"] }
|
17
|
+
]
|
18
|
+
end
|
19
|
+
|
20
|
+
def _body
|
21
|
+
begin
|
22
|
+
#Just for showing how would a simple 404 would work
|
23
|
+
raise "404" if @content.count < 1
|
24
|
+
body_content = [
|
25
|
+
h1 { @content["title"] },
|
26
|
+
p { @content["first_line"] }
|
27
|
+
]
|
28
|
+
if @content["a_list"].respond_to? :each
|
29
|
+
@content["a_list"].each { |elem|
|
30
|
+
body_content.push( p { elem })
|
31
|
+
}
|
32
|
+
end
|
33
|
+
body_content
|
34
|
+
rescue
|
35
|
+
[
|
36
|
+
h1 { "#{$!} - Object not found" },
|
37
|
+
p { puts @content }
|
38
|
+
]
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
class DeklarativnaSinatraRemixed < Sinatra::Base
|
44
|
+
get '/' do
|
45
|
+
template = DynamicTemplate.new { |instance|
|
46
|
+
instance.content["title"] = "Deklarativna"
|
47
|
+
instance.content["first_line"] = "Hey how cool is this?"
|
48
|
+
instance.content["a_list"] = [
|
49
|
+
"really cool",
|
50
|
+
"cool cool",
|
51
|
+
"more than cool"
|
52
|
+
]
|
53
|
+
}
|
54
|
+
template.render
|
55
|
+
end
|
56
|
+
|
57
|
+
get '/404' do
|
58
|
+
DynamicTemplate.new.render
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
DeklarativnaSinatraRemixed.run!
|
data/lib/deklarativna.rb
CHANGED
@@ -1,7 +1,15 @@
|
|
1
1
|
require 'deklarativna_core'
|
2
2
|
|
3
|
+
## Module
|
4
|
+
# Deklarativna is a module thought for creating embedded HTML/XML
|
5
|
+
# templates fully written in ruby
|
6
|
+
#
|
7
|
+
# It can provide its functionallity to any of your ruby existing code
|
8
|
+
# just by adding `include Deklarativna` inside your classes or modules
|
3
9
|
module Deklarativna
|
4
10
|
|
11
|
+
## Private Method
|
12
|
+
# It metaprograms most of the module's methods
|
5
13
|
def self.included(base)
|
6
14
|
nesting_renderables = ["html", "head", "body", "p", "div", "span",
|
7
15
|
"table", "tr", "td", "ul", "ol", "li",
|
@@ -43,32 +51,48 @@ module Deklarativna
|
|
43
51
|
end
|
44
52
|
end
|
45
53
|
|
54
|
+
## Public Method
|
55
|
+
# Helper for adding your javascript to the template
|
46
56
|
def javascript attributes={}, &script_text_block
|
47
57
|
attributes["type"] = "text/javascript"
|
48
58
|
script attributes, &script_text_block
|
49
59
|
end
|
50
60
|
|
61
|
+
## Public Method
|
62
|
+
# Helper for adding your css to the template
|
51
63
|
def css attributes={}, &style_text_block
|
52
64
|
attributes["type"] = "text/css"
|
53
65
|
style attributes, &style_text_block
|
54
66
|
end
|
55
67
|
|
68
|
+
## Public Method
|
69
|
+
# Helper for adding your comments to the template
|
56
70
|
def comment &comment_block
|
57
71
|
comment_renderable_string comment_block
|
58
72
|
end
|
59
73
|
|
74
|
+
## Public Method
|
75
|
+
# Helper for adding XML tags with the format '<tagname />'
|
60
76
|
def xml_single_tag tag_name, attributes={}
|
61
77
|
single_tag_renderable_string tag_name.downcase, attributes
|
62
78
|
end
|
63
79
|
|
80
|
+
## Public Method
|
81
|
+
# Helper for adding XML tags with the format '<tagname></tagname>'
|
64
82
|
def xml_double_tag tag_name, attributes={}, &html_block
|
65
83
|
nesting_renderable_string tag_name.downcase, html_block, attributes
|
66
84
|
end
|
67
85
|
end
|
68
86
|
|
87
|
+
## Public Class
|
88
|
+
# Base Class for Template Creation using Deklarativna Class Based Model
|
89
|
+
# If you need any special implementation, you can create your own using
|
90
|
+
# this one as example, or just extend this one
|
69
91
|
class BaseTemplate
|
70
92
|
include Deklarativna
|
71
93
|
|
94
|
+
## Public Method
|
95
|
+
# Creates the rendered html
|
72
96
|
def render
|
73
97
|
html {[
|
74
98
|
head {
|
@@ -80,9 +104,13 @@ class BaseTemplate
|
|
80
104
|
]}
|
81
105
|
end
|
82
106
|
|
107
|
+
## Template Method
|
108
|
+
# Should be redefined in your own classes
|
83
109
|
def _head
|
84
110
|
end
|
85
111
|
|
112
|
+
## Template Method
|
113
|
+
# Should be redefined in your own classes
|
86
114
|
def _body
|
87
115
|
end
|
88
116
|
end
|
data/lib/deklarativna_core.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
module Deklarativna
|
2
2
|
|
3
|
+
## Abstract Class
|
4
|
+
# Base Class for all renderables
|
3
5
|
class Renderable
|
4
6
|
attr_accessor :tag_name,:attributes
|
5
7
|
|
@@ -7,6 +9,8 @@ module Deklarativna
|
|
7
9
|
initialization_block.call self
|
8
10
|
end
|
9
11
|
|
12
|
+
## Private Method
|
13
|
+
# This method renders the attributes for any Renderable class
|
10
14
|
def render_attributes
|
11
15
|
rendering_tags = []
|
12
16
|
attribute_list = @attributes.sort if @attributes.respond_to? :sort
|
@@ -19,6 +23,9 @@ module Deklarativna
|
|
19
23
|
end
|
20
24
|
end
|
21
25
|
|
26
|
+
## Public Class
|
27
|
+
# This class is intended to render tags
|
28
|
+
# following the '<tagname></tagname>' pattern
|
22
29
|
class NestingRenderable < Renderable
|
23
30
|
attr_accessor :content
|
24
31
|
|
@@ -41,18 +48,25 @@ module Deklarativna
|
|
41
48
|
end
|
42
49
|
end
|
43
50
|
|
51
|
+
## Public Class
|
52
|
+
# This class is intended to render comments
|
44
53
|
class CommentRenderable < NestingRenderable
|
45
54
|
def to_s
|
46
55
|
"<!--#{proc_call}-->"
|
47
56
|
end
|
48
57
|
end
|
49
58
|
|
59
|
+
## Public Class
|
60
|
+
# This class is intended to render tags
|
61
|
+
# following the '<tagname />' pattern
|
50
62
|
class SingleTagRenderable < Renderable
|
51
63
|
def to_s
|
52
64
|
"<#{@tag_name}#{render_attributes} />"
|
53
65
|
end
|
54
66
|
end
|
55
67
|
|
68
|
+
## Private Method
|
69
|
+
# This is a helper for factory methods
|
56
70
|
def renderable_string renderable_class, block, attributes={}, tag_name=""
|
57
71
|
(renderable_class.new { |instance|
|
58
72
|
instance.tag_name = tag_name
|
@@ -63,14 +77,20 @@ module Deklarativna
|
|
63
77
|
}).to_s
|
64
78
|
end
|
65
79
|
|
80
|
+
## Private Method
|
81
|
+
# Factory Method for creating single tag renderables
|
66
82
|
def single_tag_renderable_string tag_name, attributes={}
|
67
83
|
renderable_string SingleTagRenderable, nil, attributes, tag_name
|
68
84
|
end
|
69
85
|
|
86
|
+
## Private Method
|
87
|
+
# Factory Method for creating nesting renderables
|
70
88
|
def nesting_renderable_string tag_name, block, attributes={}
|
71
89
|
renderable_string NestingRenderable, block, attributes, tag_name
|
72
90
|
end
|
73
91
|
|
92
|
+
## Private Method
|
93
|
+
# Factory Method for creating comment renderables
|
74
94
|
def comment_renderable_string comment_block
|
75
95
|
renderable_string CommentRenderable, comment_block
|
76
96
|
end
|
data/run_test.sh
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: deklarativna
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- David Litvak
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-01-
|
18
|
+
date: 2012-01-04 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
requirement: &id001 !ruby/object:Gem::Requirement
|
@@ -115,6 +115,7 @@ files:
|
|
115
115
|
- deklarativna.gemspec
|
116
116
|
- examples/class_based_example.rb
|
117
117
|
- examples/dinamic_sinatra_example.rb
|
118
|
+
- examples/more_interesting_sinatra_example/example.rb
|
118
119
|
- examples/simple_example.rb
|
119
120
|
- examples/static_sinatra_example.rb
|
120
121
|
- lib/deklarativna.rb
|