bldr 0.5.3 → 0.5.4

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml CHANGED
@@ -1,6 +1,5 @@
1
1
  rvm:
2
- - 1.8.7
3
- - 1.9.2
2
+ - 1.9.3
4
3
  - ree
5
4
  - ruby-head
6
5
  - rbx
data/HISTORY.md CHANGED
@@ -1,5 +1,8 @@
1
- ## 0.5.3
1
+ ## 0.5.4 (2012-04-24)
2
+ * Fix bug to allow using `template` method at the root of a bldr template
3
+ * Add `locals` reader method to allow access to locals passed into a bldr template
2
4
 
5
+ ## 0.5.3
3
6
  * Add ability to use `attribute` method at the root-level in a bldr template
4
7
  * Fix for when partials return nil (#19)
5
8
 
@@ -10,5 +13,4 @@
10
13
  * Add new `attribute` inferred object syntax (@ihunter)
11
14
 
12
15
  ## 0.1.2 (2011-09-08)
13
-
14
16
  * Return an empty collection when a nil value is passed to `collection` method
data/bldr.gemspec CHANGED
@@ -19,7 +19,7 @@ Gem::Specification.new do |s|
19
19
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
20
  s.require_paths = ["lib"]
21
21
 
22
- s.add_dependency 'multi_json', '~> 1.0.3'
22
+ s.add_dependency 'multi_json'
23
23
 
24
24
  s.add_development_dependency 'json_pure'
25
25
  s.add_development_dependency 'sinatra', '~>1.2.6'
data/lib/bldr.rb CHANGED
@@ -1,5 +1,3 @@
1
- $:.unshift(File.dirname(File.expand_path(__FILE__)))
2
-
3
1
  begin
4
2
  require 'tilt'
5
3
  rescue LoadError
data/lib/bldr/node.rb CHANGED
@@ -3,7 +3,7 @@ module Bldr
3
3
 
4
4
  class Node
5
5
 
6
- attr_reader :current_object, :result, :parent, :opts, :views
6
+ attr_reader :current_object, :result, :parent, :opts, :views, :locals
7
7
 
8
8
  # Initialize a new Node instance.
9
9
  #
@@ -22,6 +22,7 @@ module Bldr
22
22
  @opts = opts
23
23
  @parent = opts[:parent]
24
24
  @views = opts[:views]
25
+ @locals = opts[:locals]
25
26
  # Storage hash for all descendant nodes
26
27
  @result = {}
27
28
 
@@ -60,8 +61,7 @@ module Bldr
60
61
  # and the object to serialize.
61
62
  # @param [Proc] block the code block to evaluate
62
63
  #
63
- # @return [String] returns a json-encoded string of itself and all
64
- # descendant nodes.
64
+ # @return [Bldr::Node] returns self
65
65
  def object(base = nil, &block)
66
66
  if base.kind_of? Hash
67
67
  key = base.keys.first
@@ -78,6 +78,7 @@ module Bldr
78
78
  self
79
79
  end
80
80
 
81
+ # @return [Bldr::Node] returns self
81
82
  def collection(items, &block)
82
83
 
83
84
  if items.respond_to?('keys')
@@ -185,12 +186,16 @@ module Bldr
185
186
  # object :person => dude do
186
187
  # template "path/to/template", :locals => {:foo => 'bar'}
187
188
  # end
188
- def template(template,options={})
189
+ #
190
+ # @return [Bldr::Node] returns self
191
+ def template(template, options={})
189
192
  locals = options[:locals] || options['locals']
190
193
 
191
194
  if tpl = Bldr::Template.new(find_template(template)).render(self, locals)
192
195
  merge_result! nil, tpl.result
193
196
  end
197
+
198
+ self
194
199
  end
195
200
 
196
201
  private
data/lib/bldr/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
 
2
2
  module Bldr
3
- VERSION = '0.5.3'
3
+ VERSION = '0.5.4'
4
4
  end
data/lib/sinatra/bldr.rb CHANGED
@@ -23,10 +23,9 @@ module Sinatra
23
23
  #
24
24
  # @param [Hash] opts a hash of options
25
25
  # @option opts [Hash] :locals a hash of local variables to be used in the template
26
- # @option
27
26
  def bldr(template, opts = {}, &block)
28
- opts[:scope] = ::Bldr::Node.new(nil,opts.merge(:views => (settings.views || "./views")))
29
- locals = opts.delete(:locals)
27
+ opts[:scope] = ::Bldr::Node.new(nil, opts.merge(:views => (settings.views || "./views")))
28
+ locals = opts.delete(:locals)
30
29
  MultiJson.encode render(:bldr, template, opts, locals, &block).result
31
30
  # @todo add support for alternate formats, like plist
32
31
  end
@@ -0,0 +1 @@
1
+ template 'fixtures/partial.json.bldr'
@@ -147,3 +147,10 @@ describe "evaluating a tilt template" do
147
147
 
148
148
  end
149
149
  end
150
+
151
+ describe "using a partial template at the root of another template" do
152
+ it "works as expected" do
153
+ template = Bldr::Template.new('./spec/fixtures/root_partial.bldr')
154
+ template.render(Bldr::Node.new(nil, :views => './spec')).result.should == {:foo => 'bar'}
155
+ end
156
+ end
@@ -8,6 +8,8 @@ describe "Using Bldr with a sinatra app" do
8
8
  register Sinatra::Bldr
9
9
 
10
10
  set :views, File.expand_path(__FILE__ + '/../..')
11
+ disable :show_exceptions
12
+ enable :raise_errors
11
13
 
12
14
  get '/' do
13
15
  alex = Person.new("alex", 25)
@@ -49,6 +51,15 @@ describe "Using Bldr with a sinatra app" do
49
51
 
50
52
  bldr :'fixtures/root_template.json', :locals => {:name => name, :age => age}
51
53
  end
54
+
55
+ get '/root_partial' do
56
+ bldr :'fixtures/root_partial'
57
+ end
58
+ end
59
+
60
+ it "properly renders a template that only contains a template call" do
61
+ response = Rack::MockRequest.new(TestApp).get('/root_partial')
62
+ MultiJson.decode(response.body).should == {'foo' => 'bar'}
52
63
  end
53
64
 
54
65
  it "returns json for a simple single-level template" do
@@ -86,3 +97,25 @@ describe "Using Bldr with a sinatra app" do
86
97
  parse_json(response.body).should == {'name' => 'john doe', 'age' => 26}
87
98
  end
88
99
  end
100
+
101
+ describe "access to the locals hash inside sinatra bldr templates" do
102
+ class Locals < BaseTestApp
103
+ disable :show_exceptions
104
+ enable :raise_errors
105
+
106
+ get '/' do
107
+ tpl = <<-RUBY
108
+ object(:locals) do
109
+ attribute(:key) { locals[:key] }
110
+ end
111
+ RUBY
112
+
113
+ bldr tpl, :locals => {:key => 'val'}
114
+ end
115
+ end
116
+
117
+ it "provides access to the locals hash in the template" do
118
+ response = Rack::MockRequest.new(Locals).get('/')
119
+ MultiJson.decode(response.body)['locals']['key'].should == 'val'
120
+ end
121
+ end
data/spec/spec_helper.rb CHANGED
@@ -3,6 +3,7 @@ require 'rspec'
3
3
 
4
4
  require 'yajl'
5
5
  require 'tilt'
6
+ require 'sinatra/base'
6
7
 
7
8
  $:.unshift(File.dirname(File.expand_path(__FILE__)))
8
9
 
@@ -10,6 +11,16 @@ require File.join(File.dirname(File.expand_path(__FILE__)), "..", "lib", "bldr")
10
11
 
11
12
  Dir['spec/models/*'].each { |f| require File.expand_path(f) }
12
13
 
14
+ require 'sinatra/bldr'
15
+
16
+ class BaseTestApp < Sinatra::Base
17
+ register Sinatra::Bldr
18
+
19
+ set :views, File.expand_path(__FILE__ + '/../..')
20
+ disable :show_exceptions
21
+ enable :raise_errors
22
+ end
23
+
13
24
  RSpec.configure do |c|
14
25
  def node_wrap(*args, &block)
15
26
  Bldr::Node.new(*args, &block)
@@ -35,6 +46,4 @@ RSpec.configure do |c|
35
46
  c.after do
36
47
  Bldr.handlers.clear
37
48
  end
38
-
39
-
40
49
  end
@@ -2,17 +2,17 @@ require 'spec_helper'
2
2
 
3
3
  describe "the json encoding library" do
4
4
  it "uses yajl by default" do
5
- MultiJson.engine.should == MultiJson::Engines::Yajl
5
+ MultiJson.engine.should == MultiJson::Adapters::Yajl
6
6
  end
7
7
 
8
8
  it "allows changing the json encoder to json pure" do
9
9
  Bldr.json_encoder = :json_pure
10
- MultiJson.engine.should == MultiJson::Engines::JsonPure
10
+ MultiJson.engine.should == MultiJson::Adapters::JsonPure
11
11
  end
12
12
 
13
13
  it "allows changing the json encoder to the json gem" do
14
14
  Bldr.json_encoder = :json_gem
15
- MultiJson.engine.should == MultiJson::Engines::JsonGem
15
+ MultiJson.engine.should == MultiJson::Adapters::JsonGem
16
16
  end
17
17
 
18
18
  end
@@ -622,4 +622,11 @@ describe "Node#template" do
622
622
  end
623
623
  end
624
624
  end
625
+ end
626
+
627
+ describe "Node#locals" do
628
+ let(:node) { Bldr::Node.new({:foo => 'bar'}, :locals => {:key => 'val'})}
629
+ subject { node.locals }
630
+
631
+ it { should == {:key => 'val'} }
625
632
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bldr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.3
4
+ version: 0.5.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,22 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-22 00:00:00.000000000 Z
12
+ date: 2012-04-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: multi_json
16
- requirement: &70338454067680 !ruby/object:Gem::Requirement
16
+ requirement: &70282606563340 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
- - - ~>
19
+ - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
- version: 1.0.3
21
+ version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70338454067680
24
+ version_requirements: *70282606563340
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: json_pure
27
- requirement: &70338454067260 !ruby/object:Gem::Requirement
27
+ requirement: &70282606562140 !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: *70338454067260
35
+ version_requirements: *70282606562140
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: sinatra
38
- requirement: &70338454066640 !ruby/object:Gem::Requirement
38
+ requirement: &70282606560360 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 1.2.6
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70338454066640
46
+ version_requirements: *70282606560360
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: tilt
49
- requirement: &70338454065940 !ruby/object:Gem::Requirement
49
+ requirement: &70282606558200 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 1.3.2
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70338454065940
57
+ version_requirements: *70282606558200
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: yajl-ruby
60
- requirement: &70338454065240 !ruby/object:Gem::Requirement
60
+ requirement: &70282606557500 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '1.0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70338454065240
68
+ version_requirements: *70282606557500
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: actionpack
71
- requirement: &70338454080880 !ruby/object:Gem::Requirement
71
+ requirement: &70282606573160 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ~>
@@ -76,7 +76,7 @@ dependencies:
76
76
  version: 3.0.7
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *70338454080880
79
+ version_requirements: *70282606573160
80
80
  description: Provides a simple and intuitive templating DSL for serializing objects
81
81
  to JSON.
82
82
  email:
@@ -105,6 +105,7 @@ files:
105
105
  - spec/fixtures/nested_objects.json.bldr
106
106
  - spec/fixtures/partial.json.bldr
107
107
  - spec/fixtures/partial_with_locals.json.bldr
108
+ - spec/fixtures/root_partial.bldr
108
109
  - spec/fixtures/root_template.json.bldr
109
110
  - spec/fixtures/some/include.json.bldr
110
111
  - spec/functional/handlers_spec.rb
@@ -144,6 +145,7 @@ test_files:
144
145
  - spec/fixtures/nested_objects.json.bldr
145
146
  - spec/fixtures/partial.json.bldr
146
147
  - spec/fixtures/partial_with_locals.json.bldr
148
+ - spec/fixtures/root_partial.bldr
147
149
  - spec/fixtures/root_template.json.bldr
148
150
  - spec/fixtures/some/include.json.bldr
149
151
  - spec/functional/handlers_spec.rb