Class: RablRails::Compiler

Inherits:
Object
  • Object
show all
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

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
    options = args.extract_options!
    args.each { |name|
      key = options[:as] || name
      node[key] = name
    }
    node.condition = options[:if]
  end

  @template.add_node node
end

#cache(&block) ⇒ Object



142
143
144
# File 'lib/rabl-rails/compiler.rb', line 142

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. Notes 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
77
78
79
80
81
82
# File 'lib/rabl-rails/compiler.rb', line 70

def child(name_or_data, options = {})
  data, name = extract_data_and_name(name_or_data)
  name = options[:root] if options.has_key? :root

  if options.key?(:partial)
    template = Library.instance.compile_template_from_path(options[:partial], @view)
    template.data = data
  elsif block_given?
    template = sub_compile(data) { yield }
  end

  @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

Provide a conditionnal block

condition(->(u) { u.is_a?(Admin) }) do

attributes :secret

end



137
138
139
140
# File 'lib/rabl-rails/compiler.rb', line 137

def condition(proc)
  return unless block_given?
  @template.add_node Nodes::Condition.new(proc, sub_compile(nil, true) { yield })
end

#extends(path) ⇒ Object

Extends an existing rabl template Example:

extends 'users/base'


126
127
128
# File 'lib/rabl-rails/compiler.rb', line 126

def extends(path)
  @template.extends Library.instance.compile_template_from_path(path, @view)
end

#glue(data) ⇒ Object

Glues data from a child node to the output Example:

glue(:@user) { attribute :name }


89
90
91
92
93
94
# File 'lib/rabl-rails/compiler.rb', line 89

def glue(data)
  return unless block_given?

  template = sub_compile(data) { yield }
  @template.add_node Nodes::Glue.new(template)
end

#mergeObject

Merge arbitrary data into json output. Given block should return a hash. Example:

merge { |item| partial("specific/#{item.to_s}", object: item) }


116
117
118
119
# File 'lib/rabl-rails/compiler.rb', line 116

def merge
  return unless block_given?
  node(nil) { 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 }


104
105
106
107
# File 'lib/rabl-rails/compiler.rb', line 104

def node(name = nil, options = {}, &block)
  return unless block_given?
  @template.add_node Nodes::Code.new(name, block, options[: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, options = {})
  @template.data, @template.root_name = extract_data_and_name(data)
  @template.root_name = options[:root] if options.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