Class: S3Rails::Resolver

Inherits:
ActionView::PathResolver
  • Object
show all
Includes:
Singleton
Defined in:
lib/s3_rails/resolver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResolver

Returns a new instance of Resolver.



6
7
8
9
# File 'lib/s3_rails/resolver.rb', line 6

def initialize()
  super
  @s3 = S3Rails::S3.new('config/s3_rails.yml')
end

Instance Attribute Details

#s3Object

Returns the value of attribute s3.



4
5
6
# File 'lib/s3_rails/resolver.rb', line 4

def s3
  @s3
end

Instance Method Details

#build_query(path, details) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/s3_rails/resolver.rb', line 11

def build_query(path, details)
  exts = EXTENSIONS.map do |ext, prefix|
    "{" + 
    details[ext].compact.uniq.map { |e| "#{prefix}#{e}," }.join +
    "}"
  end.join

  path.to_s + exts
end

#query(path, details, formats) ⇒ Object



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
# File 'lib/s3_rails/resolver.rb', line 21

def query(path, details, formats)
  query = build_query(path, details)

  if File.exists?('tmp/reload_s3.txt') &&
      @s3.last_load < File.mtime('tmp/reload_s3.txt')
    reload
  end

  # objects = @s3.bucket.objects.with_prefix(path.prefix).select do |obj|
  #   File.fnmatch query, obj.key, File::FNM_EXTGLOB
  # end

  objects = @s3.objects.select do |key, obj|
    File.fnmatch query, key, File::FNM_EXTGLOB
  end

  objects.map do |key, obj|
    template = "s3/#{@s3.bucket_name}/#{obj.key}"
    handler, format, variant = 
      extract_handler_and_format_and_variant(template, formats)
    contents = obj.read

    ActionView::Template.new(contents, template, handler,
      :virtual_path => path.virtual,
      :format       => format,
      :variant      => variant,
      :updated_at   => obj.last_modified
    )
  end
end

#reloadObject



52
53
54
55
# File 'lib/s3_rails/resolver.rb', line 52

def reload
  @s3.load_cache
  clear_cache
end