Class: KLookup::Lookup::Kanji

Inherits:
Object
  • Object
show all
Defined in:
lib/klookup/lookup_kanji.rb

Overview

An abstract representation of 漢字 (kanji).

Constant Summary collapse

@@data =

A class variable because we have class methods which need this.

KLookup::Lookup.default_handler

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kanji) ⇒ Kanji

Returns a new instance of Kanji.



18
19
20
21
22
23
24
25
# File 'lib/klookup/lookup_kanji.rb', line 18

def initialize(kanji)
  unless @@data.instance.is_kanji?(kanji)
    raise ArgumentError
  end
  @kanji=kanji.to_s
  @reading=nil
  @meaning=nil
end

Class Method Details

.exist?(kanji) ⇒ Boolean

Returns true if kanji exists in database.

Returns:

  • (Boolean)


58
59
60
61
62
63
64
65
# File 'lib/klookup/lookup_kanji.rb', line 58

def self.exist?(kanji)
  begin
    new(kanji)
    return true
  rescue ArgumentError
    return false
  end
end

.handlerObject

Get handler used by this class.



28
29
30
# File 'lib/klookup/lookup_kanji.rb', line 28

def self.handler
  @@data
end

.handler=(h) ⇒ Object

Set handler used by this class (see also Lookup.handler=).



33
34
35
# File 'lib/klookup/lookup_kanji.rb', line 33

def self.handler=(h)
  @@data = h
end

.lookup(strokes = nil, *radicals) ⇒ Object

Returns an array of Kanji that are made up of these radicals which have strokes strokes.

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/klookup/lookup_kanji.rb', line 39

def self.lookup(strokes=nil, *radicals)
  unless strokes.nil?
    raise ArgumentError unless strokes.respond_to?(:to_i)
    strokes=strokes.to_i
  end

  raise ArgumentError unless radicals.respond_to?(:to_a)
  radicals = radicals.to_a.flatten
  raise ArgumentError unless radicals.size > 0
  radicals.collect! { |r|
    raise ArgumentError unless r.respond_to?(:to_s)
    r.to_s
  }
  return @@data.instance.get_kanji(strokes, radicals).collect {|k|
    KLookup::Lookup::Kanji.new(k)
  }
end

Instance Method Details

#==(kanji) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/klookup/lookup_kanji.rb', line 67

def == (kanji)
  unless kanji.is_a? KLookup::Lookup::Kanji
    kanji = KLookup::Lookup::Kanji.new(kanji.to_s)
  end
  return true if kanji.to_s == to_s
  false
end

#meaningObject

Returns an array of English meanings.



101
102
103
104
105
106
# File 'lib/klookup/lookup_kanji.rb', line 101

def meaning
  if @meaning.nil?
    @meaning = @@data.instance.get_meaning(to_s)
  end
  @meaning
end

#radicalsObject

Returns an array of radicals that make up a kanji.



76
77
78
79
80
# File 'lib/klookup/lookup_kanji.rb', line 76

def radicals
  return @@data.instance.get_radicals(to_s).collect {|r|
    KLookup::Lookup::Radical.new(r)
  }
end

#readingObject

Returns a Struct of arrays of Japanese readings.

KLookup::Lookup::Kanji.new(‘富’).reading KLookup::Database::FlatFile::KanjiDic.instance.get_reading(‘富’) #=> #<struct #<Class:0xb7d3b5dc> reading=[“フ”, “フウ”, “と.む”, “とみ”],

name_reading=["と", "とん", "ふっ"]>


93
94
95
96
97
98
# File 'lib/klookup/lookup_kanji.rb', line 93

def reading
  if @reading.nil?
    @reading = @@data.instance.get_reading(to_s)
  end
  @reading
end

#to_sObject

Returns a textual representation of a radical.



83
84
85
# File 'lib/klookup/lookup_kanji.rb', line 83

def to_s
  @kanji
end