Class: OPDS::Support::LinkSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/opds/support/linkset.rb

Overview

TODO:

use a true Set to provide storage

Set of links.

It provides ways to query and filter the set

Instance Method Summary collapse

Constructor Details

#initialize(browser = OPDS::Support::Browser.new) ⇒ LinkSet

Returns a new instance of LinkSet.

Parameters:

  • browser (OPDS::Support::Browser) (defaults to: OPDS::Support::Browser.new)

    an optional compatible browser to use



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

Parameters:

  • k (String)

    rel value where to add the link

  • v (Array)

    remainder of link structure



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

Parameters:

  • in (Symbol)

    (:link,:rel,:txt,: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

#firstLink

Returns First link in store.

Returns:

  • (Link)

    First link in store



226
227
228
# File 'lib/opds/support/linkset.rb', line 226

def first 
	@store.first
end

#inspectObject



221
222
223
# File 'lib/opds/support/linkset.rb', line 221

def inspect
	@store.inspect
end

#lastLink

Returns Last link in store.

Returns:

  • (Link)

    Last link in store



231
232
233
# File 'lib/opds/support/linkset.rb', line 231

def last
	@store.last
end

Find first link rel corresponding to the query

Examples:

Query :

{:rel => "related" }


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

Find first link text corresponding to the query

Examples:

Query :

{:rel => "related" }


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

Find first link type corresponding to the query

Examples:

Query :

{:rel => "related" }


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

Find first link url corresponding to the query

Examples:

Query :

{:rel => "related" }


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

Returns all links.

Returns:

  • (Array)

    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 an existing link to the set

Parameters:

  • kink (Link)

    to add



154
155
156
# File 'lib/opds/support/linkset.rb', line 154

def push_link(link)
	@store.push link if link.is_a?Link
end

#relsArray

Returns all rels.

Returns:

  • (Array)

    all rels



212
213
214
# File 'lib/opds/support/linkset.rb', line 212

def rels
	@rel_store.keys
end

#sizeInteger

Size of the set

Returns:

  • (Integer)


196
197
198
# File 'lib/opds/support/linkset.rb', line 196

def size
	@store.size
end

#textsArray

Returns all titles.

Returns:

  • (Array)

    all titles



217
218
219
# File 'lib/opds/support/linkset.rb', line 217

def texts
	@txt_store.keys
end

#to_yamlObject



235
236
237
# File 'lib/opds/support/linkset.rb', line 235

def to_yaml
	@store.to_yaml
end