nm 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/nm/source.rb CHANGED
@@ -7,10 +7,17 @@ module Nm
7
7
 
8
8
  EXT = ".nm"
9
9
 
10
- attr_reader :root
10
+ attr_reader :root, :template_class
11
11
 
12
- def initialize(root)
12
+ def initialize(root, locals = nil)
13
13
  @root = Pathname.new(root.to_s)
14
+ @template_class = Class.new(Template) do
15
+ (locals || {}).each{ |key, value| define_method(key){ value } }
16
+ end
17
+ end
18
+
19
+ def inspect
20
+ "#<#{self.class}:#{'0x0%x' % (object_id << 1)} @root=#{@root.inspect}>"
14
21
  end
15
22
 
16
23
  def data(file_path)
@@ -18,7 +25,7 @@ module Nm
18
25
  end
19
26
 
20
27
  def render(file_name, locals = nil)
21
- Template.new(self, source_file_path(file_name), locals || {}).__data__
28
+ @template_class.new(self, source_file_path(file_name), locals || {}).__data__
22
29
  end
23
30
 
24
31
  alias_method :partial, :render
data/lib/nm/template.rb CHANGED
@@ -10,21 +10,29 @@ module Nm
10
10
 
11
11
  # apply any given locals to template scope as methods
12
12
  metaclass = class << self; self; end
13
- (args.last.kind_of?(::Hash) ? args.pop : {}).each do |key, value|
14
- metaclass.class_eval{ define_method(key){ value } }
13
+ metaclass.class_eval do
14
+ (args.last.kind_of?(::Hash) ? args.pop : {}).each do |key, value|
15
+ define_method(key){ value }
16
+ end
15
17
  end
16
18
 
17
19
  source_file = args.last.kind_of?(::String) ? args.pop : ''
18
20
  @__source__ = args.last.kind_of?(Source) ? args.pop : DefaultSource.new
19
21
 
20
22
  return if source_file.empty?
21
-
22
- unless File.exists?(source_file)
23
+ if !File.exists?(source_file)
23
24
  raise ArgumentError, "source file `#{source_file}` does not exist"
24
25
  end
26
+
25
27
  instance_eval(@__source__.data(source_file), source_file, 1)
26
28
  end
27
29
 
30
+ def inspect
31
+ "#<Template:#{@__source__.class}:#{'0x0%x' % (@__source__.object_id << 1)}"\
32
+ " @__source__.root=#{@__source__.root.to_s.inspect}"\
33
+ " __data__=#{self.__data__.inspect}>"
34
+ end
35
+
28
36
  def __data__
29
37
  @__dstack__.last
30
38
  end
data/lib/nm/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Nm
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
data/nm.gemspec CHANGED
@@ -13,11 +13,14 @@ Gem::Specification.new do |gem|
13
13
  gem.homepage = "http://github.com/redding/nm"
14
14
  gem.license = 'MIT'
15
15
 
16
- gem.files = `git ls-files`.split($/)
16
+ gem_files = `git ls-files`.split($/)
17
+ gem_files -= gem_files.grep(%r{^(bench)/})
18
+ gem_files -= gem_files.grep(%r{^(script)/})
19
+ gem.files = gem_files
17
20
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
21
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
22
  gem.require_paths = ["lib"]
20
23
 
21
- gem.add_development_dependency("assert", ["~> 2.10"])
24
+ gem.add_development_dependency("assert", ["~> 2.12"])
22
25
 
23
26
  end
@@ -1,6 +1,8 @@
1
1
  require 'assert'
2
2
  require 'nm/source'
3
3
 
4
+ require 'nm/template'
5
+
4
6
  class Nm::Source
5
7
 
6
8
  class UnitTests < Assert::Context
@@ -21,13 +23,26 @@ class Nm::Source
21
23
  end
22
24
  subject{ @source }
23
25
 
24
- should have_readers :root
26
+ should have_readers :root, :template_class
25
27
  should have_imeths :data, :render, :partial
26
28
 
27
29
  should "know its root" do
28
30
  assert_equal @root, subject.root.to_s
29
31
  end
30
32
 
