Class: NHKore::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/nhkore/entry.rb

Constant Summary collapse

HYOUKI_SEP =
'・'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEntry

Returns a new instance of Entry.



21
22
23
24
25
26
# File 'lib/nhkore/entry.rb', line 21

def initialize
  super

  @defns = []
  @id = nil
end

Instance Attribute Details

#defnsObject (readonly)

Returns the value of attribute defns.



18
19
20
# File 'lib/nhkore/entry.rb', line 18

def defns
  @defns
end

#idObject

Returns the value of attribute id.



19
20
21
# File 'lib/nhkore/entry.rb', line 19

def id
  @id
end

Class Method Details

.scrape(id, array, missingno: nil, url: nil) ⇒ Object



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

def self.scrape(id,array,missingno: nil,url: nil)
  entry = Entry.new

  entry.id = Util.unspace_web_str(id.to_s).downcase

  return nil if entry.id.empty?

  array.each do |hash|
    defn = Defn.scrape(hash,missingno: missingno,url: url)
    entry.defns << defn unless defn.nil?
  end

  return nil if entry.defns.empty?
  return entry
end

Instance Method Details

#build_defnObject



28
29
30
31
32
33
34
35
# File 'lib/nhkore/entry.rb', line 28

def build_defn
  i = 0
  defns = @defns.map do |defn|
    "#{i += 1})#{defn}" # Japanese parenthesis
  end

  return defns.join("\n")
end

#build_hyoukiObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/nhkore/entry.rb', line 37

def build_hyouki
  # Since Ruby v1.9, Hash preserves order.
  # Ruby v2.7 doc for Set still says no guarantee of order, so don't use.
  hyoukis = {}

  @defns.each do |defn|
    defn.hyoukis.each do |hyouki|
      hyouki = hyouki.chomp(HYOUKI_SEP)

      next if hyouki.empty?

      hyoukis[hyouki] = true
    end
  end

  return hyoukis.keys.join(HYOUKI_SEP)
end

#to_sObject



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/nhkore/entry.rb', line 71

def to_s
  s = ''.dup

  return s if @defns.empty?

  hyouki = build_hyouki

  s << "#{hyouki}\n" unless Util.empty_web_str?(hyouki)
  s << build_defn

  return s
end