Class: Ezframe::Route

Inherits:
Object show all
Defined in:
lib/ezframe/route.rb

Class Method Summary collapse

Class Method Details

._scan_route(target, route_h) ⇒ Object

targetに対応する名称のクラスまでの経路を返す



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ezframe/route.rb', line 88

def _scan_route(target, route_h)
  if route_h.keys.include?(target.to_sym)
    @get_path_found_it = true
    return [ target ]
  else
    route_h.each do |k, v|
      next if k.to_s =~ /^option_/
      if v.is_a?(Hash)
        a = _scan_route(target, v)
        if @get_path_found_it
          a.push(k)
          return a
        end
      end
    end
  end
  return nil
end

.choose(request, route_h = nil) ⇒ Object



4
5
6
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
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ezframe/route.rb', line 4

def choose(request, route_h = nil)
  path_parts = request.path_info.split("/").drop(1)
  route_h ||= Config[:route].deep_dup
  unless route_h
    raise "Config[:route] is not defined. It should be defined in config/route.yml"
  end
  # puts  "config=#{Config[:route]}, route_h=#{route_h}"
  args = {}
  opts = {}
  class_a = []
  # p path_parts
  if path_parts.empty?
    root_conf = route_h[:/]
    # p root_conf
    if root_conf
      klass = get_class(root_conf[:class])
      return [ klass.new, make_method_name("default", request.request_method) ]
    end
    return [ 404 ]
  end
  # URLを解析して、クラスの決定とIDの取得を行う
  while path_parts.length > 0
    part = path_parts.shift
    # break if part.empty?
    # EzLog.info "part=#{part}, route_h=#{route_h.inspect}"
    if route_h.has_key?(part.to_sym)
      # EzLog.info "has_route: #{part}"
      class_a.push(part)
      if path_parts[0].to_i > 0
        args[part.to_sym] = val = path_parts.shift
        # EzLog.info "value: part=#{part}, val=#{val}"
      end
      route_h = route_h[part.to_sym]
      break if route_h.nil?
      opts = {}
      route_h.keys.compact.each do |rkey|
        if rkey =~ /option_(\w+)/
          opt_key = $1
          opts[opt_key.to_sym] = route_h[rkey]
        end
      end
    else
      # routeに無ければ、メソッドを探す
      # EzLog.info "no_route: #{part}"
      klass = get_class(class_a[-1])
      return [ 404 ] unless klass
      instance = klass.new
      method_name = make_method_name(part, request.request_method)
      if instance.respond_to?(method_name)
        return [instance, method_name, args, opts]
      else
        EzLog.info "undefined method: #{klass}.#{method_name}: full path=#{request.path_info}"
      end
    end
  end
  # 最後にメソッド名が無い場合はpublic_default_#{method}を実行。
  #puts "class_a=#{class_a}"
  klass = get_class(class_a[-1])
  return [404] unless klass
  if path_parts.length > 0
    part = path_parts.shift
  else
    part = "default"
  end
  method_name = make_method_name(part, request.request_method)
  #EzLog.info "method_name=#{method_name}"
  instance = klass.new
  if instance.respond_to?(method_name)
    return [instance, method_name, args, opts]
  end
  return [ 404 ]
end

.get_class(keys) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/ezframe/route.rb', line 111

def get_class(keys)
  # EzLog.info "get_class: #{keys.inspect}"
  return nil unless keys
  keys = [ keys ] if keys.is_a?(String)
  klass = (%w[Ezframe] + keys.map { |k| k.to_s.to_camel }).join("::")
  # EzLog.info "get_class: #{klass}"
  if Object.const_defined?(klass)
    return Object.const_get(klass)
  else
    raise "get_class: undefined class: #{klass}"
  end
  return nil
end

.get_path(class_snake, route_h = nil) ⇒ Object

ページクラスの階層を辿る



78
79
80
81
82
83
84
85
# File 'lib/ezframe/route.rb', line 78

def get_path(class_snake, route_h = nil)
  route_h = Config[:route] unless route_h
  # EzLog.info "get_path: route_h=#{route_h}"
  @get_path_found_it = nil
  route =_scan_route(class_snake, route_h.deep_dup) 
  return route.reverse if route
  return nil
end

.make_method_name(base_name, method = "get") ⇒ Object



107
108
109
# File 'lib/ezframe/route.rb', line 107

def make_method_name(base_name, method = "get")
  return ["public", base_name, method.downcase].join("_")
end