Module: Rabbit::Theme::Searcher

Included in:
Applier
Defined in:
lib/rabbit/theme/searcher.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

._collect_theme(path, entry_classes, converter = nil, &block) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/rabbit/theme/searcher.rb', line 129

def _collect_theme(path, entry_classes, converter=nil, &block)
  converter ||= "theme_dir"
  themes = []
  theme_names = {}
  path.each do |dir|
    base_name = __send__(converter, dir)
    if File.directory?(base_name)
      Dir.foreach(base_name) do |theme|
        next if /\A..?\z/ =~ theme
        next if theme_names.has_key?(theme)
        theme_dir = File.join(File.expand_path(base_name), theme)
        entry_classes.each do |entry_class|
          entry = entry_class.new(@logger, theme_dir, theme)
          if entry.available?
            block.call(entry) if block
            themes << entry
            theme_names[theme] = true
            break
          end
        end
      end
    end
  end
  themes.sort
end

.collect_all_theme(&block) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/rabbit/theme/searcher.rb', line 97

def collect_all_theme(&block)
  theme_names = {}
  themes = []
  callback = Proc.new do |entry|
    unless theme_names.has_key?(entry.name)
      theme_names[entry.name] = true
      themes << entry
      block.call(entry) if block
    end
  end
  collect_image_theme(&callback)
  collect_theme(&callback)
  themes.sort
end

.collect_image_theme(&block) ⇒ Object



116
117
118
119
# File 'lib/rabbit/theme/searcher.rb', line 116

def collect_image_theme(&block)
  _collect_theme(image_load_path, [ImageFileEntry, ImageDirectoryEntry],
                 "image_dir", &block)
end

.collect_theme(&block) ⇒ Object



112
113
114
# File 'lib/rabbit/theme/searcher.rb', line 112

def collect_theme(&block)
  _collect_theme(theme_load_path, [FileEntry, DirectoryEntry], &block)
end

.find_file(target, themes = nil) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rabbit/theme/searcher.rb', line 84

def find_file(target, themes=nil)
  themes ||= @theme_stack + @image_entries
  found_entry = themes.find do |entry|
    entry.have_file?(target)
  end
  if found_entry.nil?
    names = themes.collect {|entry| entry.name}
    raise LoadError,
          "can't find file in themes #{names.inspect}: #{target}."
  end
  found_entry.full_path(target)
end

.find_theme(theme_name = name, only_image = false) ⇒ Object



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
76
77
78
79
80
81
82
# File 'lib/rabbit/theme/searcher.rb', line 43

def find_theme(theme_name=name, only_image=false)
  if theme_name == "."
    if only_image
      entry = ImageDirectoryEntry.new(@logger, ".", ".")
    else
      entry = DirectoryEntry.new(@logger, ".", ".")
    end
    return entry if entry.available?
  end

  unless only_image
    entry = FileEntry.new(@logger, base_directory, theme_name)
    return entry if entry.available?
  end

  if only_image
    collector = "collect_image_theme"
  else
    collector = "collect_all_theme"
  end
  found_entry = nil
  __send__(collector) do |entry|
    if theme_name == entry.name
      found_entry = entry
      break
    end
  end

  if found_entry.nil?
    if only_image
      entry = ImageGemEntry.new(@logger, theme_name)
    else
      entry = GemEntry.new(@logger, theme_name)
    end
    return entry if entry.available?
    raise LoadError, "can't find theme: #{theme_name}."
  end

  found_entry
end

.image_dir(base_dir) ⇒ Object



39
40
41
# File 'lib/rabbit/theme/searcher.rb', line 39

def image_dir(base_dir)
  File.join(base_dir, 'rabbit', 'image')
end

.image_load_pathObject



125
126
127
# File 'lib/rabbit/theme/searcher.rb', line 125

def image_load_path
  Config::IMAGE_PATH + $LOAD_PATH
end

.theme_dir(base_dir) ⇒ Object



35
36
37
# File 'lib/rabbit/theme/searcher.rb', line 35

def theme_dir(base_dir)
  File.join(base_dir, 'rabbit', 'theme')
end

.theme_load_pathObject



121
122
123
# File 'lib/rabbit/theme/searcher.rb', line 121

def theme_load_path
  $LOAD_PATH
end

Instance Method Details

#add_image_path(name) ⇒ Object Also known as: add_theme_path



27
28
29
# File 'lib/rabbit/theme/searcher.rb', line 27

def add_image_path(name)
  @image_entries << find_theme(name, true)
end

#in_theme(entry) ⇒ Object



20
21
22
23
24
25
# File 'lib/rabbit/theme/searcher.rb', line 20

def in_theme(entry)
  push_theme(entry)
  yield(entry)
ensure
  pop_theme
end

#initialize(*args, &blocks) ⇒ Object



6
7
8
9
10
# File 'lib/rabbit/theme/searcher.rb', line 6

def initialize(*args, &blocks)
  @theme_stack = []
  @image_entries = []
  super
end

#pop_themeObject



16
17
18
# File 'lib/rabbit/theme/searcher.rb', line 16

def pop_theme
  @theme_stack.pop
end

#push_theme(entry) ⇒ Object



12
13
14
# File 'lib/rabbit/theme/searcher.rb', line 12

def push_theme(entry)
  @theme_stack.push(entry)
end