Class: RGallery::Page

Inherits:
PhotoConfig show all
Includes:
Enumerable
Defined in:
lib/rails-gallery/rgallery/page.rb

Instance Attribute Summary

Attributes inherited from PhotoConfig

#options

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(photo_objs = [], options = {}) ⇒ Page

Returns a new instance of Page.



5
6
7
8
# File 'lib/rails-gallery/rgallery/page.rb', line 5

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

Class Method Details

.from_source(sources) ⇒ Object

a source is a hash of the form: ‘banner’ => [‘banner-HD’, sizing: ‘2x’, ‘banner-phone’, sizing: ‘100w’] see: add_photo_sources



13
14
15
16
17
18
19
# File 'lib/rails-gallery/rgallery/page.rb', line 13

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

Instance Method Details

#<<(photo_objs) ⇒ Object



21
22
23
24
# File 'lib/rails-gallery/rgallery/page.rb', line 21

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

#add_photo_sources(sources_hash) ⇒ Object



26
27
28
29
30
# File 'lib/rails-gallery/rgallery/page.rb', line 26

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

#add_photo_w_sources(source) ⇒ Object

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
39
40
41
# File 'lib/rails-gallery/rgallery/page.rb', line 32

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

#each(&block) ⇒ Object



53
54
55
# File 'lib/rails-gallery/rgallery/page.rb', line 53

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

#photo_objsObject



43
44
45
# File 'lib/rails-gallery/rgallery/page.rb', line 43

def photo_objs
  @photo_objs ||= []
end

#photosObject



47
48
49
# File 'lib/rails-gallery/rgallery/page.rb', line 47

def photos
  @photos ||= photo_objs.map {|obj| photo_class.new obj, options } 
end