petroglyph 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 +12 -1
- data/lib/petroglyph.rb +11 -0
- data/lib/petroglyph/engine.rb +9 -6
- data/lib/petroglyph/railtie.rb +10 -0
- data/lib/petroglyph/scope.rb +2 -1
- data/lib/petroglyph/template.rb +64 -0
- data/lib/petroglyph/version.rb +1 -1
- data/spec/engine_spec.rb +5 -0
- data/spec/sinatra_integration_spec.rb +7 -3
- metadata +12 -10
data/README.md
CHANGED
@@ -77,9 +77,20 @@ This can be overridden by re-implementing the `Petroglyph.partial(name)` method.
|
|
77
77
|
partial :tea, :tea => tea
|
78
78
|
end
|
79
79
|
|
80
|
+
## Rails 3
|
81
|
+
|
82
|
+
In your controller:
|
83
|
+
|
84
|
+
render 'index', :locals => {:teas => teas}, :layout => false
|
85
|
+
|
86
|
+
Support for partials is non-standard at this time: create a subdirectory in the directory that your template lives in and call it `partials`.
|
87
|
+
|
88
|
+
|
80
89
|
## Caveat
|
81
90
|
|
82
|
-
|
91
|
+
There is currently no support for instance variables in Sinatra and Rails 3.
|
92
|
+
|
93
|
+
Avoid using `post` as a local variable name in sinatra. Since the template context in sinatra is the controller, calling `post` from your template will call the http `post` action. Same goes for `get`, `head`, `put`, etc, but these are less likely to be resources in your application.
|
83
94
|
|
84
95
|
## Related Projects
|
85
96
|
|
data/lib/petroglyph.rb
CHANGED
@@ -2,9 +2,13 @@ require 'json'
|
|
2
2
|
|
3
3
|
require 'petroglyph/scope'
|
4
4
|
require 'petroglyph/engine'
|
5
|
+
require 'petroglyph/railtie' if defined?(Rails) && Rails.version =~ /^3/
|
5
6
|
|
6
7
|
module Petroglyph
|
7
8
|
class << self
|
9
|
+
def register!
|
10
|
+
require 'petroglyph/template'
|
11
|
+
end
|
8
12
|
def partial(filename, template_filename)
|
9
13
|
basedir = File.dirname(template_filename)
|
10
14
|
[basedir, "#{basedir}/partials"].each do |dir|
|
@@ -15,3 +19,10 @@ module Petroglyph
|
|
15
19
|
end
|
16
20
|
end
|
17
21
|
end
|
22
|
+
|
23
|
+
if defined?(Padrino)
|
24
|
+
require 'padrino-core'
|
25
|
+
Padrino.after_load { Petroglyph.register! }
|
26
|
+
elsif defined?(Rails) && Rails.version =~ /^2/
|
27
|
+
Petroglyph.register!
|
28
|
+
end
|
data/lib/petroglyph/engine.rb
CHANGED
@@ -6,13 +6,16 @@ module Petroglyph
|
|
6
6
|
end
|
7
7
|
|
8
8
|
def render(context = Object.new, locals = {}, file = nil, &block)
|
9
|
+
to_hash(locals, file, context, &block).to_json
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_hash(locals = {}, file = nil, context = Object.new, &block)
|
9
13
|
scope = Scope.new(context, locals, file)
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
scope.value.to_json
|
14
|
+
|
15
|
+
scope.instance_eval(@data) if @data
|
16
|
+
scope.instance_eval(&block) if block_given?
|
17
|
+
|
18
|
+
scope.value
|
16
19
|
end
|
17
20
|
end
|
18
21
|
end
|
data/lib/petroglyph/scope.rb
CHANGED
@@ -5,6 +5,7 @@ module Petroglyph
|
|
5
5
|
def initialize(context = nil, locals = {}, template_filename = nil, parent_scope = nil)
|
6
6
|
@file = template_filename
|
7
7
|
@context = context
|
8
|
+
self.copy_instance_variables_from(@context, [:@assigns, :@helpers]) if self.respond_to?(:copy_instance_variables_from)
|
8
9
|
@locals = locals
|
9
10
|
@parent_scope = parent_scope
|
10
11
|
@value = nil
|
@@ -81,7 +82,7 @@ module Petroglyph
|
|
81
82
|
end
|
82
83
|
|
83
84
|
def method_missing(method, *args, &block)
|
84
|
-
if @locals.has_key?(method)
|
85
|
+
if @locals and @locals.has_key?(method)
|
85
86
|
@locals[method]
|
86
87
|
elsif @context.respond_to?(method)
|
87
88
|
@context.send(method)
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# TILT Template
|
2
|
+
if defined?(Tilt)
|
3
|
+
class PetroglyphTemplate < Tilt::Template
|
4
|
+
def initialize_engine
|
5
|
+
return if defined?(::Petroglyph)
|
6
|
+
require_template_library 'petroglyph'
|
7
|
+
end
|
8
|
+
|
9
|
+
def prepare
|
10
|
+
options = @options.merge(:source_location => file)
|
11
|
+
@engine = ::Petroglyph::Engine.new(data, options)
|
12
|
+
end
|
13
|
+
|
14
|
+
def evaluate(scope, locals, &block)
|
15
|
+
@engine.render(scope, locals, &block)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
Tilt.register 'pg', PetroglyphTemplate
|
20
|
+
end
|
21
|
+
|
22
|
+
# Rails 2.X Template
|
23
|
+
if defined?(Rails) && Rails.version =~ /^2/
|
24
|
+
require 'action_view/base'
|
25
|
+
require 'action_view/template'
|
26
|
+
|
27
|
+
module ActionView
|
28
|
+
module TemplateHandlers
|
29
|
+
class PetroglyphHandler < TemplateHandler
|
30
|
+
include Compilable
|
31
|
+
|
32
|
+
def compile(template) %{
|
33
|
+
::Petroglyph::Engine.new(#{template.source.inspect}).
|
34
|
+
render(self, assigns.merge(local_assigns), '#{template.filename}')
|
35
|
+
} end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
ActionView::Template.register_template_handler :pg, ActionView::TemplateHandlers::PetroglyphHandler
|
41
|
+
end
|
42
|
+
|
43
|
+
# Rails 3.X Template
|
44
|
+
if defined?(Rails) && Rails.version =~ /^3/
|
45
|
+
module ActionView
|
46
|
+
module Template::Handlers
|
47
|
+
class Petroglyph
|
48
|
+
|
49
|
+
def call(template)
|
50
|
+
source = if template.source.empty?
|
51
|
+
File.read(template.identifier)
|
52
|
+
else
|
53
|
+
template.source
|
54
|
+
end
|
55
|
+
|
56
|
+
%{ ::Petroglyph::Engine.new(#{source.inspect}).
|
57
|
+
render(self, assigns.merge(local_assigns), '#{template.identifier}') }
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
ActionView::Template.register_template_handler :pg, ActionView::Template::Handlers::Petroglyph.new
|
64
|
+
end
|
data/lib/petroglyph/version.rb
CHANGED
data/spec/engine_spec.rb
CHANGED
@@ -1,6 +1,11 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Petroglyph::Engine do
|
4
|
+
specify "#to_hash" do
|
5
|
+
engine = Petroglyph::Engine.new('node :whatever => thing')
|
6
|
+
engine.to_hash({:thing => 'stuff'}).should eq({:whatever => 'stuff'})
|
7
|
+
end
|
8
|
+
|
4
9
|
it "takes a template" do
|
5
10
|
engine = Petroglyph::Engine.new('node :whatever => "nevermind"')
|
6
11
|
engine.render.should eq({:whatever => "nevermind"}.to_json)
|
@@ -30,8 +30,12 @@ describe "Sinatra integration" do
|
|
30
30
|
last_response.body.should eq '{"drinks":[{"type":"tea","temperature":"hot"},{"type":"coffee","temperature":"lukewarm"}]}'
|
31
31
|
end
|
32
32
|
|
33
|
-
|
34
|
-
|
35
|
-
|
33
|
+
it "overshadows the controller methods" do
|
34
|
+
# look at /views/fixtures/post.pg
|
35
|
+
# and line 18 of this file
|
36
|
+
pending "right now if you refer to a local var 'post' in the template, you will accidentally call sinatra's post method" do
|
37
|
+
post '/'
|
38
|
+
last_response.body.should eq "{\"post\":\"a post\"}"
|
39
|
+
end
|
36
40
|
end
|
37
41
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: petroglyph
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
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:
|
12
|
+
date: 2012-03-18 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: tilt
|
16
|
-
requirement: &
|
16
|
+
requirement: &70204971169420 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70204971169420
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &70204971169000 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70204971169000
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: sinatra
|
38
|
-
requirement: &
|
38
|
+
requirement: &70204971168580 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70204971168580
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rack-test
|
49
|
-
requirement: &
|
49
|
+
requirement: &70204971168160 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,7 +54,7 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70204971168160
|
58
58
|
description: A simple, terse, and unsurprising ruby dsl to create json views.
|
59
59
|
email:
|
60
60
|
- katrina.owen@gmail.com
|
@@ -70,7 +70,9 @@ files:
|
|
70
70
|
- Rakefile
|
71
71
|
- lib/petroglyph.rb
|
72
72
|
- lib/petroglyph/engine.rb
|
73
|
+
- lib/petroglyph/railtie.rb
|
73
74
|
- lib/petroglyph/scope.rb
|
75
|
+
- lib/petroglyph/template.rb
|
74
76
|
- lib/petroglyph/version.rb
|
75
77
|
- lib/sinatra/petroglyph.rb
|
76
78
|
- lib/tilt/petroglyph.rb
|