1
(defun rails-lib-layout:keymap (type)
2
  (let* ((name (capitalize (substring (symbol-name type) 1)))
3
         (map (make-sparse-keymap))
4
         (menu (make-sparse-keymap)))
5
    (when type
6
      (define-keys menu
7
        ([goto-lib]      '(menu-item "Go to Lib"
8
                                     rails-lib-layout:switch-to-lib
9
                                     :enable (and (not (eq (rails-core:buffer-type) :lib))
10
                                                  (rails-core:lib-exist-p (rails-core:current-lib)))))
11
        ([goto-rspec]    '(menu-item "Go to RSpec"
12
                                     rails-lib-layout:switch-to-rspec-lib
13
                                     :enable (and (not (eq (rails-core:buffer-type) :rspec-lib))
14
                                                  (rails-core:rspec-lib-exist-p (rails-core:current-lib))))))
15
      (define-keys map
16
        ((rails-key "l")         'rails-lib-layout:switch-to-lib)
17
        ((rails-key "r")         'rails-lib-layout:switch-to-unit-test)
18
        ([menu-bar rails-lib-layout] (cons name menu))))
19
    map))
20
21
(defun rails-lib-layout:switch-to (type)
22
  (let* ((name (capitalize (substring (symbol-name type) 1)))
23
         (lib (rails-core:current-lib))
24
         (item (if lib lib))
25
         (item (case type
26
                 (:rspec-lib (rails-core:rspec-lib-file item))
27
                 (:lib (rails-core:lib-file lib)))))
28
    (if item
29
        (let ((file (rails-core:file item)))
30
          (if (file-exists-p file)
31
              (progn
32
                (find-file file)
33
                (message (format "%s: %s" (substring (symbol-name type) 1) item)))
34
            (message "File %s not exists" file)))
35
      (message "%s not found" name))))
36
37
(defun rails-lib-layout:switch-to-rspec-lib () (interactive) (rails-lib-layout:switch-to :rspec-lib))
38
(defun rails-lib-layout:switch-to-lib () (interactive) (rails-lib-layout:switch-to :lib))
39
40
(defun rails-lib-layout:menu ()
41
  (interactive)
42
  (let* ((item (list))
43
         (type (rails-core:buffer-type))
44
         (title (capitalize (substring (symbol-name type) 1)))
45
         (lib (rails-core:current-lib)))
46
    (when lib
47
      (unless (eq type :rspec-lib)
48
        (add-to-list 'item (cons "RSpec" :rspec-lib)))
49
      (unless (eq type :lib)
50
        (add-to-list 'item (cons "Lib" :lib))))
51
    (when item
52
      (setq item
53
            (rails-core:menu
54
             (list (concat title " " lib)
55
                   (cons "Please select.."
56
                         item))))
57
      (typecase item
58
        (symbol (rails-lib-layout:switch-to item))
59
        (string (rails-core:find-file-if-exist item))))))
60
61
(provide 'rails-lib-layout)