Module: RablToJbuilder

Defined in:
lib/rabl_to_jbuilder.rb,
lib/rabl_to_jbuilder/version.rb,
lib/rabl_to_jbuilder/transformer.rb

Defined Under Namespace

Classes: Base, ChildTransformer, NodeTransformer, SimpleTransformer, Transformer

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.convert(rabl, object: nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rabl_to_jbuilder.rb', line 8

def self.convert(rabl, object: nil)
  parser    = RubyParser.new
  ruby2ruby = Ruby2Ruby.new
  root_node = parser.process(rabl)

  return "" unless root_node

  if !object
    match = root_node / Sexp.s(:call, nil, :object, Sexp._)
    object = match[0][3] unless match.empty?

    collection_match = root_node / Sexp.s(:call, nil, :collection, Sexp._)
    if collection_match.any?
      collection = collection_match[0][3]
      raise unless collection[0] == :ivar
      singular_name = collection[1].to_s[1..-1].singularize.to_sym
      root_node = s(:iter, s(:call, s(:call, nil, :json), :array!, collection), s(:args, singular_name), root_node)
      object = s(:lvar, singular_name.to_sym)
    end
  end

  transformer = Transformer.new(object)
  converted = transformer.process(root_node)

  ruby2ruby.process(converted)
end