Class: Picatrix::Routes

Inherits:
Object
  • Object
show all
Defined in:
lib/picatrix/routes.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(edges, control_templates) ⇒ Routes

Returns a new instance of Routes.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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

Instance Attribute Details

#controlsObject

Returns the value of attribute controls.



5
6
7
# File 'lib/picatrix/routes.rb', line 5

def controls
  @controls
end

#root_pathObject

Returns the value of attribute root_path.



5
6
7
# File 'lib/picatrix/routes.rb', line 5

def root_path
  @root_path
end

#unique_routesObject

Returns the value of attribute unique_routes.



5
6
7
# File 'lib/picatrix/routes.rb', line 5

def unique_routes
  @unique_routes
end

Instance Method Details

#is_collection?(target) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/picatrix/routes.rb', line 65

def is_collection?(target)
  (target == target.pluralize)
end