Class: NHKore::Entry

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

Overview

Author:

  • Jonathan Bradley Whited

Since:

  • 0.2.0

Constant Summary collapse

HYOUKI_SEP =

Since:

  • 0.2.0

''

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEntry

Returns a new instance of Entry.

Since:

  • 0.2.0



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

def initialize
  super()

  @defns = []
  @id = nil
end

Instance Attribute Details

#defnsObject (readonly)

Since:

  • 0.2.0



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

def defns
  @defns
end

#idObject

Since:

  • 0.2.0



25
26
27
# File 'lib/nhkore/entry.rb', line 25

def id
  @id
end

Class Method Details

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

Since:

  • 0.2.0



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/nhkore/entry.rb', line 63

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

Since:

  • 0.2.0



34
35
36
37
38
39
40
41
42
43
# File 'lib/nhkore/entry.rb', line 34

def build_defn
  defns = []
  i = 0

  @defns.each do |defn|
    defns << "#{i += 1}#{defn}" # Japanese parenthesis
  end

  return defns.join("\n")
end

#build_hyoukiObject

Since:

  • 0.2.0



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/nhkore/entry.rb', line 45

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

Since:

  • 0.2.0



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/nhkore/entry.rb', line 79

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