Class: ReactManifest::TreeClassifier

Inherits:
Object
  • Object
show all
Defined in:
lib/react_manifest/tree_classifier.rb

Overview

Walks the ux/ directory tree and classifies subdirectories into:

- controller_dirs: immediate subdirs of ux/app/ (each gets a ux_<name>.js)
- shared_dirs:     everything else directly under ux/ (feeds ux_shared.js)

No hard-coded dir names — anything that is not app_dir is shared.

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Constructor Details

#initialize(config = ReactManifest.configuration) ⇒ TreeClassifier

Returns a new instance of TreeClassifier.



10
11
12
# File 'lib/react_manifest/tree_classifier.rb', line 10

def initialize(config = ReactManifest.configuration)
  @config = config
end

Instance Method Details

#classifyObject



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
# File 'lib/react_manifest/tree_classifier.rb', line 14

def classify
  controller_dirs = []
  shared_dirs     = []

  unless Dir.exist?(@config.abs_ux_root)
    warn "[ReactManifest] ux_root does not exist: #{@config.abs_ux_root}. " \
         "Create the directory and run `rails react_manifest:generate`."
    return Result.new(controller_dirs: [], shared_dirs: [])
  end

  begin
    Dir.children(@config.abs_ux_root).sort.each do |entry|
      full_path = File.join(@config.abs_ux_root, entry)
      next unless real_directory?(full_path)

      if entry == @config.app_dir
        begin
          Dir.children(full_path).sort.each do |ctrl_entry|
            ctrl_path = File.join(full_path, ctrl_entry)
            next unless real_directory?(ctrl_path)
            next if @config.ignore.include?(ctrl_entry)

            controller_dirs << {
              name: ctrl_entry,
              path: ctrl_path,
              bundle_name: "ux_#{ctrl_entry}"
            }
          end
        rescue Errno::EACCES => e
          warn "[ReactManifest] Permission denied reading #{full_path}: #{e.message}"
        end
      else
        shared_dirs << {
          name: entry,
          path: full_path
        }
      end
    end
  rescue Errno::EACCES => e
    warn "[ReactManifest] Permission denied reading ux_root #{@config.abs_ux_root}: #{e.message}"
    return Result.new(controller_dirs: [], shared_dirs: [])
  end

  Result.new(controller_dirs: controller_dirs, shared_dirs: shared_dirs)
end

#watched_dirsObject

Watch ux_root recursively so newly added controller directories are automatically detected without restarting the development server.



74
75
76
77
78
79
80
# File 'lib/react_manifest/tree_classifier.rb', line 74

def watched_dirs
  return [] unless Dir.exist?(@config.abs_ux_root)

  dirs = [@config.abs_ux_root]
  dirs << @config.abs_app_dir if Dir.exist?(@config.abs_app_dir)
  dirs
end