1 2 3 4 5 6 7 8 9 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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
# -*- coding: utf-8 -*-
require_relative 'model/identity'
require_relative 'model/memory'
module Mikutter::DivaHacks::Model
# Entityのリストを返す。
# ==== Return
# Diva::Entity::BlankEntity
def links
@entity ||= self.class.entity_class.new(self)
end
alias :entity :links
end
module Mikutter::DivaHacks::ModelExtend
extend Gem::Deprecate
# Modelの情報を設定する。
# このメソッドを呼ぶと、他のプラグインがこのModelを見つけることができるようになるので、
# 抽出タブの抽出条件が追加されたり、設定で背景色が指定できるようになる
# ==== Args
# [new_slug] Symbol
# [name:] String Modelの名前
# [reply:] bool このModelに、宛先が存在するなら真
# [myself:] bool このModel、自分のアカウントによって作成できるなら真
# [timeline:] bool 真ならタイムラインに表示することができる
def register(new_slug,
name: new_slug.to_s,
reply: true,
myself: true,
timeline: false
)
@slug = new_slug.to_sym
spec = @spec = Diva::ModelSpec.new(@slug,
name.to_s.freeze,
!!reply,
!!myself,
!!timeline
).freeze
plugin do
filter_retrievers do |retrievers|
retrievers << spec
[retrievers]
end
end
end
def plugin(&block)
if not @slug
raise Diva::DivaError, "`#{self}'.slug is not set."
end
Plugin.create(:"retriever_model_#{@slug}", &block)
end
# Entityクラスを設定する。
# ==== Args
# [klass] Class 新しく設定するEntityクラス
# ==== Return
# [Class] セットされた(されている)Entityクラス
def entity_class(klass=nil)
if klass
@entity_class = klass
else
@entity_class ||= Diva::Entity::BlankEntity
end
end
# あるURIが、このModelを示すものであれば真を返す条件 _condition_ を設定する。
# _condition_ === uri が実行され、真を返せばそのURIをこのModelで取り扱えるということになる
# ==== Args
# [condition] 正規表現など、URIにマッチするもの
# ==== Return
# self
# ==== Block
# 実際にURIが指し示すリソースの内容を含んだModelを作って返す
# ===== Args
# [uri] URI マッチしたURI
# ===== Return
# [Delayer::Deferred::Deferredable]
# ネットワークアクセスを行って取得するなど取得に時間がかかる場合
# [self]
# すぐにModelを生成できる場合、そのModel
# ===== Raise
# [Diva::ModelNotFoundError] _uri_ に対応するリソースが見つからなかった
def handle(condition, &block) # :yield: uri
model_slug = self.slug
plugin do
if condition.is_a? Regexp
filter_model_of_uri do |uri, models|
if condition =~ uri.to_s
models << model_slug
end
[uri, models]
end
else
filter_model_of_uri do |uri, models|
if condition === uri
models << model_slug
end
[uri, models]
end
end
end
if block
define_singleton_method(:find_by_uri, &block)
end
end
# まだそのレコードのインスタンスがない場合、それを生成して返します。
def new_ifnecessary(hash)
type_strict hash => tcor(self, Hash)
result_strict(self) do
case hash
when self
hash
when Hash
self.new(hash)
else
raise ArgumentError.new("incorrect type #{hash.class} #{hash.inspect}")
end
end
end
#deprecate :new_ifnecessary, 'new', 2018, 2
def rewind(args)
type_strict args => Hash
result_strict(:merge){ new_ifnecessary(args) }.merge(args)
end
#deprecate :rewind, 'new', 2018, 2
end
module Diva
class Model
extend Mikutter::DivaHacks::ModelExtend
include Mikutter::DivaHacks::Model
end
RetrieverError = DivaError
deprecate_constant :RetrieverError
end
|