Sha256: 834fa114eb2728fbe92b6fd5a3e0861156061e5355176e5f0bbc9459c2dc34f6

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 KB

Contents

module RGallery
  class Photo
    attr_reader :id, :sizing, :sources, :options

    def initialize id, options = {}
      @id = id
      self.sources = options.delete :sources
      @sizing = options.delete :sizing
      @options = options
    end

    # map [{src: 'banner-HD.jpeg', sizing: '2x'}, {src: 'banner-phone.jpeg', sizing: '100w'}]
    # into -> "banner-HD.jpeg 2x, banner-phone.jpeg 100w
    def srcset
      return '' unless sources_photos.kind_of? Array
      @srcset ||= source_photos.inject([]) do |res, photo| 
        res << [photo.id, photo.sizing].join(' ')
      end.join(',')
    end

    def srcset?
      !srcset.blank?
    end

    # A photo can contain a source set of other photos!
    def source_photos
      return [] unless sources.kind_of? Array
      @source_photos ||= sources.map do |source| 
        RGallery::Photo.new source.src, options.merge(:sizing => source.sizing)
      end
    end

    # make sure that sources are wrapped as Hashies to allow method access
    def sources= sources = []
      return unless sources.kind_of? Array
      @sources = sources.map{|source| Hashie::Mash.new source }
    end

    def filename
      id
    end

    def file_path
      "#{filename}.#{extension}"
    end

    def path
      file_path
    end

    def thumb
      path
    end

    def title
      'no title'
    end

    def alt
      'no alt'
    end

    def description
      'no description'
    end

    def extension
      options[:extension] || self.class.extension
    end

    class << self
      attr_writer :extension

      def extension
        @extension ||= :png
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails-gallery-0.3.0 lib/rails-gallery/rgallery/photo.rb