hobber 0.6.4 → 0.6.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Gemfile +3 -0
- data/lib/hobber/renderable_object.rb +16 -51
- data/lib/hobber/renderer/tilt.rb +30 -0
- data/lib/hobber/tmpl_var_extractor/yaml.rb +36 -0
- data/lib/hobber/version.rb +1 -1
- data/spec/lib/hobber/render_action_spec.rb +1 -1
- metadata +33 -11
data/Gemfile
CHANGED
@@ -1,23 +1,24 @@
|
|
1
|
-
require 'tilt'
|
2
|
-
require 'yaml'
|
3
|
-
require 'active_support/core_ext/hash/indifferent_access'
|
4
|
-
|
1
|
+
require 'hobber/renderer/tilt'
|
2
|
+
require 'hobber/tmpl_var_extractor/yaml'
|
5
3
|
|
6
4
|
module Hobber
|
7
|
-
class ProblemParsingYaml < RuntimeError; end
|
8
5
|
class RenderError < RuntimeError; end
|
6
|
+
|
9
7
|
class RenderableObject
|
10
|
-
attr_reader :path, :
|
8
|
+
attr_reader :path, :tmpl_content, :renderer
|
9
|
+
|
10
|
+
def initialize(path, a_renderer=nil, a_tmpl_var_extractor=nil)
|
11
|
+
@path = path
|
12
|
+
@tmpl_content = block_given? ? yield(self) : File.open(@path, 'r:UTF-8') { |f| f.read }
|
11
13
|
|
12
|
-
|
13
|
-
@
|
14
|
-
@data = yield(self) if block_given?
|
14
|
+
@renderer = a_renderer || Renderer::Tilt.new
|
15
|
+
@tmpl_var_extractor = a_tmpl_var_extractor || TmplVarExtractor::Yaml.new(@tmpl_content, @path)
|
15
16
|
end
|
16
17
|
|
17
|
-
def render(vars={},
|
18
|
-
|
18
|
+
def render(vars={}, ctx=Object.new, &block)
|
19
|
+
@renderer.render(path, tmpl_content, ctx, tmpl_vars.merge(vars), &block)
|
19
20
|
end
|
20
|
-
|
21
|
+
|
21
22
|
def to_a
|
22
23
|
[self]
|
23
24
|
end
|
@@ -30,48 +31,12 @@ module Hobber
|
|
30
31
|
tmpl_vars.fetch(*a)
|
31
32
|
end
|
32
33
|
|
33
|
-
def
|
34
|
-
@
|
34
|
+
def tmpl_content
|
35
|
+
@tmpl_var_extractor.tmpl_content
|
35
36
|
end
|
36
37
|
|
37
38
|
def tmpl_vars
|
38
|
-
@tmpl_vars
|
39
|
-
end
|
40
|
-
def _extract_tmpl_vars
|
41
|
-
@tmpl_vars = {}
|
42
|
-
if data.match(/\s*---.*---/m)
|
43
|
-
ignore, yaml_buffer, template_data = data.split(/---/,3)
|
44
|
-
@data = template_data
|
45
|
-
@tmpl_vars = YAML.parse(yaml_buffer).to_ruby
|
46
|
-
end
|
47
|
-
HashWithIndifferentAccess.new(@tmpl_vars)
|
48
|
-
rescue Psych::SyntaxError => e
|
49
|
-
raise ProblemParsingYaml.new([e.message, "while trying to extract tmpl_vars from [#{path}]"] * " -- ")
|
50
|
-
end
|
51
|
-
|
52
|
-
private
|
53
|
-
|
54
|
-
def _render_template_chain(path, data, context, vars, &block)
|
55
|
-
# termination condition, if tilt template class
|
56
|
-
# is
|
57
|
-
tilt_template_class = Tilt[path]
|
58
|
-
unless tilt_template_class
|
59
|
-
return data
|
60
|
-
end
|
61
|
-
|
62
|
-
template = tilt_template_class.new(@path) { |t| data }
|
63
|
-
|
64
|
-
# remove extention
|
65
|
-
path = path.gsub(/\.\w+$/,'')
|
66
|
-
data = template.render(context, vars, &block)
|
67
|
-
|
68
|
-
# iterate again to next available template
|
69
|
-
# engine
|
70
|
-
_render_template_chain(path, data, context, vars, &block)
|
71
|
-
rescue => e
|
72
|
-
render_error = RenderError.new("#{self.class}: While rendering #{path} with #{tilt_template_class}: #{e.message}")
|
73
|
-
render_error.set_backtrace(e.backtrace)
|
74
|
-
raise render_error
|
39
|
+
@tmpl_var_extractor.tmpl_vars
|
75
40
|
end
|
76
41
|
end
|
77
42
|
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'tilt'
|
2
|
+
|
3
|
+
module Hobber
|
4
|
+
module Renderer
|
5
|
+
class Tilt
|
6
|
+
def render(path, data, context, vars, &block)
|
7
|
+
# termination condition, if tilt template class
|
8
|
+
# is
|
9
|
+
tilt_template_class = ::Tilt[path]
|
10
|
+
unless tilt_template_class
|
11
|
+
return data
|
12
|
+
end
|
13
|
+
|
14
|
+
template = tilt_template_class.new(path) { |t| data }
|
15
|
+
|
16
|
+
# remove extention
|
17
|
+
path = path.gsub(/\.\w+$/,'')
|
18
|
+
data = template.render(context, vars, &block)
|
19
|
+
|
20
|
+
# iterate again to next available template
|
21
|
+
# engine
|
22
|
+
render(path, data, context, vars, &block)
|
23
|
+
rescue => e
|
24
|
+
render_error = RenderError.new("#{self.class}: While rendering #{path} with #{tilt_template_class}: #{e.message}")
|
25
|
+
render_error.set_backtrace(e.backtrace)
|
26
|
+
raise render_error
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'active_support/core_ext/hash/indifferent_access'
|
3
|
+
|
4
|
+
module Hobber
|
5
|
+
class ProblemParsingYaml < RuntimeError; end
|
6
|
+
module TmplVarExtractor
|
7
|
+
class Yaml
|
8
|
+
attr_reader :combined_content, :tmpl_content, :tmpl_vars
|
9
|
+
|
10
|
+
def initialize(combined_content, path=nil)
|
11
|
+
@combined_content = combined_content
|
12
|
+
@path = path
|
13
|
+
|
14
|
+
@tmpl_content = combined_content
|
15
|
+
@tmpl_vars = {}
|
16
|
+
extract
|
17
|
+
end
|
18
|
+
|
19
|
+
def extract
|
20
|
+
@tmpl_vars = {}
|
21
|
+
if @combined_content.match(/\s*---.*---/m)
|
22
|
+
ignore, yaml_buffer, tmpl_content = @combined_content.split(/---/,3)
|
23
|
+
@tmpl_content = tmpl_content
|
24
|
+
@tmpl_vars = YAML.parse(yaml_buffer).to_ruby
|
25
|
+
@tmpl_vars = HashWithIndifferentAccess.new(@tmpl_vars)
|
26
|
+
end
|
27
|
+
# rescue Psych::SyntaxError => e
|
28
|
+
rescue => e
|
29
|
+
debugger
|
30
|
+
error = ProblemParsingYaml.new(["#{e.message} (#{e.class})", "while trying to extract tmpl_vars from [#{@path}]"] * " -- ")
|
31
|
+
error.set_backtrace(e.backtrace)
|
32
|
+
raise error
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/lib/hobber/version.rb
CHANGED
@@ -6,7 +6,7 @@ module Hobber
|
|
6
6
|
subject {described_class.new([])}
|
7
7
|
context "#layout" do
|
8
8
|
it "adds a layout" do
|
9
|
-
robject = RenderableObject.new('a_path')
|
9
|
+
robject = RenderableObject.new('a_path') { '' }
|
10
10
|
subject.layout robject
|
11
11
|
end
|
12
12
|
it "raises when adding something other than RenderableObject" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hobber
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-07-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: tilt
|
16
|
-
requirement:
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,15 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements:
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
25
30
|
- !ruby/object:Gem::Dependency
|
26
31
|
name: thor
|
27
|
-
requirement:
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
28
33
|
none: false
|
29
34
|
requirements:
|
30
35
|
- - ! '>='
|
@@ -32,10 +37,15 @@ dependencies:
|
|
32
37
|
version: '0'
|
33
38
|
type: :runtime
|
34
39
|
prerelease: false
|
35
|
-
version_requirements:
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
36
46
|
- !ruby/object:Gem::Dependency
|
37
47
|
name: active_support
|
38
|
-
requirement:
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
39
49
|
none: false
|
40
50
|
requirements:
|
41
51
|
- - ! '>='
|
@@ -43,10 +53,15 @@ dependencies:
|
|
43
53
|
version: '0'
|
44
54
|
type: :runtime
|
45
55
|
prerelease: false
|
46
|
-
version_requirements:
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
47
62
|
- !ruby/object:Gem::Dependency
|
48
63
|
name: sprockets
|
49
|
-
requirement:
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
50
65
|
none: false
|
51
66
|
requirements:
|
52
67
|
- - ! '>='
|
@@ -54,7 +69,12 @@ dependencies:
|
|
54
69
|
version: '0'
|
55
70
|
type: :runtime
|
56
71
|
prerelease: false
|
57
|
-
version_requirements:
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
58
78
|
description: Minimal implementation ..
|
59
79
|
email:
|
60
80
|
- boy.maas@gmail.com
|
@@ -74,6 +94,8 @@ files:
|
|
74
94
|
- lib/hobber/renderable_object.rb
|
75
95
|
- lib/hobber/rendered_object.rb
|
76
96
|
- lib/hobber/rendered_object_saver.rb
|
97
|
+
- lib/hobber/renderer/tilt.rb
|
98
|
+
- lib/hobber/tmpl_var_extractor/yaml.rb
|
77
99
|
- lib/hobber/version.rb
|
78
100
|
- spec/integration_spec.rb
|
79
101
|
- spec/lib/hobber/render_action_spec.rb
|
@@ -112,7 +134,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
112
134
|
version: '0'
|
113
135
|
requirements: []
|
114
136
|
rubyforge_project:
|
115
|
-
rubygems_version: 1.8.
|
137
|
+
rubygems_version: 1.8.24
|
116
138
|
signing_key:
|
117
139
|
specification_version: 3
|
118
140
|
summary: Minimal implementation ..
|