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.
-
#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
Provide a conditionnal block.
-
#extends(path) ⇒ Object
Extends an existing rabl template Example: extends ‘users/base’.
-
#glue(data) ⇒ Object
Glues data from a child node to the output Example: glue(:@user) { attribute :name }.
-
#initialize ⇒ Compiler
constructor
A new instance of Compiler.
-
#node(name, 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 ⇒ Compiler
Returns a new instance of Compiler.
7 8 9 |
# File 'lib/rabl-rails/compiler.rb', line 7 def initialize @i = 0 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 |
# File 'lib/rabl-rails/compiler.rb', line 43 def attribute(*args) if args.first.is_a?(Hash) args.first.each_pair { |k, v| @template[v] = k } else = args. args.each { |name| key = [:as] || name @template[key] = name } end 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. Notes that partial and blocks are not compatible Example:
child(:@posts, :root => :posts) { attribute :id }
child(:posts, :partial => 'posts/base')
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/rabl-rails/compiler.rb', line 65 def child(name_or_data, = {}) data, name = extract_data_and_name(name_or_data) name = [:root] if .has_key? :root if [:partial] template = Library.instance.compile_template_from_path([:partial]) @template[name] = template.merge!(:_data => data) elsif block_given? @template[name] = sub_compile(data) { yield } end 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
Provide a conditionnal block
condition(->(u) { u.is_a?(Admin) }) do
attributes :secret
end
128 129 130 131 132 133 |
# File 'lib/rabl-rails/compiler.rb', line 128 def condition(proc) return unless block_given? name = :"_if#{@i}" @i += 1 @template[name] = Condition.new(proc, sub_compile(nil) { yield }) end |
#extends(path) ⇒ Object
Extends an existing rabl template Example:
extends 'users/base'
116 117 118 119 |
# File 'lib/rabl-rails/compiler.rb', line 116 def extends(path) t = Library.instance.compile_template_from_path(path) @template.merge!(t.source) end |
#glue(data) ⇒ Object
Glues data from a child node to the output Example:
glue(:@user) { attribute :name }
81 82 83 84 85 86 |
# File 'lib/rabl-rails/compiler.rb', line 81 def glue(data) return unless block_given? name = :"_glue#{@i}" @i += 1 @template[name] = sub_compile(data) { yield } end |
#node(name, 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 }
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/rabl-rails/compiler.rb', line 96 def node(name, = {}, &block) condition = [:if] if condition if condition.is_a?(Proc) @template[name] = [condition, block] else @template[name] = block if condition end else @template[name] = block end 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 |