class Picatrix::Routes

this class is the data structure you’ll construct the Sinatra routes and their corresponding handling method bodies

Attributes

controls[RW]
root_path[RW]
unique_routes[RW]

Public Class Methods

new(edges, control_templates) click to toggle source
# File lib/picatrix/routes.rb, line 7
def initialize(edges, control_templates)
  @controls = {}

  edges.collect do |edge|
    edge.values.first[:target]
  end.each do |resource|
    if (control = control_templates[resource])
      @controls.merge!(control.to_hash)
    end
  end

  @unique_routes = edges.collect do |edge|
    source = edge.keys.first
    target = edge.values.first[:target]
    properties = edge[source]

    link_relation = properties[:link_relation]

    if source == "root"
      #TODO right now app is using this in place of
      # figuring out parent collections of collections
      @root_path = target
      source = target
    end

    path = case properties[:method]
           when :post
             source.pluralize
           when :get
             if is_collection?(target)
               "#{source.pluralize}"
             else
               "#{source.pluralize}/#{link_relation}"
             end
           when :patch
             "#{source.pluralize}/:item_id"
           when :put
             "#{source.pluralize}/:item_id"
           when :delete
             "#{source.pluralize}/:item_id"
           else
             "#{source.pluralize}/#{link_relation}"
           end

    {
      path: path,
      target: target,
      link_relation: link_relation,
      method: properties[:method],
      response_resource: {
        name: target.camelcase.constantize,
        collection: is_collection?(target)
      }
    }
    # make sure routes are not duplicated
  end.compact.uniq {|e| [e[:path], e[:method]]}
end

Public Instance Methods

is_collection?(target) click to toggle source
# File lib/picatrix/routes.rb, line 65
def is_collection?(target)
  (target == target.pluralize)
end