petroglyph 0.0.6 → 0.0.7
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/.rspec +1 -1
- data/.travis.yml +1 -0
- data/LICENSE +22 -0
- data/README.md +38 -1
- data/Rakefile +6 -0
- data/lib/petroglyph.rb +11 -4
- data/lib/petroglyph/scope.rb +10 -5
- data/lib/petroglyph/version.rb +1 -1
- data/lib/tilt/petroglyph.rb +4 -5
- data/spec/scope_spec.rb +16 -3
- metadata +32 -12
data/.rspec
CHANGED
@@ -1 +1 @@
|
|
1
|
-
--color
|
1
|
+
--color
|
data/.travis.yml
CHANGED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Katrina Owen
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -50,6 +50,14 @@ Conveniently define which attributes to include. Create a new node with a differ
|
|
50
50
|
end
|
51
51
|
=> '{"person":{"name":"Alice","gender":"female","job":"surgeon"}}'
|
52
52
|
|
53
|
+
If you don't want to namespace the object, you can use `merge` in place of `node`:
|
54
|
+
|
55
|
+
merge alice do
|
56
|
+
attributes :name, :gender
|
57
|
+
node :job => alice.profession
|
58
|
+
end
|
59
|
+
=> '{"name":"Alice","gender":"female","job":"surgeon"}'
|
60
|
+
|
53
61
|
Iterate through collections:
|
54
62
|
|
55
63
|
wulong = Tea.new(:type => 'wulong')
|
@@ -73,7 +81,11 @@ You can also explicitly reference each item in the collection if you need to:
|
|
73
81
|
|
74
82
|
Partials have been implemented. This defaults to looking for a file in the same file as the template, or in a subdirectory called `partials`.
|
75
83
|
|
76
|
-
|
84
|
+
There is a shortcut if you're looping over a collection to render a partial for each item in the collection:
|
85
|
+
|
86
|
+
collection :teas => teas, :partial => :tea
|
87
|
+
|
88
|
+
The name of the partials subdirectory can be overridden by re-implementing the `Petroglyph.partial(name)` method.
|
77
89
|
|
78
90
|
collection :teas => teas do |tea|
|
79
91
|
# partial(template_name, local_variables)
|
@@ -92,6 +104,24 @@ Support for partials is non-standard at this time: create a subdirectory in the
|
|
92
104
|
|
93
105
|
This works with version 1.3 of Sinatra. It may work with earlier versions.
|
94
106
|
|
107
|
+
### Config
|
108
|
+
|
109
|
+
You need to require the Sinatra petroglyph extension:
|
110
|
+
|
111
|
+
```ruby
|
112
|
+
require 'sinatra/petroglyph'
|
113
|
+
```
|
114
|
+
|
115
|
+
Then you can use the `pg` helper method in your endpoints:
|
116
|
+
|
117
|
+
```ruby
|
118
|
+
get '/api/v1/widgets' do
|
119
|
+
pg :widgets, :locals => {:widgets => Widget.all}
|
120
|
+
end
|
121
|
+
```
|
122
|
+
|
123
|
+
### Gotcha
|
124
|
+
|
95
125
|
There is a known incompatibility in Sinatra versions prior to 1.3 where a local variable named `post` will crash with Sinatra's HTTP `post` action.
|
96
126
|
The same goes for `get`, `head`, `put`, etc, but these are less likely to be resources in your application.
|
97
127
|
|
@@ -112,3 +142,10 @@ Other json templating libraries exist, some of which also generate XML.
|
|
112
142
|
* [Jsonify](https://github.com/bsiggelkow/jsonify)
|
113
143
|
* [Representative](https://github.com/mdub/representative)
|
114
144
|
* [Tokamak](https://github.com/abril/tokamak)
|
145
|
+
|
146
|
+
## License
|
147
|
+
|
148
|
+
MIT License
|
149
|
+
|
150
|
+
Copyright (c) 2012 Katrina Owen
|
151
|
+
|
data/Rakefile
CHANGED
data/lib/petroglyph.rb
CHANGED
@@ -6,13 +6,20 @@ require 'petroglyph/engine'
|
|
6
6
|
module Petroglyph
|
7
7
|
class << self
|
8
8
|
def partial(filename, template_filename)
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
9
|
+
paths(filename, template_filename).each do |path|
|
10
|
+
if File.exist?(path)
|
11
|
+
return eval "Proc.new{#{File.read(path)}}"
|
12
|
+
end
|
13
13
|
end
|
14
14
|
raise Exception.new("Could not find partial #{filename}")
|
15
15
|
end
|
16
|
+
|
17
|
+
def paths(filename, template_filename)
|
18
|
+
basedir = File.dirname(template_filename)
|
19
|
+
[basedir, "#{basedir}/partials"].map do |dir|
|
20
|
+
File.join(dir, "#{filename}.pg")
|
21
|
+
end
|
22
|
+
end
|
16
23
|
end
|
17
24
|
end
|
18
25
|
|
data/lib/petroglyph/scope.rb
CHANGED
@@ -39,10 +39,15 @@ module Petroglyph
|
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
|
-
def collection(
|
42
|
+
def collection(args, &block)
|
43
43
|
@value ||= {}
|
44
|
-
name =
|
45
|
-
|
44
|
+
name, items = args.first
|
45
|
+
|
46
|
+
if args.length == 2
|
47
|
+
singular = args[:partial]
|
48
|
+
block = eval "Proc.new{|item| partial #{singular.inspect}, #{singular.inspect} => item}"
|
49
|
+
end
|
50
|
+
|
46
51
|
results = []
|
47
52
|
items.each do |item|
|
48
53
|
scope = sub_scope(item)
|
@@ -77,9 +82,9 @@ module Petroglyph
|
|
77
82
|
def partial(name, locals = nil)
|
78
83
|
locals ||= {name => send(name)} if respond_to?(name)
|
79
84
|
locals ||= {}
|
80
|
-
|
85
|
+
partial = Petroglyph.partial(name, file)
|
81
86
|
scope = Scope.new(@context, locals, file)
|
82
|
-
scope.instance_eval(
|
87
|
+
scope.instance_eval(&partial)
|
83
88
|
merge scope.value
|
84
89
|
end
|
85
90
|
|
data/lib/petroglyph/version.rb
CHANGED
data/lib/tilt/petroglyph.rb
CHANGED
@@ -1,7 +1,4 @@
|
|
1
1
|
# Makes Petroglyph available through Tilt.
|
2
|
-
# Also contains a utility function that will
|
3
|
-
# be added to Sinatra if Sinatra is defined.
|
4
|
-
|
5
2
|
require 'tilt'
|
6
3
|
require 'petroglyph'
|
7
4
|
|
@@ -13,14 +10,16 @@ module Tilt
|
|
13
10
|
require_template_library 'petroglyph'
|
14
11
|
end
|
15
12
|
|
16
|
-
def prepare
|
13
|
+
def prepare
|
14
|
+
@engine = Petroglyph::Engine.new(data)
|
15
|
+
end
|
17
16
|
|
18
17
|
def precompiled_template(locals)
|
19
18
|
data.to_str
|
20
19
|
end
|
21
20
|
|
22
21
|
def evaluate(scope = Object.new, locals = {}, &block)
|
23
|
-
|
22
|
+
@engine.render(scope, locals, file, &block)
|
24
23
|
end
|
25
24
|
end
|
26
25
|
register PetroglyphTemplate, 'pg'
|
data/spec/scope_spec.rb
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
+
def fake_partial(s)
|
4
|
+
eval "Proc.new{#{s}}"
|
5
|
+
end
|
6
|
+
|
3
7
|
describe Petroglyph::Scope do
|
4
8
|
|
5
9
|
it 'takes a simple string value' do
|
@@ -219,11 +223,20 @@ describe Petroglyph::Scope do
|
|
219
223
|
|
220
224
|
scope.value.should eq({:beverages => []})
|
221
225
|
end
|
226
|
+
|
227
|
+
it 'has a convenience handler' do
|
228
|
+
Petroglyph.stub(:partial) { fake_partial('node :drink => drink.type') }
|
229
|
+
|
230
|
+
scope = Petroglyph::Scope.new
|
231
|
+
scope.collection :drinks => drinks, :partial => :drink
|
232
|
+
|
233
|
+
scope.value.should eq({:drinks => [{:drink => 'tea'}, {:drink => 'coffee'}]})
|
234
|
+
end
|
222
235
|
end
|
223
236
|
|
224
237
|
context 'with partials' do
|
225
238
|
it 'renders a partial' do
|
226
|
-
Petroglyph.stub(:partial) { 'node :drink => "tea"' }
|
239
|
+
Petroglyph.stub(:partial) { fake_partial('node :drink => "tea"') }
|
227
240
|
|
228
241
|
scope = Petroglyph::Scope.new
|
229
242
|
scope.node :partial do
|
@@ -234,7 +247,7 @@ describe Petroglyph::Scope do
|
|
234
247
|
end
|
235
248
|
|
236
249
|
it 'renders a partial with local variables' do
|
237
|
-
Petroglyph.stub(:partial) { 'node :drink => drink' }
|
250
|
+
Petroglyph.stub(:partial) { fake_partial('node :drink => drink') }
|
238
251
|
|
239
252
|
scope = Petroglyph::Scope.new
|
240
253
|
scope.node :partial do
|
@@ -245,7 +258,7 @@ describe Petroglyph::Scope do
|
|
245
258
|
end
|
246
259
|
|
247
260
|
it 'defaults locals to match the name of the partial' do
|
248
|
-
Petroglyph.stub(:partial) { 'node :beverage => drink' }
|
261
|
+
Petroglyph.stub(:partial) { fake_partial('node :beverage => drink') }
|
249
262
|
|
250
263
|
scope = Petroglyph::Scope.new(nil, {:drink => 'coffee'})
|
251
264
|
scope.node :beverages do
|
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.7
|
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: 2013-
|
12
|
+
date: 2013-08-07 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: rspec
|
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: :development
|
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: sinatra
|
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: '1.3'
|
44
54
|
type: :development
|
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: '1.3'
|
47
62
|
- !ruby/object:Gem::Dependency
|
48
63
|
name: rack-test
|
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: :development
|
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: A simple, terse, and unsurprising ruby dsl to create json views.
|
59
79
|
email:
|
60
80
|
- katrina.owen@gmail.com
|
@@ -67,6 +87,7 @@ files:
|
|
67
87
|
- .simplecov
|
68
88
|
- .travis.yml
|
69
89
|
- Gemfile
|
90
|
+
- LICENSE
|
70
91
|
- README.md
|
71
92
|
- Rakefile
|
72
93
|
- lib/padrino/petroglyph.rb
|
@@ -114,9 +135,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
114
135
|
version: '0'
|
115
136
|
requirements: []
|
116
137
|
rubyforge_project: petroglyph
|
117
|
-
rubygems_version: 1.8.
|
138
|
+
rubygems_version: 1.8.23
|
118
139
|
signing_key:
|
119
140
|
specification_version: 3
|
120
141
|
summary: A simple, terse, and unsurprising ruby dsl to create json views.
|
121
142
|
test_files: []
|
122
|
-
has_rdoc:
|