nm 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +3 -0
- data/README.md +101 -21
- data/bench/_slide.nm +5 -0
- data/bench/logger.rb +36 -0
- data/bench/results/nm.txt +30 -0
- data/bench/results/nm_partials.txt +30 -0
- data/bench/results/nm_re_source.txt +30 -0
- data/bench/results/rabl.txt +30 -0
- data/bench/runner.rb +84 -0
- data/bench/slideshow.nm +12 -0
- data/bench/slideshow.rabl +5 -0
- data/bench/slideshow.rb +47 -0
- data/bench/slideshow_partials.nm +8 -0
- data/bench/slideshow_partials.rb +17 -0
- data/bench/template.rb +16 -0
- data/lib/nm.rb +1 -0
- data/lib/nm/ext.rb +31 -0
- data/lib/nm/source.rb +49 -0
- data/lib/nm/template.rb +95 -0
- data/lib/nm/version.rb +1 -1
- data/nm.gemspec +2 -2
- data/script/bench_all.rb +5 -0
- data/script/bench_nm.rb +13 -0
- data/script/bench_nm_partials.rb +13 -0
- data/script/bench_nm_re_source.rb +13 -0
- data/script/bench_rabl.rb +13 -0
- data/test/helper.rb +8 -1
- data/test/support/factory.rb +14 -0
- data/test/support/templates/_list.nm +3 -0
- data/test/support/templates/_locals.nm +3 -0
- data/test/support/templates/_obj.nm +3 -0
- data/test/support/templates/aliases.nm +4 -0
- data/test/support/templates/list.nm +3 -0
- data/test/support/templates/locals.nm +3 -0
- data/test/support/templates/obj.nm +5 -0
- data/test/unit/ext_tests.rb +94 -0
- data/test/unit/source_tests.rb +94 -0
- data/test/unit/template_tests.rb +355 -0
- metadata +58 -33
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA512:
|
3
|
+
metadata.gz: d389b30bc53c3293e4e428b9126c64838124a270d3f535ce360ca3cf984466537f2707a2c149b8ae3d1cbcb231f685a24af6b5ed41f12f0a2b22b7d207f2db09
|
4
|
+
data.tar.gz: 9be9ce99e660a80fe20d6e034de82511081b9ad00cf30dbd0b879ff2ec0ebb671279f30611572ffc00e7be77bea456af5d09b1108147f63a9e2c4d7da4e4d461
|
5
|
+
SHA1:
|
6
|
+
metadata.gz: 6e4b7a81896c762b7c9dea9b642f4eb3c4137967
|
7
|
+
data.tar.gz: 579835a684d21fde132b8ab12bd0423381629f44
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
# Nm
|
2
2
|
|
3
|
-
|
3
|
+
Data templating system. Named for its two main markup methods: "node" and "map". Designed to template data objects for JSON/BSON/whatever/etc serialization.
|
4
4
|
|
5
5
|
## Usage
|
6
6
|
|
7
7
|
Template:
|
8
8
|
|
9
9
|
```ruby
|
10
|
-
# in views/slideshow.json.nm
|
10
|
+
# in /path/to/views/slideshow.json.nm
|
11
11
|
|
12
12
|
node 'slideshow' do
|
13
13
|
node 'start_slide', start_slide
|
@@ -23,35 +23,115 @@ node 'slideshow' do
|
|
23
23
|
end
|
24
24
|
```
|
25
25
|
|
26
|
+
Render:
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
require 'nm'
|
30
|
+
source = Nm::Source.new('/path/to/views')
|
31
|
+
source.render('slideshow.json', {
|
32
|
+
:start_slide => 1,
|
33
|
+
:slides => [ ... ] #=> list of slide objects 1, 2 and 3
|
34
|
+
})
|
35
|
+
```
|
36
|
+
|
26
37
|
Output:
|
27
38
|
|
28
|
-
```
|
29
|
-
{ "slideshow"
|
30
|
-
"start_slide"
|
31
|
-
"slides"
|
32
|
-
{ "id"
|
33
|
-
"title"
|
34
|
-
"thumb"
|
35
|
-
"image"
|
36
|
-
"url"
|
39
|
+
```ruby
|
40
|
+
{ "slideshow" => {
|
41
|
+
"start_slide" => 1,
|
42
|
+
"slides" => [
|
43
|
+
{ "id" => "slide-1",
|
44
|
+
"title" => "Slide 1",
|
45
|
+
"thumb" => "//path/to/slide-1-thumb.jpg",
|
46
|
+
"image" => "//path/to/slide-1-image.jpg",
|
47
|
+
"url" => "//path/to/slide-1-url",
|
37
48
|
},
|
38
|
-
{ "id"
|
39
|
-
"title"
|
40
|
-
"thumb"
|
41
|
-
"image"
|
42
|
-
"url"
|
49
|
+
{ "id" => "slide-2",
|
50
|
+
"title" => "Slide 2",
|
51
|
+
"thumb" => "//path/to/slide-2-thumb.jpg",
|
52
|
+
"image" => "//path/to/slide-2-image.jpg",
|
53
|
+
"url" => "//path/to/slide-2-url",
|
43
54
|
},
|
44
|
-
{ "id"
|
45
|
-
"title"
|
46
|
-
"thumb"
|
47
|
-
"image"
|
48
|
-
"url"
|
55
|
+
{ "id" => "slide-3",
|
56
|
+
"title" => "Slide 3",
|
57
|
+
"thumb" => "//path/to/slide-3-thumb.jpg",
|
58
|
+
"image" => "//path/to/slide-3-image.jpg",
|
59
|
+
"url" => "//path/to/slide-3-url",
|
49
60
|
}
|
50
61
|
]
|
51
62
|
}
|
52
63
|
}
|
53
64
|
```
|
54
65
|
|
66
|
+
## Notes
|
67
|
+
|
68
|
+
### Render Format
|
69
|
+
|
70
|
+
Rendering templates returns a data object (`::Hash` or `::Array`). To serialize, bring in your favorite JSON/BSON/whatever serializer and pass the rendered object to it.
|
71
|
+
|
72
|
+
### Markup Methods
|
73
|
+
|
74
|
+
There are two main markup methods:
|
75
|
+
|
76
|
+
* `node`: create a named attribute on a hash object
|
77
|
+
* `map`: create a list object mapped from a given list
|
78
|
+
|
79
|
+
### Partials
|
80
|
+
|
81
|
+
**Note**: using partials negatively impacts template rendering performance.
|
82
|
+
|
83
|
+
(from example above)
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
# in /path/to/views/slideshow.json.nm
|
87
|
+
|
88
|
+
node 'slideshow' do
|
89
|
+
node 'start_slide', start_slide
|
90
|
+
node 'slides' do
|
91
|
+
map slides do |slide|
|
92
|
+
partial 'slide.json', :slide => slide
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
# in /path/to/views/_slide.json.nm
|
98
|
+
|
99
|
+
node 'id', slide.id
|
100
|
+
node 'title', slide.title
|
101
|
+
node 'image', slide.image_url
|
102
|
+
node 'thumb', slide.thumb_url
|
103
|
+
node 'url', slide.url
|
104
|
+
```
|
105
|
+
|
106
|
+
This will render the same output as above.
|
107
|
+
|
108
|
+
### Markup Aliases
|
109
|
+
|
110
|
+
If you find you need to use a local named `node` or `map`, these markup methods are aliased as
|
111
|
+
`n`, `_node`, `m`, and `_map` respectively. Any combination of aliases is valid:
|
112
|
+
|
113
|
+
```ruby
|
114
|
+
node 'slideshow' do
|
115
|
+
n 'start_slide', start_slide
|
116
|
+
_node 'slides' do
|
117
|
+
_map slides do |slide|
|
118
|
+
_node 'id', slide.id
|
119
|
+
node 'title', slide.title
|
120
|
+
_node 'image', slide.image_url
|
121
|
+
node 'thumb', slide.thumb_url
|
122
|
+
_node 'url', slide.url
|
123
|
+
end
|
124
|
+
m other_slides do |slide|
|
125
|
+
node 'id', slide.id
|
126
|
+
_node 'title', slide.title
|
127
|
+
node 'image', slide.image_url
|
128
|
+
_node 'thumb', slide.thumb_url
|
129
|
+
node 'url', slide.url
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
```
|
134
|
+
|
55
135
|
## Installation
|
56
136
|
|
57
137
|
Add this line to your application's Gemfile:
|
data/bench/_slide.nm
ADDED
data/bench/logger.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'bench/runner'
|
2
|
+
|
3
|
+
module NmBench
|
4
|
+
|
5
|
+
class Logger
|
6
|
+
|
7
|
+
def initialize(file_path)
|
8
|
+
@file = File.open(file_path, 'w')
|
9
|
+
@ios = [@file, $stdout]
|
10
|
+
yield self
|
11
|
+
@file.close
|
12
|
+
end
|
13
|
+
|
14
|
+
def method_missing(meth, *args, &block)
|
15
|
+
@ios.each do |io|
|
16
|
+
io.respond_to?(meth.to_s) ? io.send(meth.to_s, *args, &block) : super
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def respond_to?(*args)
|
21
|
+
@ios.first.respond_to?(args.first.to_s) ? true : super
|
22
|
+
end
|
23
|
+
|
24
|
+
def run_template(runner, name, *args)
|
25
|
+
GC.disable
|
26
|
+
|
27
|
+
Runner.run(runner, name, self, *args)
|
28
|
+
self.puts
|
29
|
+
|
30
|
+
GC.enable
|
31
|
+
GC.start
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
Nm slideshow: 1 times
|
2
|
+
---------------------
|
3
|
+
whysoslow? ..
|
4
|
+
|
5
|
+
mem @ start 41 MB ??
|
6
|
+
mem @ finish 41 MB [31m+ 0 MB, 0%[0m
|
7
|
+
|
8
|
+
user system total real
|
9
|
+
time 0.0 ms 0.0 ms 0.0 ms 5.086 ms
|
10
|
+
|
11
|
+
Nm slideshow: 10 times
|
12
|
+
----------------------
|
13
|
+
whysoslow? ..
|
14
|
+
|
15
|
+
mem @ start 41 MB ??
|
16
|
+
mem @ finish 54 MB [31m+ 13 MB, 32%[0m
|
17
|
+
|
18
|
+
user system total real
|
19
|
+
time 40.0 ms 0.0 ms 40.0 ms 39.215 ms
|
20
|
+
|
21
|
+
Nm slideshow: 100 times
|
22
|
+
-----------------------
|
23
|
+
whysoslow? ..
|
24
|
+
|
25
|
+
mem @ start 54 MB ??
|
26
|
+
mem @ finish 212 MB [31m+ 158 MB, 294%[0m
|
27
|
+
|
28
|
+
user system total real
|
29
|
+
time 320.0 ms 60.0 ms 380.0 ms 379.717 ms
|
30
|
+
|
@@ -0,0 +1,30 @@
|
|
1
|
+
Nm slideshow_partials: 1 times
|
2
|
+
------------------------------
|
3
|
+
whysoslow? ..
|
4
|
+
|
5
|
+
mem @ start 41 MB ??
|
6
|
+
mem @ finish 41 MB [31m+ 0 MB, 0%[0m
|
7
|
+
|
8
|
+
user system total real
|
9
|
+
time 20.0 ms 0.0 ms 20.0 ms 20.947 ms
|
10
|
+
|
11
|
+
Nm slideshow_partials: 10 times
|
12
|
+
-------------------------------
|
13
|
+
whysoslow? ..
|
14
|
+
|
15
|
+
mem @ start 41 MB ??
|
16
|
+
mem @ finish 74 MB [31m+ 33 MB, 81%[0m
|
17
|
+
|
18
|
+
user system total real
|
19
|
+
time 170.0 ms 30.0 ms 200.0 ms 196.232 ms
|
20
|
+
|
21
|
+
Nm slideshow_partials: 100 times
|
22
|
+
--------------------------------
|
23
|
+
whysoslow? ..
|
24
|
+
|
25
|
+
mem @ start 50 MB ??
|
26
|
+
mem @ finish 531 MB [31m+ 480 MB, 952%[0m
|
27
|
+
|
28
|
+
user system total real
|
29
|
+
time 1640.0 ms 310.0 ms 1950.0 ms 1944.464 ms
|
30
|
+
|
@@ -0,0 +1,30 @@
|
|
1
|
+
Nm slideshow: 1 times
|
2
|
+
---------------------
|
3
|
+
whysoslow? ..
|
4
|
+
|
5
|
+
mem @ start 41 MB ??
|
6
|
+
mem @ finish 41 MB [31m+ 0 MB, 0%[0m
|
7
|
+
|
8
|
+
user system total real
|
9
|
+
time 0.0 ms 0.0 ms 0.0 ms 6.727 ms
|
10
|
+
|
11
|
+
Nm slideshow: 10 times
|
12
|
+
----------------------
|
13
|
+
whysoslow? ..
|
14
|
+
|
15
|
+
mem @ start 41 MB ??
|
16
|
+
mem @ finish 51 MB [31m+ 10 MB, 23%[0m
|
17
|
+
|
18
|
+
user system total real
|
19
|
+
time 40.0 ms 10.0 ms 50.0 ms 40.104 ms
|
20
|
+
|
21
|
+
Nm slideshow: 100 times
|
22
|
+
-----------------------
|
23
|
+
whysoslow? ..
|
24
|
+
|
25
|
+
mem @ start 50 MB ??
|
26
|
+
mem @ finish 209 MB [31m+ 159 MB, 320%[0m
|
27
|
+
|
28
|
+
user system total real
|
29
|
+
time 310.0 ms 60.0 ms 370.0 ms 376.581 ms
|
30
|
+
|
@@ -0,0 +1,30 @@
|
|
1
|
+
RABL slideshow: 1 times
|
2
|
+
-----------------------
|
3
|
+
whysoslow? ..
|
4
|
+
|
5
|
+
mem @ start 40 MB ??
|
6
|
+
mem @ finish 40 MB [31m+ 0 MB, 0%[0m
|
7
|
+
|
8
|
+
user system total real
|
9
|
+
time 10.0 ms 0.0 ms 10.0 ms 18.029 ms
|
10
|
+
|
11
|
+
RABL slideshow: 10 times
|
12
|
+
------------------------
|
13
|
+
whysoslow? ..
|
14
|
+
|
15
|
+
mem @ start 40 MB ??
|
16
|
+
mem @ finish 85 MB [31m+ 45 MB, 112%[0m
|
17
|
+
|
18
|
+
user system total real
|
19
|
+
time 170.0 ms 20.0 ms 190.0 ms 193.534 ms
|
20
|
+
|
21
|
+
RABL slideshow: 100 times
|
22
|
+
-------------------------
|
23
|
+
whysoslow? ..
|
24
|
+
|
25
|
+
mem @ start 69 MB ??
|
26
|
+
mem @ finish 458 MB [31m+ 389 MB, 565%[0m
|
27
|
+
|
28
|
+
user system total real
|
29
|
+
time 1530.0 ms 190.0 ms 1720.0 ms 1720.782 ms
|
30
|
+
|
data/bench/runner.rb
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
require 'whysoslow'
|
2
|
+
require 'rabl'
|
3
|
+
require 'nm'
|
4
|
+
|
5
|
+
require 'bench/template'
|
6
|
+
|
7
|
+
module NmBench
|
8
|
+
|
9
|
+
class Runner
|
10
|
+
|
11
|
+
attr_reader :result
|
12
|
+
|
13
|
+
RUNNERS = {}
|
14
|
+
|
15
|
+
def self.run(runner, *args)
|
16
|
+
RUNNERS[runner].new(*args).run
|
17
|
+
end
|
18
|
+
|
19
|
+
def initialize(printer_io, title, num_times, &run_proc)
|
20
|
+
@proc = proc do
|
21
|
+
num_times.times do
|
22
|
+
run_proc.call
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
@printer = Whysoslow::DefaultPrinter.new(printer_io, {
|
27
|
+
:title => "#{title}: #{num_times} times",
|
28
|
+
:verbose => true
|
29
|
+
})
|
30
|
+
@runner = Whysoslow::Runner.new(@printer)
|
31
|
+
end
|
32
|
+
|
33
|
+
def run
|
34
|
+
@runner.run &@proc
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
class RablRunner < Runner
|
40
|
+
|
41
|
+
RUNNERS[:rabl] = self
|
42
|
+
|
43
|
+
def initialize(template_name, printer_io, num_times = 10)
|
44
|
+
template = Template.find(template_name)
|
45
|
+
super(printer_io, "RABL #{template.name}", num_times) do
|
46
|
+
out = Rabl::Renderer.new(template.name, nil, {
|
47
|
+
:view_path => File.expand_path('..', __FILE__),
|
48
|
+
:locals => template.locals,
|
49
|
+
:format => 'hash'
|
50
|
+
}).render
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
class NmRunner < Runner
|
57
|
+
|
58
|
+
RUNNERS[:nm] = self
|
59
|
+
|
60
|
+
def initialize(template_name, printer_io, num_times = 10)
|
61
|
+
template = Template.find(template_name)
|
62
|
+
source = Nm::Source.new(File.expand_path('..', __FILE__))
|
63
|
+
super(printer_io, "Nm #{template.name}", num_times) do
|
64
|
+
out = source.render(template.name, template.locals)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
|
70
|
+
class NmReSourceRunner < Runner
|
71
|
+
|
72
|
+
RUNNERS[:nm_re_source] = self
|
73
|
+
|
74
|
+
def initialize(template_name, printer_io, num_times = 10)
|
75
|
+
template = Template.find(template_name)
|
76
|
+
super(printer_io, "Nm #{template.name}", num_times) do
|
77
|
+
source = Nm::Source.new(File.expand_path('..', __FILE__))
|
78
|
+
out = source.render(template.name, template.locals)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
data/bench/slideshow.nm
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
node 'slideshow' do
|
2
|
+
node 'start_slide', view.start_slide
|
3
|
+
node 'slides' do
|
4
|
+
map view.slides do |slide|
|
5
|
+
node 'id', slide.id
|
6
|
+
node 'image', slide.image_url
|
7
|
+
node 'thumb', slide.thumb_url
|
8
|
+
node 'title', slide.title
|
9
|
+
node 'url', slide.url
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
data/bench/slideshow.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'assert/factory'
|
2
|
+
require 'bench/template'
|
3
|
+
|
4
|
+
module NmBench
|
5
|
+
|
6
|
+
class SlideshowTemplate < Template
|
7
|
+
|
8
|
+
TEMPLATES[:slideshow] = self
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@name = 'slideshow'
|
12
|
+
@locals = { :view => View.new }
|
13
|
+
end
|
14
|
+
|
15
|
+
class View
|
16
|
+
attr_reader :start_slide, :slides
|
17
|
+
|
18
|
+
def initialize
|
19
|
+
@slides = (1..100).map{ |n| Slide.new(n) }
|
20
|
+
@start_slide = @slides.first.id
|
21
|
+
end
|
22
|
+
|
23
|
+
# for RABL template syntax needs
|
24
|
+
def slideshow
|
25
|
+
self
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class Slide
|
30
|
+
attr_reader :id, :image_url, :thumb_url, :title, :url
|
31
|
+
|
32
|
+
def initialize(n)
|
33
|
+
@id = Assert::Factory.integer
|
34
|
+
@image_url = Assert::Factory.url
|
35
|
+
@thumb_url = Assert::Factory.url
|
36
|
+
@title = "Slide #{n}"
|
37
|
+
@url = Assert::Factory.url
|
38
|
+
end
|
39
|
+
|
40
|
+
# for RABL syntax needs
|
41
|
+
alias_method :image, :image_url
|
42
|
+
alias_method :thumb, :thumb_url
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|