lib/yaks/format/html.rb in yaks-html-0.9.0 vs lib/yaks/format/html.rb in yaks-html-0.10.0

- old
+ new

@@ -10,31 +10,39 @@ def template @template ||= Hexp.parse(File.read(File.expand_path('../template.html', __FILE__))) end def section(name) - template.select(".#{name}").first + template.select(".#{name}").first # rubocop:disable Performance/Detect end def serialize_resource(resource) template.replace('.resource') do |_| - render_resource(resource) + render(resource) end.replace('.yaks-version') do |ver| ver.content(Yaks::VERSION) end.replace('.request-info') do |req| req.content(env['REQUEST_METHOD'], ' ', env['PATH_INFO']) end end + def render(*args) + object = args.first + type = object.class.name.split('::').last + send("render_#{underscore(type)}", *args) + end + def render_resource(resource, templ = section('resource')) templ .replace('.type') { |header| header.content(resource.type.to_s + (resource.collection? ? ' collection' : '')) } .replace('.attribute', &render_attributes(resource.attributes)) .replace('.links') {|links| resource.links.empty? ? [] : links.replace('.link', &render_links(resource.links)) } .replace('.forms') {|div| render_forms(resource.forms).call(div) } .replace('.subresource') {|sub_templ| render_subresources(resource, templ, sub_templ) } end + alias_method :render_collection_resource, :render_resource + alias_method :render_null_resource, :render_resource def render_attributes(attributes) ->(templ) do attributes.map do |key, value| templ @@ -54,39 +62,46 @@ def render_links(links) ->(templ) do links.map do |link| templ - .replace('.rel a') {|a| a.attr('href', rel_href(link.rel)).content(link.rel.to_s) } - .replace('.uri a') {|a| a.attr('href', link.uri).content(link.uri) } + .replace('.rel a') {|a| + a.attr('href', rel_href(link.rel)).content(link.rel.to_s) + } + .replace('.uri a') {|a| + a.attr('href', link.uri).content(link.uri) + .attr('rel', link.rel.to_s) + } .replace('.title') {|x| x.content(link.title.to_s) } .replace('.templated') {|x| x.content(link.templated?.inspect) } end end end def render_subresources(resource, templ, sub_templ) - templ = templ.replace('h1,h2,h3,h4') {|h| h.set_tag("h#{h.tag[/\d/].to_i.next}") } + templ = templ + .replace('h1,h2,h3,h4') {|h| h.set_tag("h#{h.tag[/\d/].to_i.next}") } + .add_class('collapsed') if resource.collection? resource.seq.map do |r| - render_resource(r, templ) + render(r, templ) end else resource.subresources.map do |resources| rel = resources.rels.first sub_templ .replace('.rel a') {|a| a.attr('href', rel_href(rel)).content(rel.to_s) } - .replace('.value') {|x| x.content(resources.seq.map { |resource| render_resource(resource, templ) })} + .replace('.value') {|x| x.content(resources.seq.map { |res| render(res, templ) })} + .attr('rel', rel.to_s) end end end - def render_forms(forms) ->(div) do div.content( - forms.map(&method(:render_form)) + forms.map(&method(:render)) ) end end def render_form(form_control) @@ -94,32 +109,29 @@ form = form.attr('name', form_control.name) if form_control.name form = form.attr('method', form_control.method) if form_control.method form = form.attr('action', form_control.action) if form_control.action form = form.attr('enctype', form_control.media_type) if form_control.media_type - rows = form_control.fields.map(&method(:render_field)) + rows = form_control.fields.map(&method(:render)) rows << H[:tr, H[:td], H[:td, H[:input, {type: 'submit'}]]] H[:div, H[:h4, form_control.title || form_control.name.to_s], form.content(H[:table, rows])] end def render_field(field) - return render_fieldset(field) if field.type == :fieldset extra_info = reject_keys(field.to_h_compact, :type, :name, :value, :label, :options) H[:tr, H[:td, H[:label, {for: field.name}, [field.label || field.name.to_s, field.required ? '*' : ''].join]], H[:td, case field.type when /select/ H[:select, reject_keys(field.to_h_compact, :options), render_select_options(field.options)] when /textarea/ H[:textarea, reject_keys(field.to_h_compact, :value), field.value || ''] - when /legend/ - H[:legend, field.to_h_compact] when /hidden/ [ field.value.inspect, H[:input, field.to_h_compact] ] else @@ -128,15 +140,16 @@ H[:td, extra_info.empty? ? '' : extra_info.inspect] ] end def render_fieldset(fieldset) - legend = fieldset.fields.select {|field|field.type == :legend}.first - legend = legend ? legend.name : '' + legend = fieldset.fields.find {|field| field.type == :legend} + fields = fieldset.fields.reject {|field| field.type == :legend} + legend = legend ? legend.label : '' H[:tr, H[:th, legend], - H[:td, H[:fieldset, H[:table, fieldset.fields.map(&method(:render_field))]]]] + H[:td, H[:fieldset, H[:table, fields.map(&method(:render))]]]] end def render_select_options(options) options.map do |o| H[:option, reject_keys(o.to_h_compact, :label), o.label]