Debian Codegraph @

plugin/skin/model/image.rb

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 require 'skin'
3
4 module Plugin::Skin
5 class Image < Diva::Model
6 extend Memoist
7
8 include Diva::Model::PhotoMixin
9
10 register :skin_image, name: 'skin image'
11
12 field.string :path, required: true
13
14 def self.[](path)
15 @store ||= Hash.new{|h,k| h[k] = new(path: k) } # path => Image
16 @store[path]
17 end
18
19 memoize def uri
20 Diva::URI.new(scheme: 'file'.freeze, path: path)
21 end
22
23 # 引数の寸法の GdkPixbuf::Pixbuf を返す。
24 # ==== Args
25 # [width:] Pixbufの幅(px)
26 # [height:] Pixbufの高さ(px)
27 # ==== Return
28 # [GdkPixbuf::Pixbuf] メモリキャッシュやファイルシステムから画像が見つかった場合
29 def pixbuf(width:, height:)
30 result = pixbuf_cache[[width, height].hash]
31 if result
32 result
33 else
34 width, height = width.to_i, height.to_i
35 pixbuf_cache_set(GdkPixbuf::Pixbuf.new(file: uri.path, width: width, height: height), width: width, height: height)
36 end
37 end
38 end
39 end