rabl-rails/lib/rabl-fast-json/template.rb

86 lines
2.6 KiB
Ruby
Raw Normal View History

2012-02-22 18:14:00 +00:00
module RablFastJson
class CompiledTemplate
2012-02-27 13:49:34 +00:00
attr_accessor :source, :data, :root_name, :context
2012-02-22 18:14:00 +00:00
2012-02-27 13:49:34 +00:00
delegate :[], :[]=, :merge!, :to => :source
2012-02-22 18:14:00 +00:00
def initialize
@source = {}
end
2012-03-22 10:24:55 +00:00
def has_root_name?
!@root_name.nil?
end
2012-02-27 13:49:34 +00:00
def render
2012-03-15 14:12:52 +00:00
get_object_from_context
get_assigns_from_context
2012-02-27 13:49:34 +00:00
@object.respond_to?(:each) ? render_collection : render_resource
end
2012-03-27 15:07:30 +00:00
2012-02-27 13:49:34 +00:00
def render_resource(data = nil, source = nil)
data ||= @object
source ||= @source
2012-02-27 16:25:06 +00:00
source.inject({}) { |output, current|
key, value = current
2012-03-02 10:32:11 +00:00
2012-02-27 13:49:34 +00:00
out = case value
when Symbol
data.send(value) # attributes
when Proc
2012-03-15 14:12:52 +00:00
instance_exec data, &value # node
when Array # node with condition
2012-03-27 15:07:30 +00:00
next output if !instance_exec data, &(value.first) #value.first.call(data)
2012-03-15 14:12:52 +00:00
instance_exec data, &(value.last)
2012-02-27 13:49:34 +00:00
when Hash
2012-03-02 10:32:11 +00:00
current_value = value.dup
data_symbol = current_value.delete(:_data)
object = data_symbol.nil? ? data : data_symbol.to_s.start_with?('@') ? @context.instance_variable_get(data_symbol) : data.send(data_symbol)
2012-02-27 13:49:34 +00:00
if key.to_s.start_with?('_') # glue
2012-03-02 10:32:11 +00:00
current_value.each_pair { |k, v|
2012-02-27 13:49:34 +00:00
output[k] = object.send(v)
}
2012-02-27 16:25:06 +00:00
next output
2012-02-27 13:49:34 +00:00
else # child
2012-03-02 10:32:11 +00:00
object.respond_to?(:each) ? render_collection(object, current_value) : render_resource(object, current_value)
2012-02-27 13:49:34 +00:00
end
end
output[key] = out
2012-02-27 16:25:06 +00:00
output
2012-02-27 13:49:34 +00:00
}
end
def render_collection(collection = nil, source = nil)
collection ||= @object
2012-02-27 16:25:06 +00:00
collection.inject([]) { |output, o| output << render_resource(o, source) }
2012-02-27 13:49:34 +00:00
end
2012-03-27 16:35:36 +00:00
def method_missing(name, *args, &block)
@context.respond_to?(name) ? @context.send(name, *args, &block) : super
end
def partial(template_path, options = {})
raise "No object was given to partial" if options[:object].nil?
2012-03-27 16:35:36 +00:00
object = options[:object]
return [] if object.respond_to?(:empty?) && object.empty?
2012-03-27 16:39:26 +00:00
template = Library.instance.get(template_path)
2012-03-27 16:35:36 +00:00
object.respond_to?(:each) ? template.render_collection(object) : template.render_resource(object)
end
protected
def get_object_from_context
@object = @context.instance_variable_get(@data) if @data
end
def get_assigns_from_context
@context.instance_variable_get(:@_assigns).each_pair { |k, v|
instance_variable_set("@#{k}", v) unless k.start_with?('_') || k == @data
}
end
2012-02-22 18:14:00 +00:00
end
end