Class: KCommercial::Resources::ImagesIndexesGenerator

Inherits:
IndexesGenerator show all
Defined in:
lib/KCommercialPipeline/core/resource/bundle/images_generator.rb

Instance Attribute Summary

Attributes inherited from IndexesGenerator

#asset_objects, #root_path

Instance Method Summary collapse

Methods inherited from IndexesGenerator

#generate!, #initialize

Constructor Details

This class inherits a constructor from KCommercial::Resources::IndexesGenerator

Instance Method Details

#index_resourcesObject



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/KCommercialPipeline/core/resource/bundle/images_generator.rb', line 47

def index_resources
  hash = super
  asset_objects.each do |asset|
    hash[asset.name] = asset.trait_rows.map do |row|
      {
        :traits => row.traits,
        :resource => row.resource.filename_without_scale
      }
    end
  end
  hash
end

#indexes_filenameObject



60
61
62
# File 'lib/KCommercialPipeline/core/resource/bundle/images_generator.rb', line 60

def indexes_filename
  'images'
end

#write_filesObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/KCommercialPipeline/core/resource/bundle/images_generator.rb', line 10

def write_files
  xcassets = XCAssets::XCAssets.new('Images')
  asset_objects.each do |asset|
    rows = asset.trait_rows
    row_keys = rows.map {|row| row.resource}.uniq
    row_keys.each do |image_path|
      name = image_path.filename_without_scale
      imageset = XCAssets::ImageSet.new(name)
      xcassets.add_imageset(imageset)
      scale_paths = ['1x', '2x', '3x'].map { |scale|
        scale_path = image_path.parent.join("#{name}@#{scale}#{image_path.extname}")
        scale_path = image_path.parent.join("#{name}@#{scale.upcase}#{image_path.extname}") unless scale_path.exist?
        unless scale_path.exist?
          scale_path = nil
        end
        scale_path
      }
      if scale_paths.compact.count == 0
        imageset.scale2_image.resource = image_path
      else
        if scale_paths[0]
          imageset.scale1_image.resource = scale_paths[0]
        end
        if scale_paths[1]
          imageset.scale2_image.resource = scale_paths[1]
        end
        if scale_paths[2]
          imageset.scale3_image.resource = scale_paths[2]
        end
      end
    end
  end
  root_path.mkpath unless root_path.exist?
  xcassets.save(root_path)

end