Class: ReaPack::Index::Link

Inherits:
Object
  • Object
show all
Defined in:
lib/reapack/index/metadata.rb

Constant Summary collapse

TAG =
'link'.freeze
REL =
'rel'.freeze
URL =
'href'.freeze
VALID_TYPES =

the first type will be the default one

[:website, :screenshot, :donation].freeze
/\A(.+?)(?:\s+|=)(\w+?:\/\/.+)\Z/.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, url) ⇒ Link

Returns a new instance of Link.



212
213
214
215
# File 'lib/reapack/index/metadata.rb', line 212

def initialize(name, url)
  @name, @url = name, url
  @is_new = @modified = false
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



217
218
219
# File 'lib/reapack/index/metadata.rb', line 217

def name
  @name
end

#urlObject

Returns the value of attribute url.



217
218
219
# File 'lib/reapack/index/metadata.rb', line 217

def url
  @url
end

Class Method Details

.check_type(type) ⇒ Object

Raises:

  • (ArgumentError)


186
187
188
# File 'lib/reapack/index/metadata.rb', line 186

def self.check_type(type)
  raise ArgumentError unless VALID_TYPES.include? type
end

.find(type, search, parent) ⇒ Object



206
207
208
209
210
# File 'lib/reapack/index/metadata.rb', line 206

def self.find(type, search, parent)
  Link.find_all(type, parent).find {|node|
    node.text == search || node[URL] == search
  }
end

.find_all(type, parent) ⇒ Object



196
197
198
199
200
201
202
203
204
# File 'lib/reapack/index/metadata.rb', line 196

def self.find_all(type, parent)
  Link.check_type type

  return [] unless parent

  parent.element_children.select {|node|
    node.name == TAG && Link.same_type?(type, node[REL].to_s.to_sym)
  }
end

.from_node(node) ⇒ Object



178
179
180
181
182
183
184
# File 'lib/reapack/index/metadata.rb', line 178

def self.from_node(node)
  name, url = node.text.to_s, node[URL].to_s
  url, name = name, url if url.empty?
  name = url if name.empty?

  self.new name, url
end

.same_type?(type, user) ⇒ Boolean

Returns:

  • (Boolean)


190
191
192
193
194
# File 'lib/reapack/index/metadata.rb', line 190

def self.same_type?(type, user)
  # match invalid types by the first value of VALID_TYPES
  # while the other values require an exact match
  user == type || (type == VALID_TYPES[0] && VALID_TYPES.index(user).to_i < 1)
end

.split(input) ⇒ Object



170
171
172
173
174
175
176
# File 'lib/reapack/index/metadata.rb', line 170

def self.split(input)
  if input =~ LINK_REGEX
    [$1, $2]
  else
    [input]
  end
end

Instance Method Details

#==(other) ⇒ Object



219
220
221
# File 'lib/reapack/index/metadata.rb', line 219

def ==(other)
  name == other.name && url == other.url
end

#is_new?Boolean

Returns:

  • (Boolean)


223
224
225
# File 'lib/reapack/index/metadata.rb', line 223

def is_new?
  @is_new
end

#modified?Boolean

Returns:

  • (Boolean)


227
228
229
# File 'lib/reapack/index/metadata.rb', line 227

def modified?
  @modified
end