class RablToJbuilder::ChildTransformer

Public Instance Methods

process_iter(exp) click to toggle source
# File lib/rabl_to_jbuilder/transformer.rb, line 39
def process_iter(exp)
  exp.shift # :iter
  call = exp.shift
  args = exp.shift
  block = exp.shift

  if call[0..2] == s(:call, nil, :child)
    child = call

    key, attribute = nil, nil

    if child[3][0] == :lit
      key = child[3][1]
      attribute = s(:call, @object, key)
    elsif child[3][0] == :hash
      _, attribute, key = child[3]
      if attribute[0] == :lit
        attribute = s(:call, @object, attribute[1])
      end

      raise unless key[0] == :lit
      key = key[1]
    else
      raise "wtf"
    end

    if plural?(key)
      singular_key = key.to_s.singularize.to_sym
      args = s(:args, singular_key)

      block = Transformer.new(s(:lvar, singular_key)).process(block)

      s(:iter, s(:call, json, key, attribute), args, block)
    else
      args = 0
      block = Transformer.new(attribute).process(block)

      s(:iter, s(:call, json, key), args, block)
    end
  elsif call[0..2] == s(:call, nil, :glue)
    raise unless call[3][0] == :lit
    attribute = call[3][1]
    object = s(:call, @object, attribute)
    block = Transformer.new(object).process(block)
    block
  else
    s(:iter, call, args, block)
  end
end

Private Instance Methods

plural?(s) click to toggle source
# File lib/rabl_to_jbuilder/transformer.rb, line 91
def plural?(s)
  s = s.to_s
  s.pluralize == s
end