rabl 0.0.7 → 0.0.8
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/README.md +7 -0
- data/lib/rabl/builder.rb +17 -4
- data/lib/rabl/engine.rb +3 -2
- data/lib/rabl/version.rb +1 -1
- metadata +3 -3
data/README.md
CHANGED
@@ -101,6 +101,13 @@ This will generate a json response based on the result of the code block:
|
|
101
101
|
u.first_name + " " + u.last_name
|
102
102
|
end
|
103
103
|
|
104
|
+
or a custom node that exists only if a condition is true:
|
105
|
+
|
106
|
+
# m is the object being rendered, also supports :unless
|
107
|
+
code(:foo, :if => lambda { |m| m.has_foo? }) do |m|
|
108
|
+
m.foo
|
109
|
+
end
|
110
|
+
|
104
111
|
You can use custom "code" nodes to create flexible representations of a value utilizing all the data from the model.
|
105
112
|
|
106
113
|
### Partials ###
|
data/lib/rabl/builder.rb
CHANGED
@@ -15,8 +15,8 @@ module Rabl
|
|
15
15
|
attribute(attribute, :as => name)
|
16
16
|
end if @options.has_key?(:attributes)
|
17
17
|
# Code
|
18
|
-
@options[:code].each_pair do |name,
|
19
|
-
code(name, &block)
|
18
|
+
@options[:code].each_pair do |name, settings|
|
19
|
+
code(name, settings[:options], &settings[:block])
|
20
20
|
end if @options.has_key?(:code)
|
21
21
|
# Children
|
22
22
|
@options[:child].each do |settings|
|
@@ -51,10 +51,13 @@ module Rabl
|
|
51
51
|
alias_method :attributes, :attribute
|
52
52
|
|
53
53
|
# Creates an arbitrary code node that is included in the json output
|
54
|
+
# node(:foo) { "bar" }
|
54
55
|
# code(:foo) { "bar" }
|
55
|
-
|
56
|
-
|
56
|
+
# code(:foo, :if => lambda { |m| m.foo.present? }) { "bar" }
|
57
|
+
def code(name, options={}, &block)
|
58
|
+
@_result[name] = block.call(@_object) if resolve_condition(options)
|
57
59
|
end
|
60
|
+
alias_method :node, :code
|
58
61
|
|
59
62
|
# Creates a child node that is included in json output
|
60
63
|
# child(@user) { attribute :full_name }
|
@@ -106,5 +109,15 @@ module Rabl
|
|
106
109
|
return data.first.class.model_name.element.pluralize if data.respond_to?(:first) && data.first.respond_to?(:valid?)
|
107
110
|
data.class.model_name.element
|
108
111
|
end
|
112
|
+
|
113
|
+
# resolve_condition(:if => true) => true
|
114
|
+
# resolve_condition(:if => lambda { |m| false }) => false
|
115
|
+
# resolve_condition(:unless => lambda { |m| true }) => true
|
116
|
+
def resolve_condition(options)
|
117
|
+
return true if options[:if].nil? && options[:unless].nil?
|
118
|
+
result = options[:if] == true || (options[:if].respond_to?(:call) && options[:if].call(@_object)) if options.has_key?(:if)
|
119
|
+
result = options[:unless] == false || (options[:unless].respond_to?(:call) && !options[:unless].call(@_object)) if options.has_key?(:unless)
|
120
|
+
result
|
121
|
+
end
|
109
122
|
end
|
110
123
|
end
|
data/lib/rabl/engine.rb
CHANGED
@@ -42,9 +42,10 @@ module Rabl
|
|
42
42
|
|
43
43
|
# Creates an arbitrary code node that is included in the json output
|
44
44
|
# code(:foo) { "bar" }
|
45
|
-
|
45
|
+
# code(:foo, :if => lambda { ... }) { "bar" }
|
46
|
+
def code(name, options={}, &block)
|
46
47
|
@_options[:code] ||= {}
|
47
|
-
@_options[:code][name] = block
|
48
|
+
@_options[:code][name] = { :options => options, :block => block }
|
48
49
|
end
|
49
50
|
|
50
51
|
# Creates a child node that is included in json output
|
data/lib/rabl/version.rb
CHANGED
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:
|
4
|
+
hash: 15
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 8
|
10
|
+
version: 0.0.8
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Nathan Esquenazi
|