Module: IRails::Display::Registry

Extended by:
Registry
Included in:
Registry
Defined in:
lib/irails/display.rb

Constant Summary collapse

SUPPORTED_MIMES =
%w(
text/plain
text/html
text/latex
application/json
application/javascript
image/png
image/jpeg
image/svg+xml)

Instance Method Summary collapse

Instance Method Details

#format(mime = nil, &block) ⇒ Object



150
151
152
153
154
155
156
157
158
159
# File 'lib/irails/display.rb', line 150

def format(mime = nil, &block)
  renderer << Renderer.new(@match, mime, block, @priority)
  renderer.sort_by! {|r| -r.priority }

  # Decrease priority implicitly for all formats
  # which are added later for a type.
  # Overwrite with the `priority` method!
  @priority -= 1
  nil
end

#match(&block) ⇒ Object



123
124
125
126
127
# File 'lib/irails/display.rb', line 123

def match(&block)
  @match = block
  priority 0
  nil
end

#priority(p) ⇒ Object



145
146
147
148
# File 'lib/irails/display.rb', line 145

def priority(p)
  @priority = p
  nil
end

#rendererObject



109
110
111
# File 'lib/irails/display.rb', line 109

def renderer
  @renderer ||= []
end

#respond_to(name) ⇒ Object



129
130
131
# File 'lib/irails/display.rb', line 129

def respond_to(name)
  match {|obj| obj.respond_to?(name) }
end

#type(&block) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/irails/display.rb', line 133

def type(&block)
  match do |obj|
    begin
      block.call === obj
    # We have to rescue all exceptions since constant autoloading could fail with a different error
    rescue Exception
    rescue #NameError
      false
    end
  end
end