bldr 0.5.2 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
data/HISTORY.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 0.5.3
2
+
3
+ * Add ability to use `attribute` method at the root-level in a bldr template
4
+ * Fix for when partials return nil (#19)
5
+
1
6
  ## 0.5.0 (2012-02-08)
2
7
  * Add support "partials" (@ihunter)
3
8
 
data/lib/bldr/node.rb CHANGED
@@ -156,6 +156,7 @@ module Bldr
156
156
  raise(ArgumentError, "You may only pass one argument to #attribute when using the block syntax.") if args.size > 1
157
157
  raise(ArgumentError, "You cannot use a block of arity > 0 if current_object is not present.") if block.arity > 0 and current_object.nil?
158
158
  merge_result!(args.first, (block.arity == 1) ? block.call(current_object) : current_object.instance_eval(&block))
159
+ self
159
160
  else
160
161
  case args.size
161
162
  when 1 # inferred object
@@ -186,7 +187,10 @@ module Bldr
186
187
  # end
187
188
  def template(template,options={})
188
189
  locals = options[:locals] || options['locals']
189
- merge_result! nil, Bldr::Template.new(find_template(template)).render(self, locals).result
190
+
191
+ if tpl = Bldr::Template.new(find_template(template)).render(self, locals)
192
+ merge_result! nil, tpl.result
193
+ end
190
194
  end
191
195
 
192
196
  private
data/lib/bldr/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
 
2
2
  module Bldr
3
- VERSION = '0.5.2'
3
+ VERSION = '0.5.3'
4
4
  end
@@ -13,6 +13,15 @@ describe "evaluating a tilt template" do
13
13
  tpl.render(Bldr::Node.new, :alex => alex).result.should == {:person => {:name => 'alex'}}
14
14
  end
15
15
 
16
+ it "allows attribute to be used at the root-level" do
17
+ tpl = Bldr::Template.new {
18
+ <<-RUBY
19
+ attribute(:foo) { "bar" }
20
+ RUBY
21
+ }
22
+ tpl.render(Bldr::Node.new(nil)).result.should == {:foo => 'bar'}
23
+ end
24
+
16
25
  it "works when render two top-level objects" do
17
26
  alex = Person.new('alex')
18
27
  john = Person.new('john')
@@ -17,7 +17,6 @@ describe "Node#object" do
17
17
  end
18
18
 
19
19
  describe "#attribute" do
20
-
21
20
  it "errors on a single argument" do
22
21
  expect {
23
22
  node_wrap {
@@ -27,6 +26,7 @@ describe "Node#object" do
27
26
  }
28
27
  }.to raise_error(ArgumentError, ERROR_MESSAGES[:attribute_lambda_one_argument])
29
28
  end
29
+
30
30
  it "errors on 3 arguments" do
31
31
  expect {
32
32
  node_wrap {
@@ -34,6 +34,7 @@ describe "Node#object" do
34
34
  }
35
35
  }.to raise_error(ArgumentError, ERROR_MESSAGES[:attribute_more_than_two_arg])
36
36
  end
37
+
37
38
  it "errors on 2 arguments and a lambda" do
38
39
  expect {
39
40
  node_wrap {
@@ -43,6 +44,7 @@ describe "Node#object" do
43
44
  }
44
45
  }.to raise_error(ArgumentError, ERROR_MESSAGES[:attribute_lambda_one_argument])
45
46
  end
47
+
46
48
  it "errors on 1 argument since there is no inferred object" do
47
49
  expect {
48
50
  node_wrap {
@@ -50,10 +52,12 @@ describe "Node#object" do
50
52
  }
51
53
  }.to raise_error(ArgumentError, ERROR_MESSAGES[:attribute_inferred_missing_one_argument])
52
54
  end
55
+
53
56
  it "renders 2 arguments statically" do
54
57
  node = wrap { attribute(:name, "alex") }
55
58
  node.result.should == {:name => 'alex'}
56
59
  end
60
+
57
61
  it "renders 1 argument and one lambda with zero arity" do
58
62
  node = wrap {
59
63
  attribute(:name) do
@@ -62,6 +66,7 @@ describe "Node#object" do
62
66
  }
63
67
  node.result.should == {:name => 'alex'}
64
68
  end
69
+
65
70
  it "errors on 1 argument and one lambda with arity 1" do
66
71
  expect {
67
72
  node_wrap {
@@ -71,15 +76,14 @@ describe "Node#object" do
71
76
  }
72
77
  }.to raise_error(ArgumentError, ERROR_MESSAGES[:attribute_inferred_missing_arity_too_large])
73
78
  end
79
+
74
80
  it "should render null attributes to null, not 'null'" do
75
81
  node = wrap { attribute(:name, nil) }
76
82
  node.result.should == {:name => nil}
77
83
  end
78
-
79
84
  end
80
85
 
81
86
  describe "#attributes" do
82
-
83
87
  it "errors since current_object is nil" do
84
88
  expect {
85
89
  node_wrap {
@@ -87,9 +91,7 @@ describe "Node#object" do
87
91
  }
88
92
  }.to raise_error(ArgumentError, ERROR_MESSAGES[:attributes_inferred_missing])
89
93
  end
90
-
91
94
  end
92
-
93
95
  end
94
96
 
95
97
  context "a single arg root object node" do
@@ -181,6 +183,11 @@ describe "Node#object" do
181
183
  end
182
184
 
183
185
  describe "#attribute" do
186
+ it "works at the root level" do
187
+ node_wrap {
188
+ attribute(:foo) { "bar" }
189
+ }.result.should == {:foo => 'bar'}
190
+ end
184
191
 
185
192
  it "errors on 3 arguments" do
186
193
  expect {
@@ -520,7 +527,7 @@ describe "Node#collection" do
520
527
 
521
528
  end
522
529
 
523
- describe "Node#partial" do
530
+ describe "Node#template" do
524
531
  it "includes the partial as a top level" do
525
532
  nodes = node_wrap do
526
533
  template "spec/fixtures/partial.json.bldr"
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.2
4
+ version: 0.5.3
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-02-21 00:00:00.000000000 Z
12
+ date: 2012-02-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: multi_json
16
- requirement: &70126171949220 !ruby/object:Gem::Requirement
16
+ requirement: &70338454067680 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 1.0.3
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70126171949220
24
+ version_requirements: *70338454067680
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: json_pure
27
- requirement: &70126171948780 !ruby/object:Gem::Requirement
27
+ requirement: &70338454067260 !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: *70126171948780
35
+ version_requirements: *70338454067260
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: sinatra
38
- requirement: &70126171948200 !ruby/object:Gem::Requirement
38
+ requirement: &70338454066640 !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: *70126171948200
46
+ version_requirements: *70338454066640
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: tilt
49
- requirement: &70126171947660 !ruby/object:Gem::Requirement
49
+ requirement: &70338454065940 !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: *70126171947660
57
+ version_requirements: *70338454065940
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: yajl-ruby
60
- requirement: &70126171947160 !ruby/object:Gem::Requirement
60
+ requirement: &70338454065240 !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: *70126171947160
68
+ version_requirements: *70338454065240
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: actionpack
71
- requirement: &70126171946680 !ruby/object:Gem::Requirement
71
+ requirement: &70338454080880 !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: *70126171946680
79
+ version_requirements: *70338454080880
80
80
  description: Provides a simple and intuitive templating DSL for serializing objects
81
81
  to JSON.
82
82
  email: