Class: RablRails::Compiler
- Inherits:
-
Object
- Object
- RablRails::Compiler
- Defined in:
- lib/rabl-rails/compiler.rb
Overview
Class that will compile RABL source code into a hash representing data structure
Instance Method Summary collapse
-
#attribute(*args) ⇒ Object
(also: #attributes)
Includes the attribute or method in the output Example: attributes :id, :name attribute :email => :super_secret.
- #cache(&block) ⇒ Object
-
#child(name_or_data, options = {}) ⇒ Object
Creates a child node to be included in the output.
-
#compile_source(source) ⇒ Object
Compile from source code and return the CompiledTemplate created.
-
#condition(proc) ⇒ Object
(also: #_if)
Provide a conditionnal block.
-
#const(name, value) ⇒ Object
Creates a constant node in the json output.
-
#extends(path_or_lambda, options = nil) ⇒ Object
Extends an existing rabl template Example: extends ‘users/base’ extends ->(item) { “v1/#itemitem.class/_core” } extends ‘posts/base’, locals: { hide_comments: true }.
-
#fetch(name_or_data, options = {}) ⇒ Object
Creates a node to be added to the output by fetching an object using current resource’s field as key to the data, and appliying given template to said object Example: fetch(:@stats, field: :id) { attributes :total }.
-
#glue(data, options = {}) ⇒ Object
Glues data from a child node to the output Example: glue(:@user) { attribute :name }.
-
#initialize(view) ⇒ Compiler
constructor
A new instance of Compiler.
-
#lookup(name, object, field: :id, cast: false) ⇒ Object
Create a node ‘name` by looking the current resource being rendered in the `object` hash using, by default, the resource’s id.
-
#merge(opts = {}) ⇒ Object
Merge arbitrary data into json output.
-
#node(name = nil, options = {}, &block) ⇒ Object
(also: #code)
Creates an arbitrary node in the json output.
-
#object(data, options = {}) ⇒ Object
(also: #collection)
Sets the object to be used as the data for the template Example: object :@user object :@user, :root => :author.
- #root(name) ⇒ Object
Constructor Details
#initialize(view) ⇒ Compiler
Returns a new instance of Compiler.
7 8 9 |
# File 'lib/rabl-rails/compiler.rb', line 7 def initialize(view) @view = view end |
Instance Method Details
#attribute(*args) ⇒ Object Also known as: attributes
Includes the attribute or method in the output Example:
attributes :id, :name
attribute :email => :super_secret
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/rabl-rails/compiler.rb', line 43 def attribute(*args) node = Nodes::Attribute.new if args.first.is_a?(Hash) args.first.each_pair { |k, v| node[v] = k } else = args. args.each { |name| key = [:as] || name node[key] = name } node.condition = [:if] end @template.add_node node end |
#cache(&block) ⇒ Object
182 183 184 |
# File 'lib/rabl-rails/compiler.rb', line 182 def cache(&block) @template.cache_key = block_given? ? block : nil end |
#child(name_or_data, options = {}) ⇒ Object
Creates a child node to be included in the output. name_or data can be an object or collection or a method to call on the data. It accepts :root and :partial options. Note that partial and blocks are not compatible Example:
child(:@posts, :root => :posts) { attribute :id }
child(:posts, :partial => 'posts/base')
70 71 72 73 74 75 76 |
# File 'lib/rabl-rails/compiler.rb', line 70 def child(name_or_data, = {}) data, name = extract_data_and_name(name_or_data) name = [:root] if .has_key? :root name = [:as] if .has_key? :as template = partial_or_block(data, ) { yield } @template.add_node Nodes::Child.new(name, template) end |
#compile_source(source) ⇒ Object
Compile from source code and return the CompiledTemplate created.
15 16 17 18 19 |
# File 'lib/rabl-rails/compiler.rb', line 15 def compile_source(source) @template = CompiledTemplate.new instance_eval(source) @template end |
#condition(proc) ⇒ Object Also known as: _if
Provide a conditionnal block
condition(->(u) { u.is_a?(Admin) }) do
attributes :secret
end
176 177 178 179 |
# File 'lib/rabl-rails/compiler.rb', line 176 def condition(proc) return unless block_given? @template.add_node Nodes::Condition.new(proc, sub_compile(nil, true) { yield }) end |
#const(name, value) ⇒ Object
Creates a constant node in the json output. Example:
const(:locale, 'fr_FR')
122 123 124 |
# File 'lib/rabl-rails/compiler.rb', line 122 def const(name, value) @template.add_node Nodes::Const.new(name, value) end |
#extends(path_or_lambda, options = nil) ⇒ Object
Extends an existing rabl template Example:
extends 'users/base'
extends ->(item) { "v1/#{item.class}/_core" }
extends 'posts/base', locals: { hide_comments: true }
154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/rabl-rails/compiler.rb', line 154 def extends(path_or_lambda, = nil) if path_or_lambda.is_a?(Proc) @template.add_node Nodes::Polymorphic.new(path_or_lambda) return end other = Library.instance.compile_template_from_path(path_or_lambda, @view) if && .is_a?(Hash) @template.add_node Nodes::Extend.new(other.nodes, [:locals]) else @template.extends(other) end end |
#fetch(name_or_data, options = {}) ⇒ Object
Creates a node to be added to the output by fetching an object using current resource’s field as key to the data, and appliying given template to said object Example:
fetch(:@stats, field: :id) { attributes :total }
95 96 97 98 99 100 101 |
# File 'lib/rabl-rails/compiler.rb', line 95 def fetch(name_or_data, = {}) data, name = extract_data_and_name(name_or_data) name = [:as] if .key?(:as) field = .fetch(:field, :id) template = partial_or_block(data, ) { yield } @template.add_node Nodes::Fetch.new(name, template, field) end |
#glue(data, options = {}) ⇒ Object
Glues data from a child node to the output Example:
glue(:@user) { attribute :name }
83 84 85 86 |
# File 'lib/rabl-rails/compiler.rb', line 83 def glue(data, = {}) template = partial_or_block(data, ) { yield } @template.add_node Nodes::Glue.new(template) end |
#lookup(name, object, field: :id, cast: false) ⇒ Object
Create a node ‘name` by looking the current resource being rendered in the `object` hash using, by default, the resource’s id. Example:
lookup(:favorite, :@user_favorites, cast: true)
132 133 134 |
# File 'lib/rabl-rails/compiler.rb', line 132 def lookup(name, object, field: :id, cast: false) @template.add_node Nodes::Lookup.new(name, object, field, cast) end |
#merge(opts = {}) ⇒ Object
Merge arbitrary data into json output. Given block should return a hash. Example:
merge { |item| partial("specific/#{item.to_s}", object: item) }
142 143 144 145 |
# File 'lib/rabl-rails/compiler.rb', line 142 def merge(opts = {}) return unless block_given? node(nil, opts) { yield } end |
#node(name = nil, options = {}, &block) ⇒ Object Also known as: code
Creates an arbitrary node in the json output. It accepts :if option to create conditionnal nodes. The current data will be passed to the block so it is advised to use it instead of ivars. Example:
node(:name) { |user| user.first_name + user.last_name }
node(:role, if: ->(u) { !u.admin? }) { |u| u.role }
111 112 113 114 |
# File 'lib/rabl-rails/compiler.rb', line 111 def node(name = nil, = {}, &block) return unless block_given? @template.add_node Nodes::Code.new(name, block, [:if]) end |
#object(data, options = {}) ⇒ Object Also known as: collection
Sets the object to be used as the data for the template Example:
object :@user
object :@user, :root => :author
27 28 29 30 |
# File 'lib/rabl-rails/compiler.rb', line 27 def object(data, = {}) @template.data, @template.root_name = extract_data_and_name(data) @template.root_name = [:root] if .has_key? :root end |
#root(name) ⇒ Object
33 34 35 |
# File 'lib/rabl-rails/compiler.rb', line 33 def root(name) @template.root_name = name end |