Module: RScale

Extended by:
UUID
Defined in:
lib/rscale/uuid.rb,
lib/rscale/errors.rb,
lib/rscale/format.rb,
lib/rscale/rscale.rb,
lib/rscale/processor.rb,
lib/rscale/configuration.rb

Defined Under Namespace

Modules: Processor, UUID Classes: ConfigError, Configuration, Format, FormatError, ProcessingError

Constant Summary collapse

@@config =
nil
@@formats =
{}

Class Method Summary collapse

Methods included from UUID

uuid, uuid_local, uuid_system

Class Method Details

.configObject

Get current RScale configuration



14
15
16
# File 'lib/rscale/rscale.rb', line 14

def self.config
  @@config
end

.configure {|@@config| ... } ⇒ Object

Configure RScale

Yields:



8
9
10
11
# File 'lib/rscale/rscale.rb', line 8

def self.configure
  @@config = Configuration.new if @@config.nil?
  yield @@config
end

.format(name) ⇒ Object

Add new format



19
20
21
22
23
24
25
26
# File 'lib/rscale/rscale.rb', line 19

def self.format(name)
  unless @@formats.key?(name)
    @@formats[name] = Format.new(name)
    yield @@formats[name] if block_given?
  else
    raise FormatError, "Format with name [#{name.to_s}] is already defined"
  end
end

.formatsObject

Get list of formats



29
30
31
# File 'lib/rscale/rscale.rb', line 29

def self.formats
  @@formats
end

.image_for(format, file) ⇒ Object

Generate thumbnails for the image format



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
59
60
61
62
# File 'lib/rscale/rscale.rb', line 34

def self.image_for(format, file)
  if @@formats.key?(format.to_sym)
    fmt = @@formats[format.to_sym]
    file_info = File.get_info(file)
    url = fmt.url
    image = {}
    
    options = file_info.merge(:format => fmt.name)
    
    options[:time] = Time.now.to_i unless url[':time'].nil?
    options[:md5] = Digest::MD5.filedigest(file) unless url[':md5'].nil?
    unless url[':uuid'].nil?
      options[:uuid] = uuid
      options[:uuid_dir] = "#{options[:uuid][0,2]}/#{options[:uuid][2,2]}"
    end
    
    fmt.styles.each_pair do |k,v|
      url = fmt.url.placeholders(options.merge(v))
      file_out = @@config.public + "/#{url}"
      if Processor.process(file, file_out, v)
        image[k] = url
      end
    end
    
    return image.size == fmt.styles.size ? image : nil
  else
    raise ArgumentError, "Format #{format.to_s} cannot be found!"
  end
end