Class: OPDS::Support::LinkSet
- Inherits:
-
Object
- Object
- OPDS::Support::LinkSet
- Includes:
- Enumerable
- Defined in:
- lib/opds/support/linkset.rb
Overview
use a true Set to provide storage
Set of links.
It provides ways to query and filter the set
Instance Method Summary collapse
-
#[](k) ⇒ Object
Query the set by rel value.
-
#[]=(k, v) ⇒ Object
Add a link to the set.
-
#by(type) ⇒ Object
Collection indexed by given type.
-
#each(&block) ⇒ Object
iterate through the set.
-
#first ⇒ Link
First link in store.
-
#initialize(browser = OPDS::Support::Browser.new) ⇒ LinkSet
constructor
A new instance of LinkSet.
- #inspect ⇒ Object
-
#last ⇒ Link
Last link in store.
-
#link_rel(k) ⇒ Object
Find first link rel corresponding to the query.
-
#link_text(k) ⇒ Object
Find first link text corresponding to the query.
-
#link_type(k) ⇒ Object
Find first link type corresponding to the query.
-
#link_url(k) ⇒ Object
Find first link url corresponding to the query.
-
#links ⇒ Array
All links.
-
#push(rel, link, text = nil, type = nil, price = nil, currency = nil) ⇒ Object
Push a link to the set.
- #push_facet(link, text = nil, type = nil, facet_group = nil, active_facet = nil, count = nil) ⇒ Object
-
#push_link(link) ⇒ Object
Push an existing link to the set.
-
#rels ⇒ Array
All rels.
-
#size ⇒ Integer
Size of the set.
-
#texts ⇒ Array
All titles.
- #to_yaml ⇒ Object
Constructor Details
#initialize(browser = OPDS::Support::Browser.new) ⇒ LinkSet
Returns a new instance of LinkSet.
94 95 96 97 98 99 100 101 |
# File 'lib/opds/support/linkset.rb', line 94 def initialize(browser=OPDS::Support::Browser.new) @browser=browser @rel_store=Hash.new @txt_store=Hash.new @lnk_store=Hash.new @typ_store=Hash.new @store=[] end |
Instance Method Details
#[](k) ⇒ Object
Query the set by rel value
127 128 129 |
# File 'lib/opds/support/linkset.rb', line 127 def [](k) remap(@rel_store[k]) end |
#[]=(k, v) ⇒ Object
Add a link to the set
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/opds/support/linkset.rb', line 106 def []=(k,v) link=nil if v.size > 6 link=Facet.new([k]+v,@browser) else link=Link.new([k]+v,@browser) end @store.push link i=@store.size-1 @rel_store[k]=[] unless @rel_store[k] @rel_store[k].push i @txt_store[v[1]]=[] unless @txt_store[v[1]] @txt_store[v[1]].push i @lnk_store[v.first]=[] unless @lnk_store[v.first] @lnk_store[v.first].push i @typ_store[v.last]=[] unless @typ_store[v.last] @typ_store[v.last].push i end |
#by(type) ⇒ Object
Collection indexed by given type
202 203 204 |
# File 'lib/opds/support/linkset.rb', line 202 def by(type) Hash[collection(type).map{|k,v| [k,remap(v)]}] end |
#each(&block) ⇒ Object
iterate through the set
132 133 134 |
# File 'lib/opds/support/linkset.rb', line 132 def each(&block) @store.each(&block) end |
#first ⇒ Link
Returns First link in store.
226 227 228 |
# File 'lib/opds/support/linkset.rb', line 226 def first @store.first end |
#inspect ⇒ Object
221 222 223 |
# File 'lib/opds/support/linkset.rb', line 221 def inspect @store.inspect end |
#last ⇒ Link
Returns Last link in store.
231 232 233 |
# File 'lib/opds/support/linkset.rb', line 231 def last @store.last end |
#link_rel(k) ⇒ Object
Find first link rel corresponding to the query
170 171 172 173 174 |
# File 'lib/opds/support/linkset.rb', line 170 def link_rel(k) ty,v=k.first t=remap(collection(ty)[v]) t.first[0] unless t.nil? end |
#link_text(k) ⇒ Object
Find first link text corresponding to the query
179 180 181 182 183 |
# File 'lib/opds/support/linkset.rb', line 179 def link_text(k) ty,v=k.first t=remap(collection(ty)[v]) t.first[2] unless t.nil? end |
#link_type(k) ⇒ Object
Find first link type corresponding to the query
188 189 190 191 192 |
# File 'lib/opds/support/linkset.rb', line 188 def link_type(k) ty,v=k.first t=remap(collection(ty)[v]) t.first[3] unless t.nil? end |
#link_url(k) ⇒ Object
Find first link url corresponding to the query
161 162 163 164 165 |
# File 'lib/opds/support/linkset.rb', line 161 def link_url(k) ty,v=k.first t=remap(collection(ty)[v]) t.first[1] unless t.nil? end |
#links ⇒ Array
Returns all links.
207 208 209 |
# File 'lib/opds/support/linkset.rb', line 207 def links @lnk_store.keys end |
#push(rel, link, text = nil, type = nil, price = nil, currency = nil) ⇒ Object
Push a link to the set
142 143 144 145 146 |
# File 'lib/opds/support/linkset.rb', line 142 def push(rel,link,text=nil,type=nil, price=nil, currency=nil) tab=[link,text,type] tab+=[price.to_f,currency] unless price.nil? self[rel]=tab end |
#push_facet(link, text = nil, type = nil, facet_group = nil, active_facet = nil, count = nil) ⇒ Object
148 149 150 |
# File 'lib/opds/support/linkset.rb', line 148 def push_facet(link,text=nil,type=nil,facet_group=nil,active_facet=nil,count=nil) self['http://opds-spec.org/facet']=[link,text,type,nil,nil,facet_group,active_facet,count] end |
#push_link(link) ⇒ Object
Push an existing link to the set
154 155 156 |
# File 'lib/opds/support/linkset.rb', line 154 def push_link(link) @store.push link if link.is_a?Link end |
#rels ⇒ Array
Returns all rels.
212 213 214 |
# File 'lib/opds/support/linkset.rb', line 212 def rels @rel_store.keys end |
#size ⇒ Integer
Size of the set
196 197 198 |
# File 'lib/opds/support/linkset.rb', line 196 def size @store.size end |
#texts ⇒ Array
Returns all titles.
217 218 219 |
# File 'lib/opds/support/linkset.rb', line 217 def texts @txt_store.keys end |
#to_yaml ⇒ Object
235 236 237 |
# File 'lib/opds/support/linkset.rb', line 235 def to_yaml @store.to_yaml end |