| 1 |
|
| 2 |
require_relative 'photo_variant' |
| 3 |
|
| 4 |
module Plugin::Photo |
| 5 |
像を扱うModel。 |
| 6 |
Variant)を含むことができ、それらを自動的に使い分ける。 |
| 7 |
class Photo < Diva::Model |
| 8 |
include Diva::Model::PhotoInterface |
| 9 |
register :photo, name: Plugin[:photo]._('画像') |
| 10 |
|
| 11 |
field.has :variants, [Diva::Model], required: true |
| 12 |
field.has :original, InnerPhoto |
| 13 |
field.uri :perma_link, required: true |
| 14 |
|
| 15 |
def self.photos |
| 16 |
@photos ||= TimeLimitedStorage.new(Integer, self) |
| 17 |
end |
| 18 |
|
| 19 |
を得る。 |
| 20 |
oかURIかわからないものをPhotoに変換するのに使える。 |
| 21 |
ンはこれを呼ばず、以下のページを参考にすること。 |
| 22 |
|
| 23 |
def self.[](uri) |
| 24 |
case uri |
| 25 |
when Diva::Model |
| 26 |
uri |
| 27 |
when URI, Addressable::URI, Diva::URI, String |
| 28 |
wrapped_uri = Diva::URI(uri) |
| 29 |
photos[wrapped_uri.to_s.hash] ||= wrap(wrapped_uri) |
| 30 |
end |
| 31 |
end |
| 32 |
|
| 33 |
にある場合、それを返す。 |
| 34 |
s_ の内容を元に作る。 |
| 35 |
|
| 36 |
or(後述) |
| 37 |
なるパーマリンク |
| 38 |
ついて |
| 39 |
対応するPhotoがまだ作られていない場合にだけ利用される。 |
| 40 |
下のキーを持つ。 |
| 41 |
利用されないので、重複していても良い(Symbol) |
| 42 |
画像の幅(px) |
| 43 |
像の高さ(px) |
| 44 |
うにリサイズされたかを示す値(Symbol) |
| 45 |
to Model又は画像のURL |
| 46 |
ーについて |
| 47 |
のいずれかの値。 |
| 48 |
。一つだけ含まれていること。 |
| 49 |
antを参照 |
| 50 |
def self.generate(seeds, perma_link:) |
| 51 |
cached = photos[perma_link.to_s.hash] |
| 52 |
return cached if cached |
| 53 |
orig, other = seeds.partition{|s| s[:policy] == :original } |
| 54 |
new(variants: other.map{|s| |
| 55 |
PhotoVariant.new(s.merge(photo: InnerPhoto[s[:photo]])) |
| 56 |
}, |
| 57 |
perma_link: perma_link, |
| 58 |
original: InnerPhoto[orig.first[:photo]]) |
| 59 |
end |
| 60 |
|
| 61 |
def self.wrap(model_or_uri) |
| 62 |
inner_photo = InnerPhoto[model_or_uri] |
| 63 |
new(variants: [], |
| 64 |
perma_link: inner_photo.perma_link, |
| 65 |
original: inner_photo) |
| 66 |
end |
| 67 |
|
| 68 |
def initialize(*params) |
| 69 |
super |
| 70 |
each_photos do |photo| |
| 71 |
self.class.photos[photo.uri.to_s.hash] = self |
| 72 |
end |
| 73 |
self.class.photos[uri.to_s.hash] = self |
| 74 |
end |
| 75 |
|
| 76 |
を引数にしてblockを呼び出す。 |
| 77 |
場合は、Enumeratorを返す |
| 78 |
|
| 79 |
クを渡した場合 |
| 80 |
Enumerator |
| 81 |
def each_photos(&block) |
| 82 |
if block_given? |
| 83 |
variants.each{|pv| block.(pv.photo) } |
| 84 |
self |
| 85 |
else |
| 86 |
variants.lazy.map(&:photo) |
| 87 |
end |
| 88 |
end |
| 89 |
|
| 90 |
def download(width: nil, height: nil, &partial_callback) |
| 91 |
if width && height |
| 92 |
larger_than(width: width, height: height) |
| 93 |
.download(width: width, height: height, &partial_callback) |
| 94 |
else |
| 95 |
maximum_original.download(width: width, height: height, &partial_callback) |
| 96 |
end |
| 97 |
end |
| 98 |
|
| 99 |
張し、内部でこのメソッドを呼ぶ。 |
| 100 |
ため空のメソッドを定義しておく。 |
| 101 |
def increase_read_count |
| 102 |
end |
| 103 |
|
| 104 |
def blob |
| 105 |
maximum_original.blob |
| 106 |
end |
| 107 |
|
| 108 |
さを上回るvariantを返す。 |
| 109 |
れているvariantのうち、最小のものを使う。 |
| 110 |
ウンロードされている画像がない場合は、条件を満たす最小のvariantを返す。 |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
Model |
| 116 |
def larger_than(width:, height:) |
| 117 |
largers = variants.select{|pv| pv.policy.to_sym == :fit && pv.width >= width && pv.height >= height }.sort_by(&:width) |
| 118 |
if largers.empty? |
| 119 |
maximum_original |
| 120 |
else |
| 121 |
(largers.find{|pv| pv.photo.completed? } || largers.first).photo |
| 122 |
end |
| 123 |
end |
| 124 |
|
| 125 |
のPhotoを返す。 |
| 126 |
fit policyのvariantのうち最大のものを返す。 |
| 127 |
|
| 128 |
のPhoto |
| 129 |
def maximum |
| 130 |
self.class.wrap(maximum_original) |
| 131 |
end |
| 132 |
|
| 133 |
private |
| 134 |
|
| 135 |
def maximum_original |
| 136 |
if original |
| 137 |
original |
| 138 |
else |
| 139 |
variant = variants.find{|v| v.policy.to_sym == :fit } |
| 140 |
if variant |
| 141 |
variant.photo |
| 142 |
else |
| 143 |
variants.sample.photo |
| 144 |
end |
| 145 |
end |
| 146 |
end |
| 147 |
end |
| 148 |
end |
| 149 |
|