Sha256: 4bf5a2c0050ac8ed6575f7729f414337bfb824b660a864b04deecc9ba6898223

Contents?: true

Size: 1.85 KB

Versions: 1

Compression:

Stored size: 1.85 KB

Contents

require 'picturama'

module Picturama
  class Album
    
    def initialize(args)
      @folder = args[:folder]
      @thumbnails = "#{args[:folder]}/thumbnails"
      @resized = "#{args[:folder]}/resized"
      @info = "#{args[:folder]}/.info.yml"
    end

    def pictures(order = true)
      if File.directory?(@folder)
        list = []
        filter = "#{@folder}/*.{#{Picturama::config['allowed_formats'].join(',')}}"
        dir_list = generate_sorting(filter)
        dir_list.each do |picture|
          list.push(Picturama::Picture.new(picture))
        end
        unless order
          list.reverse!
        end
        list
      end
    end

    def folder
      File.basename(@folder)
    end

    def name
      if info.nil?
        name!
      else
        info['album']['title']
      end
    end

    def name!
      folder.humanize
    end

    def slug
      name!.to_url
    end

    def count_pictures
      pictures.size
    end

    def has_thumbnails?
      File.directory?(@thumbnails)
    end

    def has_resized?
      File.directory?(@resized)
    end

    def has_broken_thumbnails?
      thumbnails_found = Dir["#{@thumbnails}/*.{#{Picturama::config['allowed_formats'].join(',')}}"].size
      !has_thumbnails? || thumbnails_found < count_pictures
    end

    def init_thumbnails
      unless has_thumbnails?
        FileUtils.mkdir_p "#{@thumbnails}"
      end
    end

    def init_resized
      unless has_resized?
        FileUtils.mkdir_p "#{@resized}"
      end
    end

    def info
      if File.exists?(@info)
        YAML.load_file(@info)
      end
    end

    protected

    def generate_sorting(filter)
      if (filter == :size)
        dir_list = Dir[filter].sort_by{ |filename| File.size("#{filename}") }
      else
        dir_list = Dir[filter].sort_by{ |filename| File.ctime("#{filename}") }
      end
      dir_list
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
picturama-0.0.3 lib/picturama/album.rb