33
+ should "know its template class" do
34
+ assert_true subject.template_class < Nm::Template
35
+ end
36
+
37
+ should "optionally take and apply default locals to its template class" do
38
+ local_name, local_val = [Factory.string, Factory.string]
39
+ source = Nm::Source.new(@root, local_name => local_val)
40
+ template = source.template_class.new
41
+
42
+ assert_responds_to local_name, template
43
+ assert_equal local_val, template.send(local_name)
44
+ end
45
+
31
46
  end
32
47
 
33
48
  class DataTests < InitTests
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nm
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 3
8
+ - 4
9
9
  - 0
10
- version: 0.3.0
10
+ version: 0.4.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kelly Redding
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2014-11-12 00:00:00 Z
19
+ date: 2014-11-25 00:00:00 Z
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  requirement: &id001 !ruby/object:Gem::Requirement
@@ -24,11 +24,11 @@ dependencies:
24
24
  requirements:
25
25
  - - ~>
26
26
  - !ruby/object:Gem::Version
27
- hash: 23
27
+ hash: 27
28
28
  segments:
29
29
  - 2
30
- - 10
31
- version: "2.10"
30
+ - 12
31
+ version: "2.12"
32
32
  type: :development
33
33
  name: assert
34
34
  version_requirements: *id001
@@ -49,19 +49,6 @@ files:
49
49
  - LICENSE.txt
50
50
  - README.md
51
51
  - Rakefile
52
- - bench/_slide.nm
53
- - bench/logger.rb
54
- - bench/results/nm.txt
55
- - bench/results/nm_partials.txt
56
- - bench/results/nm_re_source.txt
57
- - bench/results/rabl.txt
58
- - bench/runner.rb
59
- - bench/slideshow.nm
60
- - bench/slideshow.rabl
61
- - bench/slideshow.rb
62
- - bench/slideshow_partials.nm
63
- - bench/slideshow_partials.rb
64
- - bench/template.rb
65
52
  - lib/nm.rb
66
53
  - lib/nm/ext.rb
67
54
  - lib/nm/source.rb
@@ -69,11 +56,6 @@ files:
69
56
  - lib/nm/version.rb
70
57
  - log/.gitkeep
71
58
  - nm.gemspec
72
- - script/bench_all.rb
73
- - script/bench_nm.rb
74
- - script/bench_nm_partials.rb
75
- - script/bench_nm_re_source.rb
76
- - script/bench_rabl.rb
77
59
  - test/helper.rb
78
60
  - test/support/factory.rb
79
61
  - test/support/templates/_list.nm
