Sha256: 9282ef451571afe2045f71fb1f9e3210c6dc1206e7132768c448afb2abc03290

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

module RGallery
  class Page < PhotoConfig
    include Enumerable

    def initialize photo_ids = [], options = {}
      @photo_ids = photo_ids
      super options
    end

    # a source is a hash of the form:
    # 'banner' => [{src: 'banner-HD', sizing: '2x'}, {src: 'banner-phone', sizing: '100w'}]
    # see: add_photo_sources
    def self.from_source sources
      page = self.create sources.keys, options

      @photos ||= sources.map do |key, srclist| 
        photo_class.new key, options.merge(:sources => srclist)
      end
    end

    def << photo_ids
      @photo_ids ||= []
      @photo_ids += [photo_ids].flatten
    end

    def add_photo_sources sources_hash
      sources_hash.each do |source|
        add_photo_w_sources source
      end
    end

    def add_photo_w_sources source
      raise ArgumentError, "Must be a hash, was: #{source}" unless source.kind_of? Hash
      key = source.keys.first
      srclist = source.values.first
      raise ArgumentError, "Hash value must be an Array, was: #{srclist}" unless srclist.kind_of? Array

      self.send :<<, key
      @photos ||= []
      @photos << photo_class.new(key, options.merge(:sources => srclist))
    end

    def photo_ids
      @photo_ids ||= []
    end   

    def photos
      @photos ||= photo_ids.map {|id| photo_class.new id, options } 
    end   

    delegate :empty?, to: :photos 

    def each &block
      photos.each {|photo| yield photo }
    end    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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