Class: Yaks::Format::HTML

Inherits:
Yaks::Format show all
Includes:
Util
Defined in:
lib/yaks/format/html.rb

Instance Method Summary collapse

Instance Method Details

#rel_href(rel) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/yaks/format/html.rb', line 55

def rel_href(rel)
  if rel.is_a?(Symbol)
    "http://www.iana.org/assignments/link-relations/link-relations.xhtml"
  else
    rel.to_s
  end
end

#render(*args) ⇒ Object



28
29
30
31
32
# File 'lib/yaks/format/html.rb', line 28

def render(*args)
  object = args.first
  type = object.class.name.split('::').last
  send("render_#{underscore(type)}", *args)
end

#render_attributes(attributes) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/yaks/format/html.rb', line 45

def render_attributes(attributes)
  ->(templ) do
    attributes.map do |key, value|
      templ
        .replace('.name')  {|x| x.content(key.to_s) }
        .replace('.value') {|x| x.content(value.inspect) }
    end
  end
end

#render_field(field) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/yaks/format/html.rb', line 122

def render_field(field)
  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 /hidden/
        [ field.value.inspect,
          H[:input, field.to_h_compact]
        ]
      else
        H[:input, field.to_h_compact]
      end],
    H[:td, extra_info.empty? ? '' : extra_info.inspect]
   ]
end

#render_fieldset(fieldset) ⇒ Object



144
145
146
147
148
149
150
151
152
# File 'lib/yaks/format/html.rb', line 144

def render_fieldset(fieldset)
  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, fields.map(&method(:render))]]]]
end

#render_form(form_control) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/yaks/format/html.rb', line 107

def render_form(form_control)
  form = H[:form]
  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))
  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

#render_forms(forms) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/yaks/format/html.rb', line 99

def render_forms(forms)
  ->(div) do
    div.content(
      forms.map(&method(:render))
    )
  end
end


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/yaks/format/html.rb', line 63

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)
            .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

#render_resource(resource, templ = section('resource')) ⇒ Object Also known as: render_collection_resource, render_null_resource



34
35
36
37
38
39
40
41
# File 'lib/yaks/format/html.rb', line 34

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

#render_select_options(options) ⇒ Object



154
155
156
157
158
# File 'lib/yaks/format/html.rb', line 154

def render_select_options(options)
  options.map do |o|
    H[:option, reject_keys(o.to_h_compact, :label), o.label]
  end
end

#render_subresources(resource, templ, sub_templ) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/yaks/format/html.rb', line 80

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}") }
          .add_class('collapsed')
  if resource.collection?
    resource.seq.map do |r|
      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 { |res| render(res, templ) })}
        .attr('rel', rel.to_s)
    end
  end
end

#section(name) ⇒ Object



14
15
16
# File 'lib/yaks/format/html.rb', line 14

def section(name)
  template.select(".#{name}").first   # rubocop:disable Performance/Detect
end

#serialize_resource(resource) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/yaks/format/html.rb', line 18

def serialize_resource(resource)
  template.replace('.resource') do |_|
    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

#templateObject



10
11
12
# File 'lib/yaks/format/html.rb', line 10

def template
  @template ||= Hexp.parse(File.read(File.expand_path('../template.html', __FILE__)))
end