Class: HatebuEntry::Entry

Inherits:
Struct
  • Object
show all
Defined in:
lib/hatebu_entry.rb

Overview

link: uri string count: integer title: string

Defined Under Namespace

Classes: MergeError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#countObject Also known as: bookmarks

Returns the value of attribute count

Returns:

  • (Object)

    the current value of count



14
15
16
# File 'lib/hatebu_entry.rb', line 14

def count
  @count
end

Returns the value of attribute link

Returns:

  • (Object)

    the current value of link



14
15
16
# File 'lib/hatebu_entry.rb', line 14

def link
  @link
end

#titleObject

Returns the value of attribute title

Returns:

  • (Object)

    the current value of title



14
15
16
# File 'lib/hatebu_entry.rb', line 14

def title
  @title
end

Class Method Details

.merge(ls_a, ls_b) ⇒ Object

Merge two entry lists



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/hatebu_entry.rb', line 55

def self.merge(ls_a, ls_b)
  if [ls_a, ls_b].all? { |ls| ls.is_a? Entry }
    raise ArgumentError, 'Arguments must be entry objects'
  end
  entries = []
  ls_a.each do |a|
    if m = ls_b.detect { |b| a.homogeneous? b }
      entries.push a.merge(m)
      ls_b.delete(m)
    else
      entries.push a
    end
  end
  entries + ls_b
end

Instance Method Details

#dateObject



17
18
19
20
21
# File 'lib/hatebu_entry.rb', line 17

def date
  @date ||= Date.parse(link)
rescue ArgumentError
  nil
end

#homogeneous?(other) ⇒ Boolean Also known as: same?

to find same entry but other hosts

Returns:

  • (Boolean)


28
29
30
31
32
33
# File 'lib/hatebu_entry.rb', line 28

def homogeneous?(other)
  return false if self == other
  return nil if [self, other].any? { |e| e.date.nil? }
  self.date == other.date &&
      title_similar?(self.title, other.title)
end

#merge(other) ⇒ Object

Merge its count



45
46
47
48
49
50
51
52
# File 'lib/hatebu_entry.rb', line 45

def merge(other)
  count = self.count + other.count
  if block_given? && !yield(self, other)
    raise MergeError, "They can't merge"
  else
    Entry.new(self.link, count, self.title)
  end
end

#to_sObject



23
24
25
# File 'lib/hatebu_entry.rb', line 23

def to_s
  "%5d: %s (%s)" % [count, title, link]
end