Debian Codegraph @

core/lib/diva_hacks/model.rb

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