module IRails::Display
Public Class Methods
convert(obj, options)
click to toggle source
# File lib/irails/display.rb, line 4 def convert(obj, options) Representation.new(obj, options) end
display(obj, options = {})
click to toggle source
# File lib/irails/display.rb, line 8 def display(obj, options = {}) obj = convert(obj, options) options = obj.options obj = obj.object fuzzy_mime = options[:format] # Treated like a fuzzy mime type raise 'Invalid argument :format' unless !fuzzy_mime || String === fuzzy_mime if exact_mime = options[:mime] raise 'Invalid argument :mime' unless String === exact_mime raise 'Invalid mime type' unless exact_mime.include?('/') end data = {} # Render additional representation render(data, obj, exact_mime, fuzzy_mime) # IPython always requires a text representation render(data, obj, 'text/plain', nil) unless data['text/plain'] # As a last resort, interpret string representation of the object # as the given mime type. data[exact_mime] = protect(exact_mime, obj) if exact_mime && !data.any? {|m,_| exact_mime == m } data end
Private Class Methods
protect(mime, data)
click to toggle source
# File lib/irails/display.rb, line 37 def protect(mime, data) MimeMagic.new(mime).text? ? data.to_s : [data.to_s].pack('m0') end
render(data, obj, exact_mime, fuzzy_mime)
click to toggle source
# File lib/irails/display.rb, line 41 def render(data, obj, exact_mime, fuzzy_mime) # Filter matching renderer by object type renderer = Registry.renderer.select {|r| r.match?(obj) } matching_renderer = nil # Find exactly matching display by exact_mime matching_renderer = renderer.find {|r| exact_mime == r.mime } if exact_mime # Find fuzzy matching display by fuzzy_mime matching_renderer ||= renderer.find {|r| r.mime && r.mime.include?(fuzzy_mime) } if fuzzy_mime renderer.unshift matching_renderer if matching_renderer # Return first render result which has the right mime type renderer.each do |r| mime, result = r.render(obj) if mime && result && (!exact_mime || exact_mime == mime) && (!fuzzy_mime || mime.include?(fuzzy_mime)) data[mime] = protect(mime, result) break end end nil end