Class: Rad::Router::ObjectRouter

Inherits:
Object
  • Object
show all
Includes:
AbstractRouter
Defined in:
lib/rad/router/_object_router.rb

Instance Method Summary collapse

Constructor Details

#initializeObjectRouter

Returns a new instance of ObjectRouter.



4
5
6
7
8
9
10
11
12
13
# File 'lib/rad/router/_object_router.rb', line 4

def initialize
  self.class_to_resource = -> klass      {raise ':class_to_resource not specified!'}
  self.resource_to_class = -> resource   {raise ':resource_to_class not specified!'}
  self.id_to_class       = -> id, params {raise ':id_to_class not specified!'}

  self.default_class_method = :all
  self.default_object_method    = :show

  @cached_class_to_resource, @cached_resource_to_class = {}, {}
end

Instance Method Details

#configure(options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rad/router/_object_router.rb', line 15

def configure options = {}
  raise "can't configure ObjectRouter twice!" if configured?
  self.configured = true

  # parsing options
  options.validate_options! :default_class_name, :class_to_resource, :resource_to_class, :id_to_class, :url_root, :prefix

  self.default_class_name     = options[:default_class_name] || raise(':default_class_name not provided!')

  self.class_to_resource = options[:class_to_resource] if options.include? :class_to_resource
  self.resource_to_class = options[:resource_to_class] if options.include? :resource_to_class
  self.id_to_class       = options[:id_to_class]       if options.include? :id_to_class

  self.url_root = parse_url_root options
  self.prefix   = parse_prefix options
end

#decode(path, params) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/rad/router/_object_router.rb', line 70

def decode path, params
  return nil unless configured?

  parts = path[1..-1].split('/')

  # checking 'size' of path
  extra_size = (prefix ? prefix.size : 0) + (url_root ? 1 : 0)
  valid_range = (1 + extra_size)..(2 + extra_size)
  raise "invalid 'size' of path, must be in #{valid_range}!" unless valid_range.include? parts.size

  # url_root
  if url_root
    raise "invalid :url_root ('/#{parts.first}' should be equal to '#{url_root}')!" unless "/#{parts.first}" == url_root
    parts.shift
  end

  # prefix
  decode_prefix_params! parts, params, prefix if prefix

  # id, resource and method
  if parts.first =~ /^[A-Z]/
    resource, method = parts
    method ||= default_class_method
    method = method.to_sym

    raise "resource must be in plural form ('#{resource}')!" unless resource.pluralize == resource

    klass = cached_resource_to_class resource

    raise "no class for '#{resource}' resource!" unless klass
  else
    id, method = parts

    method ||= default_object_method
    method = method.to_sym

    # sometimes we need params inside of :id_to_class block,
    # for example to find out Space before finding Item
    klass = id_to_class.call id, params

    raise "no class for '#{id}' id!" unless klass

    params[:id] = id
  end

  klass.must_be.a Class
  return klass, method, params
end

#encode(klass, method, params) ⇒ Object



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
64
65
66
67
68
# File 'lib/rad/router/_object_router.rb', line 32

def encode klass, method, params
  return nil unless configured?

  id = params[:id]

  # id and resource
  path = if id
    raise ":id can be used with :default_class only (#{klass}, '#{id}')!" if klass != default_class

    params.delete :id
    raise ":id must not start from capital letter ('#{id}')!" if id =~ /^[A-Z]/

    path = if !method or method == default_object_method
      "/#{id}"
    else
      "/#{id}/#{method}"
    end
  else
    resource = cached_class_to_resource klass
    raise "no resource for #{klass}!" unless resource
    raise "resource must start from capital letter (class: #{klass}, resource: '#{resource}')!" unless resource =~ /^[A-Z]/

    path = if !method or method == default_class_method
      "/#{resource}"
    else
      "/#{resource}/#{method}"
    end
  end

  # prefix
  path = encode_prefix_params! path, params, prefix if prefix

  # url_root
  params[:url_root] = url_root if url_root

  [path, params]
end

#encode_method(route_method, has_id) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/rad/router/_object_router.rb', line 119

def encode_method route_method, has_id
  return nil unless configured?

  token = route_method.sub(/_?path$/, '')

  if has_id
    method = token.empty? ? default_object_method : token
    method = method.to_sym

    return [default_class, method]
  else
    token = route_method.sub(/_path$/, '')
    parts = token.split '_'
    resource = parts.pop
    method = parts.empty? ? default_class_method : parts.join('_')

    # class
    resource = resource.pluralize.camelize
    klass = cached_resource_to_class resource
    raise "no class for '#{resource}' resource!" unless klass
    klass.must_be.a Class

    # method
    method = method.to_sym if method

    return [klass, method]
  end
end