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



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/rabbit/theme/searcher.rb', line 136

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

.absolute_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/rabbit/theme/searcher.rb', line 100

def absolute_path?(path)
  Pathname.new(path).absolute?
end

.collect_all_theme(&block) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/rabbit/theme/searcher.rb', line 104

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



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

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

.collect_theme(&block) ⇒ Object



119
120
121
# File 'lib/rabbit/theme/searcher.rb', line 119

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

.find_file(target, themes = nil) ⇒ Object



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

def find_file(target, themes=nil)
  return target if absolute_path?(target)
  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



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
83
84
# File 'lib/rabbit/theme/searcher.rb', line 45

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



41
42
43
# File 'lib/rabbit/theme/searcher.rb', line 41

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

.image_load_pathObject



132
133
134
# File 'lib/rabbit/theme/searcher.rb', line 132

def image_load_path
  Config::IMAGE_PATH + $LOAD_PATH
end

.theme_dir(base_dir) ⇒ Object



37
38
39
# File 'lib/rabbit/theme/searcher.rb', line 37

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

.theme_load_pathObject



128
129
130
# File 'lib/rabbit/theme/searcher.rb', line 128

def theme_load_path
  $LOAD_PATH
end

Instance Method Details

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



29
30
31
# File 'lib/rabbit/theme/searcher.rb', line 29

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

#in_theme(entry) ⇒ Object



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

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

#initialize(*args, &blocks) ⇒ Object



8
9
10
11
12
# File 'lib/rabbit/theme/searcher.rb', line 8

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

#pop_themeObject



18
19
20
# File 'lib/rabbit/theme/searcher.rb', line 18

def pop_theme
  @theme_stack.pop
end

#push_theme(entry) ⇒ Object



14
15
16
# File 'lib/rabbit/theme/searcher.rb', line 14

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