class Garage::HypermediaResponder::DataRenderer

Constants

JSON_ESCAPE_TABLE

Attributes

data[R]
options[R]

Public Class Methods

new(data, options = {}) click to toggle source
# File lib/garage/hypermedia_responder.rb, line 91
def initialize(data, options = {})
  @data, @options = data, options
end
render(*args) click to toggle source
# File lib/garage/hypermedia_responder.rb, line 85
def self.render(*args)
  new(*args).render
end

Public Instance Methods

render() click to toggle source
# File lib/garage/hypermedia_responder.rb, line 95
def render
  Oj.dump(converted_data, mode: :compat, use_as_json: true).gsub(/([<>])/, JSON_ESCAPE_TABLE)
end

Private Instance Methods

converted_data() click to toggle source
# File lib/garage/hypermedia_responder.rb, line 113
def converted_data
  if convertible_to_dictionary?
    indexed_data
  else
    data
  end
end
convertible_to_dictionary?() click to toggle source
# File lib/garage/hypermedia_responder.rb, line 105
def convertible_to_dictionary?
  dictionary? && data.is_a?(Array) && data.all? {|datum| datum.respond_to?(:[]) }
end
dictionary?() click to toggle source
# File lib/garage/hypermedia_responder.rb, line 101
def dictionary?
  !!options[:dictionary]
end
indexed_data() click to toggle source
# File lib/garage/hypermedia_responder.rb, line 109
def indexed_data
  data.index_by {|datum| datum["id"] }
end