Class: SmallCage::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/smallcage/loader.rb

Overview

.smc file loader

Constant Summary collapse

DEFAULT_TEMPLATE =
'default'
DIR_PROP_FILE =
'_dir.smc'
LOCAL_PROP_FILE =
'_local.smc'
MAX_DEPTH =
100

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ Loader

Returns a new instance of Loader.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/smallcage/loader.rb', line 15

def initialize(target)
  target = target.to_s.strip.chomp('/')
  if target.to_s =~ %r{(.*/|\A)_smc/templates/((?:.+/)?(?:[^/]+))\.rhtml\z}
    target = Regexp.last_match[1]
    target = '.' if target == ''
    @target_template = Regexp.last_match[2]
  end
  @target = real_target(Pathname.new(target))
  @dir_target = @target.directory?
  @root = self.class.find_root(@target) # absolute
  @templates_dir = @root + '_smc/templates'
  @helpers_dir = @root + '_smc/helpers'
  @filters_dir = @root + '_smc/filters'
  @erb_base = load_erb_base
  @filters = load_filters
end

Instance Attribute Details

#dir_targetObject (readonly)

Returns the value of attribute dir_target.



13
14
15
# File 'lib/smallcage/loader.rb', line 13

def dir_target
  @dir_target
end

#erb_baseObject (readonly)

Returns the value of attribute erb_base.



13
14
15
# File 'lib/smallcage/loader.rb', line 13

def erb_base
  @erb_base
end

#rootObject (readonly)

Returns the value of attribute root.



13
14
15
# File 'lib/smallcage/loader.rb', line 13

def root
  @root
end

#targetObject (readonly)

Returns the value of attribute target.



13
14
15
# File 'lib/smallcage/loader.rb', line 13

def target
  @target
end

#target_templateObject (readonly)

Returns the value of attribute target_template.



13
14
15
# File 'lib/smallcage/loader.rb', line 13

def target_template
  @target_template
end

Class Method Details

.find_root(path, depth = MAX_DEPTH) ⇒ Object

return root dir Pathname object.



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/smallcage/loader.rb', line 33

def self.find_root(path, depth = MAX_DEPTH)
  fail "Not found: #{path}" unless path.exist?
  d = path.realpath
  d = d.parent if d.file?
  loop do
    return d if d.join('_smc').directory?
    break if d.root? || (depth -= 1) <= 0
    d = d.parent
  end
  fail "Root not found: #{path}"
end

Instance Method Details

#each_not_smc_fileObject



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/smallcage/loader.rb', line 175

def each_not_smc_file
  if @target.directory?
    path = Pathname.new(@target)
    Dir.chdir(@target) do
      Dir.glob('**/*').sort.each do |f|
        f = path + f
        next if f.directory?
        next if f.to_s =~ %r{/_smc/}
        next if f.to_s =~ /\.smc$/
        yield DocumentPath.new(@root, path + f)
      end
    end
  else
    return if @target.to_s =~ %r{/_smc/}
    return if @target.to_s =~ /\.smc$/
    yield DocumentPath.new(@root, @target)
  end
end

#each_smc_fileObject



162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/smallcage/loader.rb', line 162

def each_smc_file
  if @target.directory?
    path = Pathname.new(@target)
    Dir.chdir(@target) do
      Dir.glob('**/*.smc').sort.each do |f|
        yield path + f
      end
    end
  else
    yield @target
  end
end

#each_smc_objObject



150
151
152
153
154
155
156
157
158
159
160
# File 'lib/smallcage/loader.rb', line 150

def each_smc_obj
  each_smc_file do |path|
    next if path.directory?
    next if path.basename.to_s == DIR_PROP_FILE
    next if path.basename.to_s == LOCAL_PROP_FILE
    obj = load(path)
    next if @target_template && obj['template'] != @target_template

    yield obj
  end
end

#each_smc_obj_using_target_template(list, &block) ⇒ Object

When the target is template, try to find source using the template.



195
196
197
198
199
200
201
202
203
204
# File 'lib/smallcage/loader.rb', line 195

def each_smc_obj_using_target_template(list, &block)
  return each_smc_obj(&block) unless @target_template

  list.filter_by_template(@target_template).each do |path|
    path = @root + path[1..-1]
    next unless path.file?
    obj = load(path)
    yield obj
  end
end

#filters(name) ⇒ Object



236
237
238
# File 'lib/smallcage/loader.rb', line 236

def filters(name)
  @filters[name].nil? ? [] : @filters[name]
end

#load(path) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/smallcage/loader.rb', line 45

def load(path)
  fail "Not found: #{path}" unless path.exist?

  docpath = DocumentPath.new(@root, path)

  if path.file?
    return load_smc_file(docpath)
  else
    return load_dir_prop(docpath)
  end
end

#load_dirs(path) ⇒ Object



133
134
135
136
137
138
139
140
141
142
# File 'lib/smallcage/loader.rb', line 133

def load_dirs(path)
  result = []
  loop do
    path = path.parent
    result.unshift load(path)
    break if path.join('_smc').directory?
    fail 'Root directory not found!' if path.root?
  end
  result
end

#template_path(name) ⇒ Object



144
145
146
147
148
# File 'lib/smallcage/loader.rb', line 144

def template_path(name)
  result = @templates_dir + "#{name}.rhtml"
  return nil unless result.file?
  result
end