Class: KLookup::Lookup::Kanji
- Inherits:
-
Object
- Object
- KLookup::Lookup::Kanji
- 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
-
.exist?(kanji) ⇒ Boolean
Returns true if kanji exists in database.
-
.handler ⇒ Object
Get handler used by this class.
-
.handler=(h) ⇒ Object
Set handler used by this class (see also Lookup.handler=).
-
.lookup(strokes = nil, *radicals) ⇒ Object
Returns an array of Kanji that are made up of these
radicals
which havestrokes
strokes.
Instance Method Summary collapse
- #==(kanji) ⇒ Object
-
#initialize(kanji) ⇒ Kanji
constructor
A new instance of Kanji.
-
#meaning ⇒ Object
Returns an array of English meanings.
-
#radicals ⇒ Object
Returns an array of radicals that make up a kanji.
-
#reading ⇒ Object
Returns a Struct of arrays of Japanese readings.
-
#to_s ⇒ Object
Returns a textual representation of a radical.
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.
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 |
.handler ⇒ Object
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.
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 |
#meaning ⇒ Object
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 |
#radicals ⇒ Object
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 |
#reading ⇒ Object
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_s ⇒ Object
Returns a textual representation of a radical.
83 84 85 |
# File 'lib/klookup/lookup_kanji.rb', line 83 def to_s @kanji end |