data/bench/_slide.nm DELETED
@@ -1,5 +0,0 @@
1
- node 'id', slide.id
2
- node 'title', slide.title
3
- node 'image', slide.image_url
4
- node 'thumb', slide.thumb_url
5
- node 'url', slide.url
data/bench/logger.rb DELETED
@@ -1,36 +0,0 @@
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
data/bench/results/nm.txt DELETED
@@ -1,30 +0,0 @@
1
- Nm slideshow: 1 times
2
- ---------------------
3
- whysoslow? ..
4
-
5
- mem @ start 41 MB ??
6
- mem @ finish 41 MB + 0 MB, 0%
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 + 13 MB, 32%
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 + 158 MB, 294%
27
-
28
- user system total real
29
- time 320.0 ms 60.0 ms 380.0 ms 379.717 ms
30
-
@@ -1,30 +0,0 @@
1
- Nm slideshow_partials: 1 times
2
- ------------------------------
3
- whysoslow? ..
4
-
5
- mem @ start 41 MB ??
6
- mem @ finish 41 MB + 0 MB, 0%
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 + 33 MB, 81%
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 + 480 MB, 952%
27
-
28
- user system total real
29
- time 1640.0 ms 310.0 ms 1950.0 ms 1944.464 ms
30
-
@@ -1,30 +0,0 @@
1
- Nm slideshow: 1 times
2
- ---------------------
3
- whysoslow? ..
4
-
5
- mem @ start 41 MB ??
6
- mem @ finish 41 MB + 0 MB, 0%
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 + 10 MB, 23%
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 + 159 MB, 320%
27
-
28
- user system total real
29
- time 310.0 ms 60.0 ms 370.0 ms 376.581 ms
30
-
@@ -1,30 +0,0 @@
1
- RABL slideshow: 1 times
2
- -----------------------
3
- whysoslow? ..
4
-
5
- mem @ start 40 MB ??
6
- mem @ finish 40 MB + 0 MB, 0%
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 + 45 MB, 112%
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 + 389 MB, 565%
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 DELETED
@@ -1,84 +0,0 @@
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 DELETED
@@ -1,12 +0,0 @@
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.rabl DELETED
@@ -1,5 +0,0 @@
1
- object locals[:view].slideshow => :slideshow
2
- attributes :start_slide
3
- child(:slides, :object_root => false, :root => :slides){
4
- attributes :id, :title, :url, :image, :thumb
5
- }
data/bench/slideshow.rb DELETED
@@ -1,47 +0,0 @@
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
@@ -1,8 +0,0 @@
1
- node 'slideshow' do
2
- node 'start_slide', view.start_slide
3
- node 'slides' do
4
- map view.slides do |slide|
5
- partial '_slide', :slide => slide
6
- end
7
- end
8
- end
@@ -1,17 +0,0 @@
1
- require 'assert/factory'
2
- require 'bench/slideshow'
3
-
4
- module NmBench
5
-
6
- class SlideshowPartialsTemplate < SlideshowTemplate
7
-
8
- TEMPLATES[:slideshow_partials] = self
9
-
10
- def initialize
11
- super
12
- @name = 'slideshow_partials'
13
- end
14
-
15
- end
16
-
17
- end
data/bench/template.rb DELETED
@@ -1,16 +0,0 @@
1
- module NmBench
2
-
3
- class Template
4
-
5
- attr_reader :name, :locals
6
-
7
- TEMPLATES = {}
8
- require 'bench/slideshow'
9
-
10
- def self.find(name)
11
- TEMPLATES[name].new
12
- end
13
-
14
- end
15
-
16
- end
data/script/bench_all.rb DELETED
@@ -1,6 +0,0 @@
1
- # $ bundle exec ruby script/bench.rb
2
-
3
- require 'script/bench_rabl'
4
- require 'script/bench_nm'
5
- require 'script/bench_nm_partials'
6
- require 'script/bench_nm_re_source'
data/script/bench_nm.rb DELETED
@@ -1,13 +0,0 @@
1
- # $ bundle exec ruby script/bench.rb
2
-
3
- # require pry for debugging (`binding.pry`)
4
- require 'pry'
5
-
6
- require 'bench/logger'
7
- require 'bench/slideshow'
8
-
9
- NmBench::Logger.new('bench/results/nm.txt') do |logger|
10
- logger.run_template(:nm, :slideshow, 1)
11
- logger.run_template(:nm, :slideshow, 10)
12
- logger.run_template(:nm, :slideshow, 100)
13
- end
@@ -1,13 +0,0 @@
1
- # $ bundle exec ruby script/bench.rb
2
-
3
- # require pry for debugging (`binding.pry`)
4
- require 'pry'
5
-
6
- require 'bench/logger'
7
- require 'bench/slideshow_partials'
8
-
9
- NmBench::Logger.new('bench/results/nm_partials.txt') do |logger|
10
- logger.run_template(:nm, :slideshow_partials, 1)
11
- logger.run_template(:nm, :slideshow_partials, 10)
12
- logger.run_template(:nm, :slideshow_partials, 100)
13
- end
@@ -1,13 +0,0 @@
1
- # $ bundle exec ruby script/bench.rb
2
-
3
- # require pry for debugging (`binding.pry`)
4
- require 'pry'
5
-
6
- require 'bench/logger'
7
- require 'bench/slideshow'
8
-
9
- NmBench::Logger.new('bench/results/nm_re_source.txt') do |logger|
10
- logger.run_template(:nm_re_source, :slideshow, 1)
11
- logger.run_template(:nm_re_source, :slideshow, 10)
12
- logger.run_template(:nm_re_source, :slideshow, 100)
13
- end
data/script/bench_rabl.rb DELETED
@@ -1,13 +0,0 @@
1
- # $ bundle exec ruby script/bench.rb
2
-
3
- # require pry for debugging (`binding.pry`)
4
- require 'pry'
5
-
6
- require 'bench/logger'
7
- require 'bench/slideshow'
8
-
9
- NmBench::Logger.new('bench/results/rabl.txt') do |logger|
10
- logger.run_template(:rabl, :slideshow, 1)
11
- logger.run_template(:rabl, :slideshow, 10)
12
- logger.run_template(:rabl, :slideshow, 100)
13
- end