rabl 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.md +80 -19
  2. data/lib/rabl/engine.rb +11 -1
  3. data/lib/rabl/version.rb +1 -1
  4. metadata +3 -3
data/README.md CHANGED
@@ -14,23 +14,53 @@ This general templating system solves all of those problems.
14
14
 
15
15
  ## Installation ##
16
16
 
17
+ Install as a gem:
18
+
17
19
  gem install rabl
18
20
 
21
+ or add to your Gemfile:
22
+
23
+ # Gemfile
24
+ gem 'rabl'
25
+
26
+ and run `bundle install` to install the dependency.
27
+
19
28
  ## Usage ##
20
29
 
30
+ ### Object Assignment ###
31
+
32
+ To declare the data object to use in the template:
33
+
34
+ # app/views/users/show.json.rabl
35
+ object @user
36
+
37
+ or a collection works:
38
+
39
+ object @users
40
+
41
+ and this will be used as the default data object for the rendering.
42
+
43
+ ### Attributes ###
44
+
21
45
  Basic usage of the templater:
22
46
 
23
47
  # app/views/users/show.json.rabl
24
48
  attributes :id, :foo, :bar
25
49
 
26
- This will generate a json response with the attributes specified. You can also include arbitrary code:
50
+ or with aliased attributes:
27
51
 
28
- # app/views/users/show.json.rabl
29
- code :full_name do |u|
30
- u.first_name + " " + u.last_name
31
- end
52
+ # Take the value of model attribute `foo` and name the node `bar`
53
+ # { bar : 5 }
54
+ attribute :foo => :bar
55
+
56
+ or multiple aliased attributes:
57
+
58
+ # { baz : <bar value>, animal : <dog value> }
59
+ attributes :bar => :baz, :dog => :animal
32
60
 
33
- You can also add children nodes:
61
+ ### Child Nodes ###
62
+
63
+ You can also add children nodes from an arbitrary object:
34
64
 
35
65
  child @posts => :foobar do
36
66
  attributes :id, :title
@@ -42,34 +72,65 @@ or use existing model associations:
42
72
  attributes :id, :title
43
73
  end
44
74
 
45
- You can also extend other rabl templates to reduce duplication:
75
+ ### Glued Attributes ###
76
+
77
+ You can also append attributes to the root node:
78
+
79
+ glue @post do
80
+ attributes :id => :post_id, :name => :post_name
81
+ end
82
+
83
+ Use glue to add additional attributes to the parent object.
84
+
85
+ ### Custom Nodes ###
86
+
87
+ This will generate a json response with the attributes specified. You can also include arbitrary code:
46
88
 
47
89
  # app/views/users/show.json.rabl
48
- child @address do
49
- extends "address/item"
90
+ code :full_name do |u|
91
+ u.first_name + " " + u.last_name
50
92
  end
51
93
 
52
- or get access to the hash representation of another object:
94
+ You can use custom "code" nodes to create flexible representations of a value utilizing data from the model.
95
+
96
+ ### Partials ###
97
+
98
+ Often you need to access sub-objects in order to construct your own custom nodes for more complex associations. You can get access to the hash representation of another object:
53
99
 
54
100
  code :location do
55
- { :place => partial("web/users/address", :object => @address) }
101
+ { :city => @city, :address => partial("web/users/address", :object => @address) }
56
102
  end
57
103
 
58
- You can also append attributes to the root node:
104
+ or an object associated to the parent model:
59
105
 
60
- glue @post do
61
- attribute :id => :post_id
106
+ code :location do |m|
107
+ { :city => m.city, :address => partial("web/users/address", :object => m.address) }
62
108
  end
63
109
 
64
- There is also the ability to extend other rabl templates with additional attributes:
110
+ You can use these to construct arbitrarily complex nodes for APIs.
111
+
112
+ ### Inheritance ###
113
+
114
+ Another common limitation of many json builders is code redundancy. Typically every representation of an object across endpoints share common attributes or nodes. The nodes for a 'post' object are probably the same or similar in most references throughout the various endpoints.
65
115
 
66
- extends "base"
116
+ RABL has the ability to extend other "base" rabl templates and additional attributes:
67
117
 
68
- code :release_year do |m|
69
- date = m.release_date || m.series_start
70
- date.try(:year)
118
+ # app/views/users/advanced.json.rabl
119
+ extends "users/base" # another RABL template in "app/views/users/base.json.rabl"
120
+
121
+ code :can_drink do |m|
122
+ m.age > 21
71
123
  end
72
124
 
125
+ You can also extend other rabl templates in constructing nodes to reduce duplication:
126
+
127
+ # app/views/users/show.json.rabl
128
+ child @address do
129
+ extends "address/item"
130
+ end
131
+
132
+ Using partials and inheritance can significantly reduce code duplication in your templates.
133
+
73
134
  ## Issues ##
74
135
 
75
136
  * I am sloppy and once again failed to unit test this. Don't use it in production until I do obviously.
data/lib/rabl/engine.rb CHANGED
@@ -6,7 +6,8 @@ module Rabl
6
6
  @_handler = handler
7
7
  @_options = { :handler => @_handler, :vars => @_vars, :engine => self }
8
8
  self.copy_instance_variables_from(@_handler, [:@assigns, :@helpers]);
9
- @_object = vars[:object] || instance_variable_get("@#{@_handler.controller.controller_name}")
9
+ @_object = vars[:object] || self.default_object
10
+ # raise @user.inspect + " - " + @_handler.instance_variable_get(:@options).inspect + " - " + @_handler.inspect
10
11
  instance_eval(source_string) if source_string.present?
11
12
  instance_eval(&block) if block_given?
12
13
  end
@@ -89,5 +90,14 @@ module Rabl
89
90
  return object unless object.is_a?(ActiveRecord::Base) || object.first.is_a?(ActiveRecord::Base)
90
91
  self.class.new(@_vars.merge(:object => object), @_handler, source, &block).to_hash(:root => false)
91
92
  end
93
+
94
+ protected
95
+
96
+ # Returns a guess at the default object for this template
97
+ def default_object
98
+ @_handler.respond_to?(:controller) ?
99
+ instance_variable_get("@#{@_handler.controller.controller_name}") :
100
+ nil
101
+ end
92
102
  end
93
103
  end
data/lib/rabl/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Rabl
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rabl
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Nathan Esquenazi